Skip to content

Commit 55ed653

Browse files
committed
fix: fix permissions for workspace creation
1 parent b60934b commit 55ed653

File tree

13 files changed

+53
-18
lines changed

13 files changed

+53
-18
lines changed

site/src/api/queries/organizations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ export const organizationsPermissions = (
306306

307307
export const workspacePermissionsByOrganization = (
308308
organizationIds: string[] | undefined,
309+
userId: string,
309310
) => {
310311
if (!organizationIds) {
311312
return { enabled: false };
@@ -315,7 +316,7 @@ export const workspacePermissionsByOrganization = (
315316
queryKey: ["workspaces", organizationIds.sort(), "permissions"],
316317
queryFn: async () => {
317318
const prefixedChecks = organizationIds.flatMap((orgId) =>
318-
Object.entries(workspacePermissionChecks(orgId)).map(([key, val]) => [
319+
Object.entries(workspacePermissionChecks(orgId, userId)).map(([key, val]) => [
319320
`${orgId}.${key}`,
320321
val,
321322
]),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use client"
2+
3+
import * as React from "react"
4+
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
5+
import { Check } from "lucide-react"
6+
7+
import { cn } from "utils/cn"
8+
9+
const Checkbox = React.forwardRef<
10+
React.ElementRef<typeof CheckboxPrimitive.Root>,
11+
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
12+
>(({ className, ...props }, ref) => (
13+
<CheckboxPrimitive.Root
14+
ref={ref}
15+
className={cn(
16+
"peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
17+
className
18+
)}
19+
{...props}
20+
>
21+
<CheckboxPrimitive.Indicator
22+
className={cn("flex items-center justify-center text-current")}
23+
>
24+
<Check className="h-4 w-4" />
25+
</CheckboxPrimitive.Indicator>
26+
</CheckboxPrimitive.Root>
27+
))
28+
Checkbox.displayName = CheckboxPrimitive.Root.displayName
29+
30+
export { Checkbox }

site/src/modules/permissions/workspaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export const workspacePermissionChecks = (organizationId: string) =>
1+
export const workspacePermissionChecks = (organizationId: string, userId: string) =>
22
({
3-
createWorkspaceForUser: {
3+
createWorkspace: {
44
object: {
55
resource_type: "workspace",
66
organization_id: organizationId,
7-
owner_id: "*",
7+
owner_id: userId,
88
},
99
action: "create",
1010
},

site/src/pages/CreateWorkspacePage/CreateWorkspacePage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const CreateWorkspacePage: FC = () => {
6767
const permissionsQuery = useQuery(
6868
templateQuery.data
6969
? checkAuthorization({
70-
checks: workspacePermissionChecks(templateQuery.data.organization_id),
70+
checks: workspacePermissionChecks(templateQuery.data.organization_id, me.id),
7171
})
7272
: { enabled: false },
7373
);

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const meta: Meta<typeof CreateWorkspacePageView> = {
2727
hasAllRequiredExternalAuth: true,
2828
mode: "form",
2929
permissions: {
30-
createWorkspaceForUser: true,
30+
createWorkspace: true,
3131
},
3232
onCancel: action("onCancel"),
3333
},

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
256256
<FormSection
257257
title="General"
258258
description={
259-
permissions.createWorkspaceForUser
259+
permissions.createWorkspace
260260
? "The name of the workspace and its owner. Only admins can create workspaces for other users."
261261
: "The name of your new workspace."
262262
}
@@ -301,7 +301,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
301301
</FormHelperText>
302302
</div>
303303

304-
{permissions.createWorkspaceForUser && (
304+
{permissions.createWorkspace && (
305305
<UserAutocomplete
306306
value={owner}
307307
onChange={(user) => {

site/src/pages/TemplatePage/TemplateLayout.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Loader } from "components/Loader/Loader";
66
import { Margins } from "components/Margins/Margins";
77
import { TabLink, Tabs, TabsList } from "components/Tabs/Tabs";
88
import { workspacePermissionChecks } from "modules/permissions/workspaces";
9+
import { useAuthenticated } from "contexts/auth/RequireAuth";
910
import {
1011
type FC,
1112
type PropsWithChildren,
@@ -73,6 +74,7 @@ export const TemplateLayout: FC<PropsWithChildren> = ({
7374
children = <Outlet />,
7475
}) => {
7576
const navigate = useNavigate();
77+
const { user: me } = useAuthenticated();
7678
const { organization: organizationName = "default", template: templateName } =
7779
useParams() as { organization?: string; template: string };
7880
const { data, error, isLoading } = useQuery({
@@ -81,7 +83,7 @@ export const TemplateLayout: FC<PropsWithChildren> = ({
8183
});
8284
const workspacePermissionsQuery = useQuery(
8385
checkAuthorization({
84-
checks: workspacePermissionChecks(organizationName),
86+
checks: workspacePermissionChecks(organizationName, me.id),
8587
}),
8688
);
8789

site/src/pages/TemplatePage/TemplatePageHeader.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const meta: Meta<typeof TemplatePageHeader> = {
1414
canUpdateTemplate: true,
1515
},
1616
workspacePermissions: {
17-
createWorkspaceForUser: true,
17+
createWorkspace: true,
1818
},
1919
},
2020
};
@@ -35,7 +35,7 @@ export const CanNotUpdate: Story = {
3535
export const CannotCreateWorkspace: Story = {
3636
args: {
3737
workspacePermissions: {
38-
createWorkspaceForUser: false,
38+
createWorkspace: false,
3939
},
4040
},
4141
};

site/src/pages/TemplatePage/TemplatePageHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export const TemplatePageHeader: FC<TemplatePageHeaderProps> = ({
180180
actions={
181181
<>
182182
{!template.deprecated &&
183-
workspacePermissions.createWorkspaceForUser && (
183+
workspacePermissions.createWorkspace && (
184184
<Button
185185
variant="contained"
186186
startIcon={<AddIcon />}

site/src/pages/TemplatesPage/TemplatesPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { pageTitle } from "utils/page";
1111
import { TemplatesPageView } from "./TemplatesPageView";
1212

1313
export const TemplatesPage: FC = () => {
14-
const { permissions } = useAuthenticated();
14+
const { permissions, user: me } = useAuthenticated();
1515
const { showOrganizations } = useDashboard();
1616

1717
const searchParamsResult = useSearchParams();
@@ -30,6 +30,7 @@ export const TemplatesPage: FC = () => {
3030
const workspacePermissionsQuery = useQuery(
3131
workspacePermissionsByOrganization(
3232
templatesQuery.data?.map((template) => template.organization_id),
33+
me.id,
3334
),
3435
);
3536

site/src/pages/TemplatesPage/TemplatesPageView.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const WithTemplates: Story = {
7676
examples: [],
7777
workspacePermissions: {
7878
[MockTemplate.organization_id]: {
79-
createWorkspaceForUser: true,
79+
createWorkspace: true,
8080
},
8181
},
8282
},
@@ -94,7 +94,7 @@ export const CannotCreateWorkspaces: Story = {
9494
...WithTemplates.args,
9595
workspacePermissions: {
9696
[MockTemplate.organization_id]: {
97-
createWorkspaceForUser: false,
97+
createWorkspace: false,
9898
},
9999
},
100100
},

site/src/pages/TemplatesPage/TemplatesPageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ const TemplateRow: FC<TemplateRowProps> = ({
160160
{template.deprecated ? (
161161
<DeprecatedBadge />
162162
) : workspacePermissions?.[template.organization_id]
163-
?.createWorkspaceForUser ? (
163+
?.createWorkspace ? (
164164
<MuiButton
165165
size="small"
166166
css={styles.actionButton}

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ const WorkspacesPage: FC = () => {
4040
// each hook.
4141
const searchParamsResult = useSafeSearchParams();
4242
const pagination = usePagination({ searchParamsResult });
43-
const { permissions } = useAuthenticated();
43+
const { permissions, user: me } = useAuthenticated();
4444
const { entitlements } = useDashboard();
4545

4646
const templatesQuery = useQuery(templates());
4747

4848
const orgPermissionsQuery = useQuery(
4949
workspacePermissionsByOrganization(
5050
templatesQuery.data?.map((template) => template.organization_id),
51+
me.id,
5152
),
5253
);
5354

@@ -59,7 +60,7 @@ const WorkspacesPage: FC = () => {
5960

6061
return templatesQuery.data.filter((template) => {
6162
const orgPermission = orgPermissionsQuery.data[template.organization_id];
62-
return orgPermission?.createWorkspaceForUser;
63+
return orgPermission?.createWorkspace;
6364
});
6465
}, [templatesQuery.data, orgPermissionsQuery.data]);
6566

0 commit comments

Comments
 (0)