Refactor new account handling

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga
2024-11-25 17:43:39 +03:00
parent e17d8127e3
commit 71af7edd05
5 changed files with 164 additions and 146 deletions

View File

@@ -953,8 +953,9 @@ func validateUserUpdate(groupsMap map[string]*nbgroup.Group, initiatorUser, oldU
return nil
}
// GetOrCreateAccountByUser returns an existing account for a given user id or creates a new one if doesn't exist
func (am *DefaultAccountManager) GetOrCreateAccountByUser(ctx context.Context, userID, domain string) (*Account, error) {
// GetOrCreateAccountIDByUser returns the account ID for a given user ID.
// If no account exists for the user, it creates a new one using the specified domain.
func (am *DefaultAccountManager) GetOrCreateAccountIDByUser(ctx context.Context, userID, domain string) (string, error) {
start := time.Now()
unlock := am.Store.AcquireGlobalLock(ctx)
defer unlock()
@@ -962,34 +963,39 @@ func (am *DefaultAccountManager) GetOrCreateAccountByUser(ctx context.Context, u
lowerDomain := strings.ToLower(domain)
account, err := am.Store.GetAccountByUser(ctx, userID)
accountID, err := am.Store.GetAccountIDByUserID(ctx, LockingStrengthShare, userID)
if err != nil {
if s, ok := status.FromError(err); ok && s.Type() == status.NotFound {
account, err = am.newAccount(ctx, userID, lowerDomain)
accountID, err = am.newAccount(ctx, userID, lowerDomain)
if err != nil {
return nil, err
}
err = am.Store.SaveAccount(ctx, account)
if err != nil {
return nil, err
return "", err
}
return accountID, nil
} else {
// other error
return nil, err
return "", err
}
}
userObj := account.Users[userID]
if lowerDomain != "" && account.Domain != lowerDomain && userObj.Role == UserRoleOwner {
account.Domain = lowerDomain
err = am.Store.SaveAccount(ctx, account)
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
user, err := transaction.GetUserByUserID(ctx, LockingStrengthShare, userID)
if err != nil {
return nil, status.Errorf(status.Internal, "failed updating account with domain")
return err
}
}
return account, nil
accDomain, accCategory, err := transaction.GetAccountDomainAndCategory(ctx, LockingStrengthUpdate, accountID)
if err != nil {
return err
}
if lowerDomain != "" && accDomain != lowerDomain && user.Role == UserRoleOwner {
return transaction.UpdateAccountDomainAttributes(ctx, LockingStrengthUpdate, accountID, lowerDomain, accCategory, nil)
}
return nil
})
return accountID, err
}
// GetUsersFromAccount performs a batched request for users from IDP by account ID apply filter on what data to return