@@ -24,7 +24,8 @@ const (
24
24
// customSiteRole is a placeholder for all custom site roles.
25
25
// This is used for what roles can assign other roles.
26
26
// TODO: Make this more dynamic to allow other roles to grant.
27
- customSiteRole string = "custom-site-role"
27
+ customSiteRole string = "custom-site-role"
28
+ customOrganizationRole string = "custom-organization-role"
28
29
29
30
orgAdmin string = "organization-admin"
30
31
orgMember string = "organization-member"
@@ -125,8 +126,11 @@ func (r *RoleIdentifier) UnmarshalJSON(data []byte) error {
125
126
// Once we have a database implementation, the "default" roles can be defined on the
126
127
// site and orgs, and these functions can be removed.
127
128
128
- func RoleOwner () RoleIdentifier { return RoleIdentifier {Name : owner } }
129
- func CustomSiteRole () RoleIdentifier { return RoleIdentifier {Name : customSiteRole } }
129
+ func RoleOwner () RoleIdentifier { return RoleIdentifier {Name : owner } }
130
+ func CustomSiteRole () RoleIdentifier { return RoleIdentifier {Name : customSiteRole } }
131
+ func CustomOrganizationRole (orgID uuid.UUID ) RoleIdentifier {
132
+ return RoleIdentifier {Name : customOrganizationRole , OrganizationID : orgID }
133
+ }
130
134
func RoleTemplateAdmin () RoleIdentifier { return RoleIdentifier {Name : templateAdmin } }
131
135
func RoleUserAdmin () RoleIdentifier { return RoleIdentifier {Name : userAdmin } }
132
136
func RoleMember () RoleIdentifier { return RoleIdentifier {Name : member } }
@@ -314,6 +318,9 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
314
318
DisplayName : "User Admin" ,
315
319
Site : Permissions (map [string ][]policy.Action {
316
320
ResourceAssignRole .Type : {policy .ActionAssign , policy .ActionDelete , policy .ActionRead },
321
+ // Need organization assign as well to create users. At present, creating a user
322
+ // will always assign them to some organization.
323
+ ResourceAssignOrgRole .Type : {policy .ActionAssign , policy .ActionDelete , policy .ActionRead },
317
324
ResourceUser .Type : {
318
325
policy .ActionCreate , policy .ActionRead , policy .ActionUpdate , policy .ActionDelete ,
319
326
policy .ActionUpdatePersonal , policy .ActionReadPersonal ,
@@ -361,7 +368,7 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
361
368
Site : []Permission {},
362
369
Org : map [string ][]Permission {
363
370
// Org admins should not have workspace exec perms.
364
- organizationID .String (): append (allPermsExcept (ResourceWorkspace , ResourceWorkspaceDormant ), Permissions (map [string ][]policy.Action {
371
+ organizationID .String (): append (allPermsExcept (ResourceWorkspace , ResourceWorkspaceDormant , ResourceAssignRole ), Permissions (map [string ][]policy.Action {
365
372
ResourceWorkspaceDormant .Type : {policy .ActionRead , policy .ActionDelete , policy .ActionCreate , policy .ActionUpdate , policy .ActionWorkspaceStop },
366
373
ResourceWorkspace .Type : slice .Omit (ResourceWorkspace .AvailableActions (), policy .ActionApplicationConnect , policy .ActionSSH ),
367
374
})... ),
@@ -409,32 +416,35 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
409
416
// map[actor_role][assign_role]<can_assign>
410
417
var assignRoles = map [string ]map [string ]bool {
411
418
"system" : {
412
- owner : true ,
413
- auditor : true ,
414
- member : true ,
415
- orgAdmin : true ,
416
- orgMember : true ,
417
- templateAdmin : true ,
418
- userAdmin : true ,
419
- customSiteRole : true ,
419
+ owner : true ,
420
+ auditor : true ,
421
+ member : true ,
422
+ orgAdmin : true ,
423
+ orgMember : true ,
424
+ templateAdmin : true ,
425
+ userAdmin : true ,
426
+ customSiteRole : true ,
427
+ customOrganizationRole : true ,
420
428
},
421
429
owner : {
422
- owner : true ,
423
- auditor : true ,
424
- member : true ,
425
- orgAdmin : true ,
426
- orgMember : true ,
427
- templateAdmin : true ,
428
- userAdmin : true ,
429
- customSiteRole : true ,
430
+ owner : true ,
431
+ auditor : true ,
432
+ member : true ,
433
+ orgAdmin : true ,
434
+ orgMember : true ,
435
+ templateAdmin : true ,
436
+ userAdmin : true ,
437
+ customSiteRole : true ,
438
+ customOrganizationRole : true ,
430
439
},
431
440
userAdmin : {
432
441
member : true ,
433
442
orgMember : true ,
434
443
},
435
444
orgAdmin : {
436
- orgAdmin : true ,
437
- orgMember : true ,
445
+ orgAdmin : true ,
446
+ orgMember : true ,
447
+ customOrganizationRole : true ,
438
448
},
439
449
}
440
450
@@ -596,6 +606,13 @@ func RoleByName(name RoleIdentifier) (Role, error) {
596
606
return Role {}, xerrors .Errorf ("expect a org id for role %q" , name .String ())
597
607
}
598
608
609
+ // This can happen if a custom role shares the same name as a built-in role.
610
+ // You could make an org role called "owner", and we should not return the
611
+ // owner role itself.
612
+ if name .OrganizationID != role .Identifier .OrganizationID {
613
+ return Role {}, xerrors .Errorf ("role %q not found" , name .String ())
614
+ }
615
+
599
616
return role , nil
600
617
}
601
618
0 commit comments