@@ -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 } }
@@ -307,6 +311,9 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
307
311
DisplayName : "User Admin" ,
308
312
Site : Permissions (map [string ][]policy.Action {
309
313
ResourceAssignRole .Type : {policy .ActionAssign , policy .ActionDelete , policy .ActionRead },
314
+ // Need organization assign as well to create users. At present, creating a user
315
+ // will always assign them to some organization.
316
+ ResourceAssignOrgRole .Type : {policy .ActionAssign , policy .ActionDelete , policy .ActionRead },
310
317
ResourceUser .Type : {
311
318
policy .ActionCreate , policy .ActionRead , policy .ActionUpdate , policy .ActionDelete ,
312
319
policy .ActionUpdatePersonal , policy .ActionReadPersonal ,
@@ -354,7 +361,7 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
354
361
Site : []Permission {},
355
362
Org : map [string ][]Permission {
356
363
// Org admins should not have workspace exec perms.
357
- organizationID .String (): append (allPermsExcept (ResourceWorkspace , ResourceWorkspaceDormant ), Permissions (map [string ][]policy.Action {
364
+ organizationID .String (): append (allPermsExcept (ResourceWorkspace , ResourceWorkspaceDormant , ResourceAssignRole ), Permissions (map [string ][]policy.Action {
358
365
ResourceWorkspaceDormant .Type : {policy .ActionRead , policy .ActionDelete , policy .ActionCreate , policy .ActionUpdate , policy .ActionWorkspaceStop },
359
366
ResourceWorkspace .Type : slice .Omit (ResourceWorkspace .AvailableActions (), policy .ActionApplicationConnect , policy .ActionSSH ),
360
367
})... ),
@@ -402,32 +409,35 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
402
409
// map[actor_role][assign_role]<can_assign>
403
410
var assignRoles = map [string ]map [string ]bool {
404
411
"system" : {
405
- owner : true ,
406
- auditor : true ,
407
- member : true ,
408
- orgAdmin : true ,
409
- orgMember : true ,
410
- templateAdmin : true ,
411
- userAdmin : true ,
412
- customSiteRole : true ,
412
+ owner : true ,
413
+ auditor : true ,
414
+ member : true ,
415
+ orgAdmin : true ,
416
+ orgMember : true ,
417
+ templateAdmin : true ,
418
+ userAdmin : true ,
419
+ customSiteRole : true ,
420
+ customOrganizationRole : true ,
413
421
},
414
422
owner : {
415
- owner : true ,
416
- auditor : true ,
417
- member : true ,
418
- orgAdmin : true ,
419
- orgMember : true ,
420
- templateAdmin : true ,
421
- userAdmin : true ,
422
- customSiteRole : true ,
423
+ owner : true ,
424
+ auditor : true ,
425
+ member : true ,
426
+ orgAdmin : true ,
427
+ orgMember : true ,
428
+ templateAdmin : true ,
429
+ userAdmin : true ,
430
+ customSiteRole : true ,
431
+ customOrganizationRole : true ,
423
432
},
424
433
userAdmin : {
425
434
member : true ,
426
435
orgMember : true ,
427
436
},
428
437
orgAdmin : {
429
- orgAdmin : true ,
430
- orgMember : true ,
438
+ orgAdmin : true ,
439
+ orgMember : true ,
440
+ customOrganizationRole : true ,
431
441
},
432
442
}
433
443
@@ -589,6 +599,13 @@ func RoleByName(name RoleIdentifier) (Role, error) {
589
599
return Role {}, xerrors .Errorf ("expect a org id for role %q" , name .String ())
590
600
}
591
601
602
+ // This can happen if a custom role shares the same name as a built-in role.
603
+ // You could make an org role called "owner", and we should not return the
604
+ // owner role itself.
605
+ if name .OrganizationID != role .Identifier .OrganizationID {
606
+ return Role {}, xerrors .Errorf ("role %q not found" , name .String ())
607
+ }
608
+
592
609
return role , nil
593
610
}
594
611
0 commit comments