@@ -43,9 +43,9 @@ func NewOrganizationService(muc *biz.MembershipUseCase, ouc *biz.OrganizationUse
4343 }
4444}
4545
46- // Create persists an organization with a given name and associate it to the current user.
46+ // Create persists an organization with a given name and associates it with the current user.
4747func (s * OrganizationService ) Create (ctx context.Context , req * pb.OrganizationServiceCreateRequest ) (* pb.OrganizationServiceCreateResponse , error ) {
48- currentUser , err := requireCurrentUser (ctx )
48+ currentUser , _ , err := requireCurrentUserOrAPIToken (ctx )
4949 if err != nil {
5050 return nil , err
5151 }
@@ -65,8 +65,11 @@ func (s *OrganizationService) Create(ctx context.Context, req *pb.OrganizationSe
6565 return nil , handleUseCaseErr (err , s .log )
6666 }
6767
68- if _ , err := s .membershipUC .Create (ctx , org .ID , currentUser .ID , biz .WithMembershipRole (authz .RoleOwner ), biz .WithCurrentMembership ()); err != nil {
69- return nil , handleUseCaseErr (err , s .log )
68+ // Add membership if invoker is a user
69+ if currentUser != nil {
70+ if _ , err := s .membershipUC .Create (ctx , org .ID , currentUser .ID , biz .WithMembershipRole (authz .RoleOwner ), biz .WithCurrentMembership ()); err != nil {
71+ return nil , handleUseCaseErr (err , s .log )
72+ }
7073 }
7174
7275 return & pb.OrganizationServiceCreateResponse {Result : bizOrgToPb (org )}, nil
@@ -211,25 +214,15 @@ func (s *OrganizationService) UpdateMembership(ctx context.Context, req *pb.Orga
211214}
212215
213216func (s * OrganizationService ) canCreateOrganization (ctx context.Context ) (bool , error ) {
214- // Restricted org creation is disabled, allow creation
215- if ! s .authz .RestrictOrgCreation {
217+ // if org creation restriction is disabled, allow creation to all users
218+ if ! s .authz .RestrictOrgCreation && entities . CurrentUser ( ctx ) != nil {
216219 return true , nil
217220 }
218221
219- m := entities .CurrentMembership (ctx )
220- for _ , rm := range m .Resources {
221- if rm .ResourceType != authz .ResourceTypeInstance {
222- continue
223- }
224-
225- pass , err := s .authz .Enforce (ctx , string (rm .Role ), authz .PolicyOrganizationCreate )
226- if err != nil {
227- return false , handleUseCaseErr (err , s .log )
228- }
229- if pass {
230- return true , nil
231- }
222+ // otherwise, check for permissions (both users and API tokens)
223+ if err := s .checkPolicy (ctx , authz .PolicyOrganizationCreate ); err != nil {
224+ return false , err
232225 }
233226
234- return false , nil
227+ return true , nil
235228}
0 commit comments