Skip to content

Commit

Permalink
Delete all invitations when removing a user from a project
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Jul 3, 2024
1 parent 924bd1a commit d126a74
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
15 changes: 15 additions & 0 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions database/query/invitations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ DELETE FROM user_invites WHERE code = $1 RETURNING *;

-- name: UpdateInvitationRole :one
UPDATE user_invites SET role = $2, updated_at = NOW() WHERE code = $1 RETURNING *;


-- DeleteInvitationsBySponsor deletes all invitations by a sponsor. This is intended
-- to be called by a user who has decided to revoke all invitations they have issued.

-- name: DeleteInvitationsBySponsor :many
DELETE FROM user_invites WHERE sponsor = $1 RETURNING *;
17 changes: 16 additions & 1 deletion internal/controlplane/handlers_authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ func (s *Server) removeRole(
}

// Verify if user exists
if _, err := s.store.GetUserBySubject(ctx, identity.String()); err != nil {
userToRemove, err := s.store.GetUserBySubject(ctx, identity.String())
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, util.UserVisibleError(codes.NotFound, "User not found")
}
Expand All @@ -631,6 +632,20 @@ func (s *Server) removeRole(
}
}

// In case this user is a sponsor of an invitation, we need to remove the invitation
deletedInvites, err := s.store.DeleteInvitationsBySponsor(ctx, userToRemove.ID)
if err != nil {
return nil, status.Errorf(codes.Internal, "error deleting invitations: %v", err)
}

if len(deletedInvites) > 0 {
zerolog.Ctx(ctx).Info().
Int("invites_deleted", len(deletedInvites)).
Str("sponsor_subject", userToRemove.IdentitySubject).
Int32("sponsor", userToRemove.ID).
Msg("deleted pending invitations from sponsor")
}

// Delete the role assignment
if err := s.authzClient.Delete(ctx, identity.String(), roleToRemove, targetProject); err != nil {
return nil, status.Errorf(codes.Internal, "error writing role assignment: %v", err)
Expand Down
38 changes: 38 additions & 0 deletions internal/db/invitations.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d126a74

Please sign in to comment.