-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
usePermissions
UI hook (#413)
* move types and run license check * pulling in most recent main * added useAppSelector * ran license check * updated from PR feedback * rewrote hook and permission structure based on feedback * added check for single permission * persistent arg names * additional refactor * refactored permissions definitions * removed fake permission
- Loading branch information
Showing
5 changed files
with
382 additions
and
59 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
cmd/ui/src/hooks/usePermissions/usePermissions.test.tsx
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,101 @@ | ||
// Copyright 2024 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Permission, PERMISSIONS } from 'bh-shared-ui'; | ||
import { renderHook } from 'src/test-utils'; | ||
import usePermissions, { PermissionsFns } from './usePermissions'; | ||
|
||
describe('usePermissions', () => { | ||
const getPermissionsWithUser = (permissions: Permission[]): PermissionsFns => { | ||
return renderHook(() => usePermissions(), { | ||
initialState: { | ||
auth: { | ||
user: { | ||
roles: [ | ||
{ | ||
permissions: permissions.map((p) => PERMISSIONS[p]), | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}).result.current; | ||
}; | ||
|
||
const allPermissions = [ | ||
Permission.CLIENTS_MANAGE, | ||
Permission.AUTH_CREATE_TOKEN, | ||
Permission.APP_READ_APPLICATION_CONFIGURATION, | ||
]; | ||
|
||
it('passes check if the user has a required permission', () => { | ||
const permissions = getPermissionsWithUser([Permission.CLIENTS_MANAGE]); | ||
|
||
const has = permissions.checkPermission(Permission.CLIENTS_MANAGE); | ||
const hasAll = permissions.checkAllPermissions([Permission.CLIENTS_MANAGE]); | ||
const hasAtLeastOne = permissions.checkAtLeastOnePermission([Permission.CLIENTS_MANAGE]); | ||
|
||
expect(has).toBe(true); | ||
expect(hasAll).toBe(true); | ||
expect(hasAtLeastOne).toBe(true); | ||
}); | ||
|
||
it('passes checks if the user has multiple required permissions', () => { | ||
const permissions = getPermissionsWithUser(allPermissions); | ||
|
||
const hasAll = permissions.checkAllPermissions(allPermissions); | ||
const hasAtLeastOne = permissions.checkAtLeastOnePermission(allPermissions); | ||
|
||
expect(hasAll).toBe(true); | ||
expect(hasAtLeastOne).toBe(true); | ||
}); | ||
|
||
it('fails checks if the user does not have a matching permission', () => { | ||
const permissions = getPermissionsWithUser([Permission.CLIENTS_MANAGE]); | ||
|
||
const has = permissions.checkPermission(Permission.AUTH_CREATE_TOKEN); | ||
const hasAll = permissions.checkAllPermissions([Permission.AUTH_CREATE_TOKEN]); | ||
const hasAtLeastOne = permissions.checkAtLeastOnePermission([Permission.AUTH_CREATE_TOKEN]); | ||
|
||
expect(has).toBe(false); | ||
expect(hasAll).toBe(false); | ||
expect(hasAtLeastOne).toBe(false); | ||
}); | ||
|
||
it('passes the check for at least one permission if the user is missing one of many required permissions', () => { | ||
const permissions = getPermissionsWithUser([ | ||
Permission.APP_READ_APPLICATION_CONFIGURATION, | ||
Permission.AUTH_CREATE_TOKEN, | ||
]); | ||
|
||
const hasAll = permissions.checkAllPermissions(allPermissions); | ||
const hasAtLeastOne = permissions.checkAtLeastOnePermission(allPermissions); | ||
|
||
expect(hasAll).toBe(false); | ||
expect(hasAtLeastOne).toBe(true); | ||
}); | ||
|
||
it('returns a list of the users current permissions', () => { | ||
const permissions = getPermissionsWithUser(allPermissions); | ||
const userPermissionsResult = permissions.getUserPermissions(); | ||
|
||
expect(allPermissions.length).toEqual(userPermissionsResult.length); | ||
|
||
for (const permission of allPermissions) { | ||
expect(userPermissionsResult).toContain(permission); | ||
} | ||
}); | ||
}); |
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,74 @@ | ||
// Copyright 2024 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Permission, PERMISSIONS } from 'bh-shared-ui'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useAppSelector } from 'src/store'; | ||
|
||
export type PermissionsFns = { | ||
getUserPermissions: () => Permission[]; | ||
checkPermission: (permission: Permission) => boolean; | ||
checkAllPermissions: (permissions: Permission[]) => boolean; | ||
checkAtLeastOnePermission: (permissions: Permission[]) => boolean; | ||
}; | ||
|
||
const usePermissions = (): PermissionsFns => { | ||
const auth = useAppSelector((state) => state.auth); | ||
const [userPermMap, setUserPermMap] = useState<Record<string, boolean>>({}); | ||
|
||
const formatKey = useCallback((p: { authority: string; name: string }) => `${p.authority}-${p.name}`, []); | ||
|
||
useEffect(() => { | ||
if (auth.user) { | ||
const userPermissions = auth.user.roles.map((role) => role.permissions).flat(); | ||
const newPermMap: Record<string, boolean> = {}; | ||
userPermissions.forEach((perm) => (newPermMap[formatKey(perm)] = true)); | ||
setUserPermMap(newPermMap); | ||
} | ||
}, [auth.user, formatKey]); | ||
|
||
const getUserPermissions = (): Permission[] => { | ||
if (auth.user) { | ||
return Object.entries(PERMISSIONS) | ||
.filter(([, definition]) => userPermMap[formatKey(definition)]) | ||
.map(([name]) => parseInt(name)); | ||
} | ||
return []; | ||
}; | ||
|
||
const checkPermission = (permission: Permission): boolean => { | ||
const definition = PERMISSIONS[permission]; | ||
return definition && !!userPermMap[formatKey(definition)]; | ||
}; | ||
|
||
const checkAllPermissions = (permissions: Permission[]): boolean => { | ||
for (const permission of permissions) { | ||
if (!checkPermission(permission)) return false; | ||
} | ||
return true; | ||
}; | ||
|
||
const checkAtLeastOnePermission = (permissions: Permission[]): boolean => { | ||
for (const permission of permissions) { | ||
if (checkPermission(permission)) return true; | ||
} | ||
return false; | ||
}; | ||
|
||
return { getUserPermissions, checkPermission, checkAllPermissions, checkAtLeastOnePermission }; | ||
}; | ||
|
||
export default usePermissions; |
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.