-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] add permissions support in workspaces (#209)
- Loading branch information
Showing
17 changed files
with
187 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { getPermissions, maxPermissionNumber } from './getPermissions' | ||
|
||
describe('getPermissions', () => { | ||
it('should return an empty array for zero', () => { | ||
expect(getPermissions(0)).toEqual([]) | ||
}) | ||
|
||
it('should return correct permissions for a single flag', () => { | ||
expect(getPermissions(1)).toEqual(['create']) // 1 << 0 | ||
expect(getPermissions(2)).toEqual(['read']) // 1 << 1 | ||
expect(getPermissions(8)).toEqual(['delete']) // 1 << 3 | ||
}) | ||
|
||
it('should return multiple permissions for combined flags', () => { | ||
const combined = 13 // 1 << 0 | 1 << 2 | 1 << 3 | ||
expect(getPermissions(combined)).toEqual(['create', 'update', 'delete']) | ||
}) | ||
|
||
it('should handle all flags combined', () => { | ||
expect(getPermissions(maxPermissionNumber).length).toBe(13) | ||
expect(getPermissions(maxPermissionNumber)).toContain('create') | ||
expect(getPermissions(maxPermissionNumber)).toContain('updateRoles') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const workspacePermissions = { | ||
create: 1 << 0, // Not currently used. | ||
read: 1 << 1, // Required to read any content in the workspace; without this, the workspace is effectively disabled. | ||
update: 1 << 2, // Update general workspace properties; not directly related to deletion or invitations. | ||
delete: 1 << 3, // Not currently used. | ||
inviteTo: 1 << 4, // Invite new members to the workspace. | ||
removeFrom: 1 << 5, // Remove existing members from the workspace. | ||
readSettings: 1 << 6, // Access to view workspace settings; necessary to display settings UI. | ||
updateSettings: 1 << 7, // Modify workspace settings. | ||
updateCloudAccounts: 1 << 8, // Manage cloud account integrations within the workspace. | ||
readBilling: 1 << 9, // View billing and subscription details. | ||
updateBilling: 1 << 10, // Modify billing and payment methods. | ||
readRoles: 1 << 11, // View roles of workspace members. | ||
updateRoles: 1 << 12, // Change roles of workspace members. | ||
} as const | ||
|
||
export const maxPermissionNumber = (1 << 13) - 1 | ||
|
||
export type Permissions = keyof typeof workspacePermissions | ||
|
||
export const getPermissions = (value: number) => | ||
Object.entries(workspacePermissions).reduce( | ||
(prev, [permKey, permValue]) => ((value & permValue) === permValue ? [...prev, permKey] : prev), | ||
[] as Permissions[], | ||
) | ||
|
||
export const allPermissions = getPermissions(maxPermissionNumber) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import { AxiosInstance } from 'axios' | ||
import { endPoints } from 'src/shared/constants' | ||
import { GetWorkspacesResponse } from 'src/shared/types/server' | ||
import { getPermissions } from './getPermissions' | ||
|
||
export const getWorkspacesQuery = async (axios: AxiosInstance) => { | ||
return axios.get<GetWorkspacesResponse>(endPoints.workspaces.self).then((res) => res.data) | ||
return axios | ||
.get<GetWorkspacesResponse>(endPoints.workspaces.self) | ||
.then((res) => res.data?.map((workspace) => ({ ...workspace, permissions: getPermissions(workspace.user_permissions) }))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export { AuthGuard } from './AuthGuard' | ||
export { RequireAuth } from './RequireAuth' | ||
export type { UserContextRealValues, UserContextValue } from './UserContext' | ||
export type { UserContextRealValues, UserContextValue, UserContextWorkspace } from './UserContext' | ||
export { getCurrentUserQuery } from './getCurrentUser.query' | ||
export { allPermissions, getPermissions, maxPermissionNumber, workspacePermissions } from './getPermissions' | ||
export type { Permissions } from './getPermissions' | ||
export { useUserProfile } from './useUserProfile' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.