-
Notifications
You must be signed in to change notification settings - Fork 44
feat(rbac): allow tokens to create org invitations and storage backends #2710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4aeb65d
allow creating orgs to tokens with proper permissions
jiparis 838f00c
fix
jiparis fbee36e
create invitations
jiparis 32add98
use enforcer
jiparis 0a2f322
check policies in all cases
jiparis 5cd6698
Merge branch 'main' into PFM-4137
jiparis 4803ca3
use currentuser
jiparis 8fa108f
Merge branch 'PFM-4137' into PFM-4146
jiparis 0c0e05f
Merge branch 'main' into PFM-4146
jiparis e3144c3
support org invitations for instance admins (users and tokens)
jiparis 877ceb3
fix tests
jiparis e25f747
fix test
jiparis d6f7c76
add permission
jiparis 8e5f831
remove comment
jiparis c96ea3d
Merge branch 'PFM-4146' into PFM-4147-casbackends
jiparis 8dd2cc3
add casbackend/list, as it's used by the CLI
jiparis 69054e0
fix tests
jiparis 0488f3a
move membership to function
jiparis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,7 @@ type OrgInvitationContext struct { | |
| } | ||
|
|
||
| type OrgInvitationRepo interface { | ||
| Create(ctx context.Context, orgID, senderID uuid.UUID, receiverEmail string, role authz.Role, invCtx *OrgInvitationContext) (*OrgInvitation, error) | ||
| Create(ctx context.Context, orgID uuid.UUID, senderID *uuid.UUID, receiverEmail string, role authz.Role, invCtx *OrgInvitationContext) (*OrgInvitation, error) | ||
| FindByID(ctx context.Context, ID uuid.UUID) (*OrgInvitation, error) | ||
| PendingInvitation(ctx context.Context, orgID uuid.UUID, receiverEmail string) (*OrgInvitation, error) | ||
| PendingInvitations(ctx context.Context, receiverEmail string) ([]*OrgInvitation, error) | ||
|
|
@@ -88,8 +88,9 @@ func NewOrgInvitationUseCase(r OrgInvitationRepo, mRepo MembershipRepo, uRepo Us | |
| } | ||
|
|
||
| type invitationCreateOpts struct { | ||
| role authz.Role | ||
| ctx *OrgInvitationContext | ||
| role authz.Role | ||
| ctx *OrgInvitationContext | ||
| senderID *uuid.UUID | ||
| } | ||
|
|
||
| type InvitationCreateOpt func(*invitationCreateOpts) | ||
|
|
@@ -108,7 +109,13 @@ func WithInvitationContext(ctx *OrgInvitationContext) InvitationCreateOpt { | |
| } | ||
| } | ||
|
|
||
| func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, senderID, receiverEmail string, createOpts ...InvitationCreateOpt) (*OrgInvitation, error) { | ||
| func WithSender(senderID uuid.UUID) InvitationCreateOpt { | ||
| return func(o *invitationCreateOpts) { | ||
| o.senderID = &senderID | ||
| } | ||
| } | ||
|
|
||
| func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, receiverEmail string, createOpts ...InvitationCreateOpt) (*OrgInvitation, error) { | ||
| receiverEmail = strings.ToLower(receiverEmail) | ||
|
|
||
| // 1 - Static Validation | ||
|
|
@@ -135,37 +142,26 @@ func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, senderID, rec | |
| return nil, NewErrInvalidUUID(err) | ||
| } | ||
|
|
||
| senderUUID, err := uuid.Parse(senderID) | ||
| if err != nil { | ||
| return nil, NewErrInvalidUUID(err) | ||
| } | ||
|
|
||
| // 2 - the sender exists and it's not the same than the receiver of the invitation | ||
| sender, err := uc.userRepo.FindByID(ctx, senderUUID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error finding sender %s: %w", senderUUID.String(), err) | ||
| } else if sender == nil { | ||
| return nil, NewErrNotFound("sender") | ||
| } | ||
|
|
||
| if sender.Email == receiverEmail { | ||
| return nil, NewErrValidationStr("sender and receiver emails cannot be the same") | ||
| } | ||
| if opts.senderID != nil { | ||
| sender, err := uc.userRepo.FindByID(ctx, *opts.senderID) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error finding sender %s: %w", opts.senderID.String(), err) | ||
| } else if sender == nil { | ||
| return nil, NewErrNotFound("sender") | ||
| } | ||
|
|
||
| // 3 - Check that the user is a member of the given org | ||
| // NOTE: this check is not necessary, as the user is already a member of the org | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the comment states, this is not needed since validation is done in the middleware layer already. |
||
| if membership, err := uc.mRepo.FindByOrgAndUser(ctx, orgUUID, senderUUID); err != nil { | ||
| return nil, fmt.Errorf("failed to find memberships: %w", err) | ||
| } else if membership == nil { | ||
| return nil, NewErrNotFound("user does not have permission to invite to this org") | ||
| if sender.Email == receiverEmail { | ||
| return nil, NewErrValidationStr("sender and receiver emails cannot be the same") | ||
| } | ||
| } | ||
|
|
||
| // 4 - The receiver does exist in the org already | ||
| _, membershipCount, err := uc.mRepo.FindByOrg(ctx, orgUUID, &ListByOrgOpts{ | ||
| Email: &receiverEmail, | ||
| }, pagination.NewDefaultOffsetPaginationOpts()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error finding memberships for user %s: %w", senderUUID.String(), err) | ||
| return nil, fmt.Errorf("error finding memberships for user %s: %w", receiverEmail, err) | ||
| } | ||
|
|
||
| if membershipCount > 0 { | ||
|
|
@@ -183,7 +179,7 @@ func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, senderID, rec | |
| } | ||
|
|
||
| // 6 - Create the invitation | ||
| invitation, err := uc.repo.Create(ctx, orgUUID, senderUUID, receiverEmail, opts.role, opts.ctx) | ||
| invitation, err := uc.repo.Create(ctx, orgUUID, opts.senderID, receiverEmail, opts.role, opts.ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error creating invitation: %w", err) | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just moved this earlier in the chain, so that membership is available to the middlewares in the next selector.