mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-06 17:34:35 -04:00
Compare commits
5 Commits
add-defaul
...
handle-exi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2b86463e96 | ||
|
|
9deff6f06b | ||
|
|
1a1e94c805 | ||
|
|
ed939bf7f5 | ||
|
|
7caf733217 |
@@ -571,19 +571,19 @@ func (am *DefaultAccountManager) newAccount(ctx context.Context, userID, domain
|
||||
for i := 0; i < 2; i++ {
|
||||
accountId := xid.New().String()
|
||||
|
||||
_, err := am.Store.GetAccount(ctx, accountId)
|
||||
statusErr, _ := status.FromError(err)
|
||||
switch {
|
||||
case err == nil:
|
||||
log.WithContext(ctx).Warnf("an account with ID already exists, retrying...")
|
||||
continue
|
||||
case statusErr.Type() == status.NotFound:
|
||||
newAccount := newAccountWithId(ctx, accountId, userID, domain, am.disableDefaultPolicy)
|
||||
am.StoreEvent(ctx, userID, newAccount.Id, accountId, activity.AccountCreated, nil)
|
||||
return newAccount, nil
|
||||
default:
|
||||
exists, err := am.Store.AccountExists(ctx, store.LockingStrengthShare, accountId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if exists {
|
||||
log.WithContext(ctx).Warnf("an account with ID already exists, retrying...")
|
||||
continue
|
||||
}
|
||||
|
||||
newAccount := newAccountWithId(ctx, accountId, userID, domain, am.disableDefaultPolicy)
|
||||
am.StoreEvent(ctx, userID, newAccount.Id, accountId, activity.AccountCreated, nil)
|
||||
return newAccount, nil
|
||||
}
|
||||
|
||||
return nil, status.Errorf(status.Internal, "error while creating new account")
|
||||
@@ -1143,21 +1143,29 @@ func (am *DefaultAccountManager) addNewUserToDomainAccount(ctx context.Context,
|
||||
unlockAccount := am.Store.AcquireWriteLockByUID(ctx, domainAccountID)
|
||||
defer unlockAccount()
|
||||
|
||||
newUser := types.NewRegularUser(userAuth.UserId)
|
||||
newUser.AccountID = domainAccountID
|
||||
err := am.Store.SaveUser(ctx, newUser)
|
||||
user, err := am.Store.GetUserByUserID(ctx, store.LockingStrengthShare, userAuth.UserId)
|
||||
if err != nil {
|
||||
if sErr, ok := status.FromError(err); ok && sErr.Type() == status.NotFound {
|
||||
newUser := types.NewRegularUser(userAuth.UserId)
|
||||
newUser.AccountID = domainAccountID
|
||||
err = am.Store.SaveUser(ctx, newUser)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = am.addAccountIDToIDPAppMeta(ctx, userAuth.UserId, domainAccountID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
am.StoreEvent(ctx, userAuth.UserId, userAuth.UserId, domainAccountID, activity.UserJoined, nil)
|
||||
return domainAccountID, nil
|
||||
}
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = am.addAccountIDToIDPAppMeta(ctx, userAuth.UserId, domainAccountID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
am.StoreEvent(ctx, userAuth.UserId, userAuth.UserId, domainAccountID, activity.UserJoined, nil)
|
||||
|
||||
return domainAccountID, nil
|
||||
return user.AccountID, nil
|
||||
}
|
||||
|
||||
// redeemInvite checks whether user has been invited and redeems the invite
|
||||
|
||||
@@ -3453,6 +3453,50 @@ func TestPropagateUserGroupMemberships(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestDefaultAccountManager_AddNewUserToDomainAccount(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
userAuth nbcontext.UserAuth
|
||||
expectedRole types.UserRole
|
||||
}{
|
||||
{
|
||||
name: "existing user",
|
||||
userAuth: nbcontext.UserAuth{
|
||||
Domain: "example.com",
|
||||
UserId: "user1",
|
||||
},
|
||||
expectedRole: types.UserRoleOwner,
|
||||
},
|
||||
{
|
||||
name: "new user",
|
||||
userAuth: nbcontext.UserAuth{
|
||||
Domain: "example.com",
|
||||
UserId: "user2",
|
||||
},
|
||||
expectedRole: types.UserRoleUser,
|
||||
},
|
||||
}
|
||||
|
||||
manager, err := createManager(t)
|
||||
require.NoError(t, err)
|
||||
|
||||
accountID, err := manager.GetAccountIDByUserID(context.Background(), "user1", "example.com")
|
||||
require.NoError(t, err, "create init user failed")
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
userAccountID, err := manager.addNewUserToDomainAccount(context.Background(), accountID, tc.userAuth)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, accountID, userAccountID)
|
||||
|
||||
user, err := manager.Store.GetUserByUserID(context.Background(), store.LockingStrengthShare, tc.userAuth.UserId)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, accountID, user.AccountID)
|
||||
assert.Equal(t, tc.expectedRole, user.Role)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultAccountManager_GetAccountOnboarding(t *testing.T) {
|
||||
manager, err := createManager(t)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user