Skip to content

Commit 01e1902

Browse files
authored
fix(controlplane): bubble up authorization permission (#408)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 4f278b6 commit 01e1902

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

app/controlplane/internal/biz/orginvitation.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,8 @@ func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, senderID, rec
8686
return nil, NewErrValidationStr("sender and receiver emails cannot be the same")
8787
}
8888

89-
// 3 - The receiver does not exist in the org already
90-
memberships, err := uc.mRepo.FindByOrg(ctx, orgUUID)
91-
if err != nil {
92-
return nil, fmt.Errorf("error finding memberships for user %s: %w", senderUUID.String(), err)
93-
}
94-
95-
for _, m := range memberships {
96-
if m.UserEmail == receiverEmail {
97-
return nil, NewErrValidationStr("user already exists in the org")
98-
}
99-
}
100-
101-
// 4 - Check if the user has permissions to invite to the org
102-
memberships, err = uc.mRepo.FindByUser(ctx, senderUUID)
89+
// 3 - Check if the user has permissions to invite to the org
90+
memberships, err := uc.mRepo.FindByUser(ctx, senderUUID)
10391
if err != nil {
10492
return nil, fmt.Errorf("error finding memberships for user %s: %w", senderUUID.String(), err)
10593
}
@@ -117,6 +105,18 @@ func (uc *OrgInvitationUseCase) Create(ctx context.Context, orgID, senderID, rec
117105
return nil, NewErrNotFound("user does not have permission to invite to this org")
118106
}
119107

108+
// 4 - The receiver does not exist in the org already
109+
memberships, err = uc.mRepo.FindByOrg(ctx, orgUUID)
110+
if err != nil {
111+
return nil, fmt.Errorf("error finding memberships for user %s: %w", senderUUID.String(), err)
112+
}
113+
114+
for _, m := range memberships {
115+
if m.UserEmail == receiverEmail {
116+
return nil, NewErrValidationStr("user already exists in the org")
117+
}
118+
}
119+
120120
// 5 - Check if there is already an invitation for this user for this org
121121
m, err := uc.repo.PendingInvitation(ctx, orgUUID, receiverEmail)
122122
if err != nil {

app/controlplane/internal/biz/orginvitation_integration_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,18 @@ func (s *OrgInvitationIntegrationTestSuite) TestCreate() {
7575
s.Nil(invite)
7676
})
7777

78-
s.T().Run("user is not member of that org", func(t *testing.T) {
78+
s.T().Run("sender is not member of that org", func(t *testing.T) {
7979
invite, err := s.OrgInvitation.Create(context.Background(), s.org3.ID, s.user.ID, receiverEmail)
8080
s.Error(err)
81+
s.ErrorContains(err, "user does not have permission to invite to this org")
82+
s.True(biz.IsNotFound(err))
83+
s.Nil(invite)
84+
})
85+
86+
s.T().Run("sender is not member of that org but receiver is", func(t *testing.T) {
87+
invite, err := s.OrgInvitation.Create(context.Background(), s.org3.ID, s.user.ID, s.user2.Email)
88+
s.Error(err)
89+
s.ErrorContains(err, "user does not have permission to invite to this org")
8190
s.True(biz.IsNotFound(err))
8291
s.Nil(invite)
8392
})
@@ -232,4 +241,6 @@ func (s *OrgInvitationIntegrationTestSuite) SetupTest() {
232241
assert.NoError(err)
233242
_, err = s.Membership.Create(ctx, s.org1.ID, s.user2.ID, true)
234243
assert.NoError(err)
244+
_, err = s.Membership.Create(ctx, s.org3.ID, s.user2.ID, true)
245+
assert.NoError(err)
235246
}

0 commit comments

Comments
 (0)