Skip to content

Commit

Permalink
Prevent self deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
pappz committed Sep 20, 2023
1 parent d4b6d76 commit 132616d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions management/server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ func (am *DefaultAccountManager) GetUser(claims jwtclaims.AuthorizationClaims) (

// DeleteUser deletes a user from the given account.
func (am *DefaultAccountManager) DeleteUser(accountID, initiatorUserID string, targetUserID string) error {
if initiatorUserID == targetUserID {
return status.Errorf(status.InvalidArgument, "self deletion is not allowed")
}
unlock := am.Store.AcquireAccountLock(accountID)
defer unlock()

Expand Down
28 changes: 27 additions & 1 deletion management/server/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func TestUser_DeleteUser_ServiceUser(t *testing.T) {
assert.Nil(t, store.Accounts[mockAccountID].Users[mockServiceUserID])
}

func TestUser_DeleteUser_regularUser(t *testing.T) {
func TestUser_DeleteUser_SelfDelete(t *testing.T) {
store := newStore(t)
account := newAccountWithId(mockAccountID, mockUserID, "")

Expand All @@ -439,6 +439,32 @@ func TestUser_DeleteUser_regularUser(t *testing.T) {
}

err = am.DeleteUser(mockAccountID, mockUserID, mockUserID)
if err == nil {
t.Fatalf("failed to prevent self deletion")
}
}

func TestUser_DeleteUser_regularUser(t *testing.T) {
store := newStore(t)
account := newAccountWithId(mockAccountID, mockUserID, "")
targetId := "user2"
account.Users[targetId] = &User{
Id: targetId,
IsServiceUser: true,
ServiceUserName: "user2username",
}

err := store.SaveAccount(account)
if err != nil {
t.Fatalf("Error when saving account: %s", err)
}

am := DefaultAccountManager{
Store: store,
eventStore: &activity.InMemoryEventStore{},
}

err = am.DeleteUser(mockAccountID, mockUserID, targetId)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
Expand Down

0 comments on commit 132616d

Please sign in to comment.