mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-20 20:39:08 +02:00
[management] explicit accountID check when deleting a user (#6944)
This commit is contained in:
@@ -321,6 +321,10 @@ func (am *DefaultAccountManager) DeleteUser(ctx context.Context, accountID, init
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if targetUser.AccountID != accountID {
|
||||||
|
return status.NewUserNotFoundError(targetUserID)
|
||||||
|
}
|
||||||
|
|
||||||
if targetUser.Role == types.UserRoleOwner {
|
if targetUser.Role == types.UserRoleOwner {
|
||||||
return status.NewOwnerDeletePermissionError()
|
return status.NewOwnerDeletePermissionError()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -802,6 +802,52 @@ func TestUser_DeleteUser_SelfDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUser_DeleteUser_OtherAccount(t *testing.T) {
|
||||||
|
testStore, cleanup, err := store.NewTestStoreFromSQL(context.Background(), "", t.TempDir())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error when creating store: %s", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(cleanup)
|
||||||
|
|
||||||
|
account := newAccountWithId(context.Background(), mockAccountID, mockUserID, "", "", "", false)
|
||||||
|
if err = testStore.SaveAccount(context.Background(), account); err != nil {
|
||||||
|
t.Fatalf("Error when saving account: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
otherAccount := newAccountWithId(context.Background(), "otherAccount", "otherOwner", "", "", "", false)
|
||||||
|
otherAccount.Users["otherRegularUser"] = &types.User{
|
||||||
|
Id: "otherRegularUser",
|
||||||
|
AccountID: "otherAccount",
|
||||||
|
Role: types.UserRoleUser,
|
||||||
|
}
|
||||||
|
otherAccount.Users["otherServiceUser"] = &types.User{
|
||||||
|
Id: "otherServiceUser",
|
||||||
|
AccountID: "otherAccount",
|
||||||
|
Role: types.UserRoleUser,
|
||||||
|
IsServiceUser: true,
|
||||||
|
ServiceUserName: "otherServiceUser",
|
||||||
|
}
|
||||||
|
if err = testStore.SaveAccount(context.Background(), otherAccount); err != nil {
|
||||||
|
t.Fatalf("Error when saving other account: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
am := DefaultAccountManager{
|
||||||
|
Store: testStore,
|
||||||
|
eventStore: &activity.InMemoryEventStore{},
|
||||||
|
permissionsManager: permissions.NewManager(testStore),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, targetUserID := range []string{"otherRegularUser", "otherServiceUser"} {
|
||||||
|
t.Run(targetUserID, func(t *testing.T) {
|
||||||
|
err := am.DeleteUser(context.Background(), mockAccountID, mockUserID, targetUserID)
|
||||||
|
assert.Equal(t, status.NewUserNotFoundError(targetUserID), err)
|
||||||
|
|
||||||
|
_, err = testStore.GetUserByUserID(context.Background(), store.LockingStrengthNone, targetUserID)
|
||||||
|
assert.NoError(t, err, "user of another account must not be deleted")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUser_DeleteUser_regularUser(t *testing.T) {
|
func TestUser_DeleteUser_regularUser(t *testing.T) {
|
||||||
store, cleanup, err := store.NewTestStoreFromSQL(context.Background(), "", t.TempDir())
|
store, cleanup, err := store.NewTestStoreFromSQL(context.Background(), "", t.TempDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user