@@ -55,6 +55,7 @@ import (
55
55
"github.com/coder/coder/v2/coderd/autobuild"
56
56
"github.com/coder/coder/v2/coderd/awsidentity"
57
57
"github.com/coder/coder/v2/coderd/database"
58
+ "github.com/coder/coder/v2/coderd/database/db2sdk"
58
59
"github.com/coder/coder/v2/coderd/database/dbauthz"
59
60
"github.com/coder/coder/v2/coderd/database/dbrollup"
60
61
"github.com/coder/coder/v2/coderd/database/dbtestutil"
@@ -663,21 +664,25 @@ func CreateFirstUser(t testing.TB, client *codersdk.Client) codersdk.CreateFirst
663
664
664
665
// CreateAnotherUser creates and authenticates a new user.
665
666
// Roles can include org scoped roles with 'roleName:<organization_id>'
666
- func CreateAnotherUser (t testing.TB ,client * codersdk.Client ,organizationID uuid.UUID ,roles ... string ) (* codersdk.Client , codersdk.User ) {
667
+ func CreateAnotherUser (t testing.TB ,client * codersdk.Client ,organizationID uuid.UUID ,roles ... rbac. RoleIdentifier ) (* codersdk.Client , codersdk.User ) {
667
668
return createAnotherUserRetry (t ,client ,organizationID ,5 ,roles )
668
669
}
669
670
670
- func CreateAnotherUserMutators (t testing.TB ,client * codersdk.Client ,organizationID uuid.UUID ,roles []string ,mutators ... func (r * codersdk.CreateUserRequest )) (* codersdk.Client , codersdk.User ) {
671
+ func CreateAnotherUserMutators (t testing.TB ,client * codersdk.Client ,organizationID uuid.UUID ,roles []rbac. RoleIdentifier ,mutators ... func (r * codersdk.CreateUserRequest )) (* codersdk.Client , codersdk.User ) {
671
672
return createAnotherUserRetry (t ,client ,organizationID ,5 ,roles ,mutators ... )
672
673
}
673
674
674
675
// AuthzUserSubject does not include the user's groups.
675
676
func AuthzUserSubject (user codersdk.User ,orgID uuid.UUID ) rbac.Subject {
676
- roles := make (rbac.RoleNames ,0 ,len (user .Roles ))
677
+ roles := make (rbac.RoleIdentifiers ,0 ,len (user .Roles ))
677
678
// Member role is always implied
678
679
roles = append (roles ,rbac .RoleMember ())
679
680
for _ ,r := range user .Roles {
680
- roles = append (roles ,r .Name )
681
+ orgID ,_ := uuid .Parse (r .OrganizationID )// defaults to nil
682
+ roles = append (roles , rbac.RoleIdentifier {
683
+ Name :r .Name ,
684
+ OrganizationID :orgID ,
685
+ })
681
686
}
682
687
// We assume only 1 org exists
683
688
roles = append (roles ,rbac .ScopedRoleOrgMember (orgID ))
@@ -690,7 +695,7 @@ func AuthzUserSubject(user codersdk.User, orgID uuid.UUID) rbac.Subject {
690
695
}
691
696
}
692
697
693
- func createAnotherUserRetry (t testing.TB ,client * codersdk.Client ,organizationID uuid.UUID ,retries int ,roles []string ,mutators ... func (r * codersdk.CreateUserRequest )) (* codersdk.Client , codersdk.User ) {
698
+ func createAnotherUserRetry (t testing.TB ,client * codersdk.Client ,organizationID uuid.UUID ,retries int ,roles []rbac. RoleIdentifier ,mutators ... func (r * codersdk.CreateUserRequest )) (* codersdk.Client , codersdk.User ) {
694
699
req := codersdk.CreateUserRequest {
695
700
Email :namesgenerator .GetRandomName (10 )+ "@coder.com" ,
696
701
Username :RandomUsername (t ),
@@ -748,36 +753,37 @@ func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationI
748
753
749
754
if len (roles )> 0 {
750
755
// Find the roles for the org vs the site wide roles
751
- orgRoles := make (map [string ][]string )
752
- var siteRoles []string
756
+ orgRoles := make (map [uuid. UUID ][]rbac. RoleIdentifier )
757
+ var siteRoles []rbac. RoleIdentifier
753
758
754
759
for _ ,roleName := range roles {
755
- roleName := roleName
756
- orgID ,ok := rbac .IsOrgRole (roleName )
757
- roleName ,_ ,err = rbac .RoleSplit (roleName )
758
- require .NoError (t ,err ,"split org role name" )
760
+ ok := roleName .IsOrgRole ()
759
761
if ok {
760
- roleName ,_ ,err = rbac .RoleSplit (roleName )
761
- require .NoError (t ,err ,"split rolename" )
762
- orgRoles [orgID ]= append (orgRoles [orgID ],roleName )
762
+ orgRoles [roleName .OrganizationID ]= append (orgRoles [roleName .OrganizationID ],roleName )
763
763
}else {
764
764
siteRoles = append (siteRoles ,roleName )
765
765
}
766
766
}
767
767
// Update the roles
768
768
for _ ,r := range user .Roles {
769
- siteRoles = append (siteRoles ,r .Name )
769
+ orgID ,_ := uuid .Parse (r .OrganizationID )
770
+ siteRoles = append (siteRoles , rbac.RoleIdentifier {
771
+ Name :r .Name ,
772
+ OrganizationID :orgID ,
773
+ })
774
+ }
775
+
776
+ onlyName := func (role rbac.RoleIdentifier )string {
777
+ return role .Name
770
778
}
771
779
772
- user ,err = client .UpdateUserRoles (context .Background (),user .ID .String (), codersdk.UpdateRoles {Roles :siteRoles })
780
+ user ,err = client .UpdateUserRoles (context .Background (),user .ID .String (), codersdk.UpdateRoles {Roles :db2sdk . List ( siteRoles , onlyName ) })
773
781
require .NoError (t ,err ,"update site roles" )
774
782
775
783
// Update org roles
776
784
for orgID ,roles := range orgRoles {
777
- organizationID ,err := uuid .Parse (orgID )
778
- require .NoError (t ,err ,fmt .Sprintf ("parse org id %q" ,orgID ))
779
- _ ,err = client .UpdateOrganizationMemberRoles (context .Background (),organizationID ,user .ID .String (),
780
- codersdk.UpdateRoles {Roles :roles })
785
+ _ ,err = client .UpdateOrganizationMemberRoles (context .Background (),orgID ,user .ID .String (),
786
+ codersdk.UpdateRoles {Roles :db2sdk .List (roles ,onlyName )})
781
787
require .NoError (t ,err ,"update org membership roles" )
782
788
}
783
789
}