Skip to content

feat(site): edit organization member roles #13977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coderd/database/queries/organizationmembers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- - Use both to get a specific org member row
SELECT
sqlc.embed(organization_members),
users.username, users.avatar_url, users.name, users.rbac_roles as "global_roles"
users.username, users.avatar_url, users.name, users.email, users.rbac_roles as "global_roles"
FROM
organization_members
INNER JOIN
Expand Down
1 change: 1 addition & 0 deletions coderd/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func convertOrganizationMembersWithUserData(ctx context.Context, db database.Sto
Username: rows[i].Username,
AvatarURL: rows[i].AvatarURL,
Name: rows[i].Name,
Email: rows[i].Email,
GlobalRoles: db2sdk.SlimRolesFromNames(rows[i].GlobalRoles),
OrganizationMember: convertedMembers[i],
})
Expand Down
1 change: 1 addition & 0 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type OrganizationMemberWithUserData struct {
Username string `table:"username,default_sort" json:"username"`
Name string `table:"name" json:"name"`
AvatarURL string `json:"avatar_url"`
Email string `json:"email"`
GlobalRoles []SlimRole `json:"global_roles"`
OrganizationMember `table:"m,recursive_inline"`
}
Expand Down
2 changes: 2 additions & 0 deletions docs/api/members.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/api/schemas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,27 @@ class ApiMethods {
return response.data;
};

getOrganizationRoles = async (organizationId: string) => {
const response = await this.axios.get<TypesGen.AssignableRoles[]>(
`/api/v2/organizations/${organizationId}/members/roles`,
);

return response.data;
};

updateOrganizationMemberRoles = async (
organizationId: string,
userId: string,
roles: TypesGen.SlimRole["name"][],
): Promise<TypesGen.User> => {
const response = await this.axios.put<TypesGen.User>(
`/api/v2/organizations/${organizationId}/members/${userId}/roles`,
{ roles },
);

return response.data;
};

addOrganizationMember = async (organizationId: string, userId: string) => {
const response = await this.axios.post<TypesGen.OrganizationMember>(
`/api/v2/organizations/${organizationId}/members/${userId}`,
Expand Down
21 changes: 20 additions & 1 deletion site/src/api/queries/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const deleteOrganization = (queryClient: QueryClient) => {
export const organizationMembers = (id: string) => {
return {
queryFn: () => API.getOrganizationMembers(id),
key: ["organization", id, "members"],
queryKey: ["organization", id, "members"],
};
};

Expand Down Expand Up @@ -80,6 +80,25 @@ export const removeOrganizationMember = (
};
};

export const updateOrganizationMemberRoles = (
queryClient: QueryClient,
organizationId: string,
) => {
return {
mutationFn: ({ userId, roles }: { userId: string; roles: string[] }) => {
return API.updateOrganizationMemberRoles(organizationId, userId, roles);
},

onSuccess: async () => {
await queryClient.invalidateQueries([
"organization",
organizationId,
"members",
]);
},
};
};

export const organizationsKey = ["organizations"] as const;

export const organizations = () => {
Expand Down
7 changes: 7 additions & 0 deletions site/src/api/queries/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ export const roles = () => {
queryFn: API.getRoles,
};
};

export const organizationRoles = (organizationId: string) => {
return {
queryKey: ["organization", organizationId, "roles"],
queryFn: () => API.getOrganizationRoles(organizationId),
};
};
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 40 additions & 45 deletions site/src/pages/ManagementSettingsPage/OrganizationMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import Tooltip from "@mui/material/Tooltip";
import { type FC, useState } from "react";
import { useMutation, useQuery, useQueryClient } from "react-query";
import { useParams } from "react-router-dom";
Expand All @@ -16,11 +15,13 @@ import {
addOrganizationMember,
organizationMembers,
removeOrganizationMember,
updateOrganizationMemberRoles,
} from "api/queries/organizations";
import type { OrganizationMemberWithUserData, User } from "api/typesGenerated";
import { organizationRoles } from "api/queries/roles";
import type { User } from "api/typesGenerated";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { AvatarData } from "components/AvatarData/AvatarData";
import { displayError } from "components/GlobalSnackbar/utils";
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
import {
MoreMenu,
MoreMenuTrigger,
Expand All @@ -29,24 +30,30 @@ import {
ThreeDotsButton,
} from "components/MoreMenu/MoreMenu";
import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader";
import { Pill } from "components/Pill/Pill";
import { Stack } from "components/Stack/Stack";
import { UserAutocomplete } from "components/UserAutocomplete/UserAutocomplete";
import { UserAvatar } from "components/UserAvatar/UserAvatar";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { TableColumnHelpTooltip } from "./UserTable/TableColumnHelpTooltip";
import { UserRoleCell } from "./UserTable/UserRoleCell";

const OrganizationMembersPage: FC = () => {
const queryClient = useQueryClient();
const { organization } = useParams() as { organization: string };
const { user: me } = useAuthenticated();

const membersQuery = useQuery(organizationMembers(organization));
const organizationRolesQuery = useQuery(organizationRoles(organization));

const addMemberMutation = useMutation(
addOrganizationMember(queryClient, organization),
);
const removeMemberMutation = useMutation(
removeOrganizationMember(queryClient, organization),
);
const updateMemberRolesMutation = useMutation(
updateOrganizationMemberRoles(queryClient, organization),
);

const error =
membersQuery.error ?? addMemberMutation.error ?? removeMemberMutation.error;
Expand All @@ -61,7 +68,7 @@ const OrganizationMembersPage: FC = () => {
<Stack>
{Boolean(error) && <ErrorAlert error={error} />}

<AddGroupMember
<AddOrganizationMember
isLoading={addMemberMutation.isLoading}
onSubmit={async (user) => {
await addMemberMutation.mutateAsync(user.id);
Expand All @@ -74,7 +81,12 @@ const OrganizationMembersPage: FC = () => {
<TableHead>
<TableRow>
<TableCell width="50%">User</TableCell>
<TableCell width="49%">Roles</TableCell>
<TableCell width="49%">
<Stack direction="row" spacing={1} alignItems="center">
<span>Roles</span>
<TableColumnHelpTooltip variant="roles" />
</Stack>
</TableCell>
<TableCell width="1%"></TableCell>
</TableRow>
</TableHead>
Expand All @@ -89,26 +101,25 @@ const OrganizationMembersPage: FC = () => {
avatarURL={member.avatar_url}
/>
}
title={member.name}
subtitle={member.username}
title={member.name || member.username}
subtitle={member.email}
/>
</TableCell>
<TableCell>
{getMemberRoles(member).map((role) => (
<Pill
key={role.name}
css={role.global ? styles.globalRole : styles.role}
>
{role.global ? (
<Tooltip title="This user has this role for all organizations.">
<span>{role.name}*</span>
</Tooltip>
) : (
role.name
)}
</Pill>
))}
</TableCell>
<UserRoleCell
inheritedRoles={member.global_roles}
roles={member.roles}
allAvailableRoles={organizationRolesQuery.data}
oidcRoleSyncEnabled={false}
isLoading={organizationRolesQuery.isLoading}
canEditUsers
onEditRoles={async (newRoleNames) => {
await updateMemberRolesMutation.mutateAsync({
userId: member.user_id,
roles: newRoleNames,
});
displaySuccess("Roles updated successfully.");
}}
/>
<TableCell>
{member.user_id !== me.id && (
<MoreMenu>
Expand Down Expand Up @@ -141,33 +152,17 @@ const OrganizationMembersPage: FC = () => {
);
};

function getMemberRoles(member: OrganizationMemberWithUserData) {
const roles = new Map<string, { name: string; global?: boolean }>();

for (const role of member.global_roles) {
roles.set(role.name, {
name: role.display_name || role.name,
global: true,
});
}
for (const role of member.roles) {
if (roles.has(role.name)) {
continue;
}
roles.set(role.name, { name: role.display_name || role.name });
}

return [...roles.values()];
}

export default OrganizationMembersPage;

interface AddGroupMemberProps {
interface AddOrganizationMemberProps {
isLoading: boolean;
onSubmit: (user: User) => Promise<void>;
}

const AddGroupMember: FC<AddGroupMemberProps> = ({ isLoading, onSubmit }) => {
const AddOrganizationMember: FC<AddOrganizationMemberProps> = ({
isLoading,
onSubmit,
}) => {
const [selectedUser, setSelectedUser] = useState<User | null>(null);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface EditRolesButtonProps {
selectedRoleNames: Set<string>;
onChange: (roles: SlimRole["name"][]) => void;
oidcRoleSync: boolean;
userLoginType: string;
userLoginType?: string;
}

export const EditRolesButton: FC<EditRolesButtonProps> = ({
Expand Down
Loading
Loading