Skip to content

Commit 1c3ef56

Browse files
committed
chore: Extract non-hook versions of several permissions
https://harperdb.atlassian.net/browse/STUDIO-583
1 parent 61c109c commit 1c3ef56

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

src/hooks/usePermissions.ts

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@ import {
55
LocalRoleAttributePermissionAction,
66
LocalRolePermissionAction,
77
LocalRolePermissionTable,
8+
User,
89
} from '@/lib/api.patch';
910
import { useParams } from '@tanstack/react-router';
1011

11-
export function useOrganizationPermissions(orgId?: string): { update: boolean; remove: boolean; } {
12+
interface UR {
13+
update: boolean;
14+
remove: boolean;
15+
}
16+
17+
interface CRUV {
18+
create: boolean;
19+
remove: boolean;
20+
update: boolean;
21+
view: boolean;
22+
}
23+
24+
export function useOrganizationPermissions(orgId?: string): UR {
1225
const { user } = useCloudAuth();
1326
const { organizationId: orgIdFromRoute }: { organizationId: string; } = useParams({ strict: false });
1427
const role = user?.roles?.[orgId ?? orgIdFromRoute];
@@ -21,12 +34,7 @@ export function useOrganizationPermissions(orgId?: string): { update: boolean; r
2134
return { update: role.organization.update, remove: role.organization.delete };
2235
}
2336

24-
export function useOrganizationRolePermissions(orgId?: string): {
25-
create: boolean;
26-
remove: boolean;
27-
update: boolean;
28-
view: boolean;
29-
} {
37+
export function useOrganizationRolePermissions(orgId?: string): CRUV {
3038
const { user } = useCloudAuth();
3139
const { organizationId: orgIdFromRoute }: { organizationId: string; } = useParams({ strict: false });
3240
const role = user?.roles?.[orgId ?? orgIdFromRoute];
@@ -40,26 +48,29 @@ export function useOrganizationRolePermissions(orgId?: string): {
4048
return { create: roles.create, remove: roles.delete, update: roles.update, view: roles.view };
4149
}
4250

43-
export function useOrganizationClusterPermissions(orgId?: string, clusterId?: string): {
44-
create: boolean;
45-
remove: boolean;
46-
update: boolean;
47-
view: boolean;
48-
} {
51+
export function useOrganizationClusterPermissions(orgId?: string, clusterId?: string): CRUV {
4952
const { user } = useCloudAuth();
5053
const { organizationId: orgIdFromRoute, clusterId: clusterIdFromRoute }: {
5154
organizationId: string;
5255
clusterId: string;
5356
} = useParams({ strict: false });
54-
const findClusterId = clusterId ?? clusterIdFromRoute;
55-
const role = user?.roles?.[orgId ?? orgIdFromRoute];
57+
58+
return getOrganizationClusterPermissions(
59+
user,
60+
orgId ?? orgIdFromRoute,
61+
clusterId ?? clusterIdFromRoute,
62+
);
63+
}
64+
65+
export function getOrganizationClusterPermissions(user: User | null, orgId: string, clusterId: string): CRUV {
66+
const role = user?.roles?.[orgId];
5667
if (!role?.permission && !role?.organization?.clusters) {
5768
return { create: false, remove: false, update: false, view: false };
5869
}
5970
if (role.permission?.super_user) {
6071
return { create: true, remove: true, update: true, view: true };
6172
}
62-
const specificRoles = role.organization.clusters.resources?.find(r => r.id === findClusterId);
73+
const specificRoles = role.organization.clusters.resources?.find(r => r.id === clusterId);
6374
const genericRoles = role.organization.clusters;
6475
return {
6576
create: genericRoles.create,
@@ -69,27 +80,30 @@ export function useOrganizationClusterPermissions(orgId?: string, clusterId?: st
6980
};
7081
}
7182

72-
export function useOrganizationClusterInstancePermissions(orgId?: string, clusterId?: string): {
73-
create: boolean;
74-
remove: boolean;
75-
update: boolean;
76-
view: boolean;
77-
} {
83+
export function useOrganizationClusterInstancePermissions(orgId?: string, clusterId?: string): CRUV {
7884
const { user } = useCloudAuth();
7985
const { organizationId: orgIdFromRoute, clusterId: clusterIdFromRoute }: {
8086
organizationId: string;
8187
clusterId: string;
8288
} = useParams({ strict: false });
83-
const findClusterId = clusterId ?? clusterIdFromRoute;
84-
const role = user?.roles?.[orgId ?? orgIdFromRoute];
89+
90+
return getOrganizationClusterInstancePermissions(
91+
user,
92+
orgId ?? orgIdFromRoute,
93+
clusterId ?? clusterIdFromRoute,
94+
);
95+
}
96+
97+
export function getOrganizationClusterInstancePermissions(user: User | null, orgId: string, clusterId: string): CRUV {
98+
const role = user?.roles?.[orgId];
8599
if (!role?.permission && !role?.organization?.clusters) {
86100
return { create: false, remove: false, update: false, view: false };
87101
}
88102
if (role.permission?.super_user) {
89103
return { create: true, remove: true, update: true, view: true };
90104
}
91105
const specificRoles = role.organization.clusters
92-
.resources?.find(r => r.id === findClusterId)
106+
.resources?.find(r => r.id === clusterId)
93107
?.instances;
94108
const genericRoles = role.organization.clusters;
95109
return {

0 commit comments

Comments
 (0)