Skip to content

Commit 25a5c89

Browse files
committed
🧹
1 parent bf757e1 commit 25a5c89

File tree

6 files changed

+29
-49
lines changed

6 files changed

+29
-49
lines changed

coderd/database/db2sdk/db2sdk.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/coder/coder/v2/coderd/database"
1919
"github.com/coder/coder/v2/coderd/rbac"
20+
"github.com/coder/coder/v2/coderd/rbac/rolestore"
2021
"github.com/coder/coder/v2/coderd/render"
2122
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
2223
"github.com/coder/coder/v2/codersdk"
@@ -166,24 +167,7 @@ func User(user database.User, organizationIDs []uuid.UUID) codersdk.User {
166167
convertedUser := codersdk.User{
167168
ReducedUser: ReducedUser(user),
168169
OrganizationIDs: organizationIDs,
169-
Roles: make([]codersdk.SlimRole, len(user.RBACRoles)),
170-
}
171-
172-
for i, roleName := range user.RBACRoles {
173-
// TODO: Currently the api only returns site wide roles.
174-
// Should it return organization roles?
175-
rbacRole, err := rbac.RoleByName(rbac.RoleIdentifier{
176-
Name: roleName,
177-
OrganizationID: uuid.Nil,
178-
})
179-
180-
if err == nil {
181-
convertedUser.Roles[i] = SlimRole(rbacRole)
182-
} else {
183-
// TODO: Fix this for custom roles to display the actual display_name
184-
// Requires plumbing either a cached role value, or the db.
185-
convertedUser.Roles[i] = codersdk.SlimRole{Name: roleName}
186-
}
170+
Roles: rolestore.ExpandFromGlobalNamesToSlimRole(user.RBACRoles),
187171
}
188172

189173
return convertedUser

coderd/members.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/coder/coder/v2/coderd/httpapi"
1515
"github.com/coder/coder/v2/coderd/httpmw"
1616
"github.com/coder/coder/v2/coderd/rbac"
17+
"github.com/coder/coder/v2/coderd/rbac/rolestore"
1718
"github.com/coder/coder/v2/codersdk"
1819
)
1920

@@ -312,34 +313,10 @@ func convertOrganizationMembersWithUserData(ctx context.Context, db database.Sto
312313
Username: rows[i].Username,
313314
AvatarURL: rows[i].AvatarURL,
314315
Name: rows[i].Name,
315-
GlobalRoles: convertRbacRoleNamesToSlimRoles(rows[i].GlobalRoles),
316+
GlobalRoles: rolestore.ExpandFromGlobalNamesToSlimRole(rows[i].GlobalRoles),
316317
OrganizationMember: convertedMembers[i],
317318
})
318319
}
319320

320321
return converted, nil
321322
}
322-
323-
// Taken from `func db2sdk.User`. We should probably deduplicate this.
324-
func convertRbacRoleNamesToSlimRoles(names []string) []codersdk.SlimRole {
325-
convertedRoles := make([]codersdk.SlimRole, len(names))
326-
327-
for i, roleName := range names {
328-
// TODO: Currently the api only returns site wide roles.
329-
// Should it return organization roles?
330-
rbacRole, err := rbac.RoleByName(rbac.RoleIdentifier{
331-
Name: roleName,
332-
OrganizationID: uuid.Nil,
333-
})
334-
335-
if err == nil {
336-
convertedRoles[i] = db2sdk.SlimRole(rbacRole)
337-
} else {
338-
// TODO: Fix this for custom roles to display the actual display_name
339-
// Requires plumbing either a cached role value, or the db.
340-
convertedRoles[i] = codersdk.SlimRole{Name: roleName}
341-
}
342-
}
343-
344-
return convertedRoles
345-
}

coderd/members_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ func TestRemoveMember(t *testing.T) {
185185
})
186186
}
187187

188-
func onlyIDs(u codersdk.OrganizationMemberWithName) uuid.UUID {
188+
func onlyIDs(u codersdk.OrganizationMemberWithUserData) uuid.UUID {
189189
return u.UserID
190190
}

coderd/notifications/manager_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/coder/serpent"
1011
"github.com/google/uuid"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
"golang.org/x/xerrors"
1415

15-
"github.com/coder/serpent"
16-
1716
"cdr.dev/slog"
1817
"cdr.dev/slog/sloggers/slogtest"
1918

coderd/rbac/rolestore/rolestore.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"golang.org/x/xerrors"
99

1010
"github.com/coder/coder/v2/coderd/database"
11+
"github.com/coder/coder/v2/coderd/database/db2sdk"
1112
"github.com/coder/coder/v2/coderd/rbac"
1213
"github.com/coder/coder/v2/coderd/util/syncmap"
14+
"github.com/coder/coder/v2/codersdk"
1315
)
1416

1517
type customRoleCtxKey struct{}
@@ -38,6 +40,24 @@ func roleCache(ctx context.Context) *syncmap.Map[string, rbac.Role] {
3840
return c
3941
}
4042

43+
func ExpandFromGlobalNamesToSlimRole(names []string) []codersdk.SlimRole {
44+
convertedRoles := make([]codersdk.SlimRole, 0, len(names))
45+
46+
for _, roleName := range names {
47+
// TODO: Currently the api only returns site wide roles.
48+
// Should it return organization roles?
49+
rbacRole, err := rbac.RoleByName(rbac.RoleIdentifier{Name: roleName})
50+
51+
if err == nil {
52+
convertedRoles = append(convertedRoles, db2sdk.SlimRole(rbacRole))
53+
} else {
54+
convertedRoles = append(convertedRoles, codersdk.SlimRole{Name: roleName})
55+
}
56+
}
57+
58+
return convertedRoles
59+
}
60+
4161
// Expand will expand built in roles, and fetch custom roles from the database.
4262
// If a custom role is defined, but does not exist, the role will be omitted on
4363
// the response. This means deleted roles are silently dropped.

site/src/pages/ManagementSettingsPage/OrganizationMembersPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const OrganizationMembersPage: FC = () => {
114114
}}
115115
>
116116
{role.global ? (
117-
<Tooltip title="This user has this role for all organziations.">
117+
<Tooltip title="This user has this role for all organizations.">
118118
<span>{role.name}*</span>
119119
</Tooltip>
120120
) : (
@@ -144,7 +144,7 @@ const OrganizationMembersPage: FC = () => {
144144
await removeMemberMutation.mutateAsync(
145145
member.user_id,
146146
);
147-
membersQuery.refetch();
147+
void membersQuery.refetch();
148148
}}
149149
danger
150150
>
@@ -189,7 +189,7 @@ export default OrganizationMembersPage;
189189

190190
interface AddGroupMemberProps {
191191
isLoading: boolean;
192-
onSubmit: (user: User) => void;
192+
onSubmit: (user: User) => Promise<void>;
193193
}
194194

195195
const AddGroupMember: FC<AddGroupMemberProps> = ({ isLoading, onSubmit }) => {

0 commit comments

Comments
 (0)