-
Notifications
You must be signed in to change notification settings - Fork 21
fix(admin-ui): resolve issue preventing deletion of capabilities #2303
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
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd91c78
fix(admin-ui): resolve issue preventing deletion of capabilities
faisalsiddique4400 539ed86
requested changes done
faisalsiddique4400 418e835
Merge branch 'main' into admin-ui-issue-2270
duttarnab 66b2b4f
Removed un0necessary console
faisalsiddique4400 2988eb7
Merge branch 'main' into admin-ui-issue-2270
moabu cba02b6
Merge branch 'main' into admin-ui-issue-2270
duttarnab dab2860
Merge branch 'main' into admin-ui-issue-2270
faisalsiddique4400 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,82 @@ | ||
| // Utilities for resolving permission keys, computing mapped roles, and building user-friendly messages | ||
|
|
||
| export type ApiPermissionItem = { | ||
| inum?: string | ||
| permission?: string | ||
| [key: string]: unknown | ||
| } | ||
|
|
||
| export type RolePermissionMappingEntry = { | ||
| role: string | ||
| permissions: string[] | ||
| } | ||
|
|
||
| type ActionData = | ||
| | { inum?: string; permission?: string; [key: string]: unknown } | ||
| | string | ||
| | null | ||
| | undefined | ||
|
|
||
| /** | ||
| * Resolve the permission key (string) from actionData and the list of apiPermissions. | ||
| * actionData may be an object with { inum, permission } or just an inum string. | ||
| */ | ||
| export function resolvePermissionKey( | ||
| actionData: ActionData, | ||
| apiPermissions: ApiPermissionItem[] | undefined, | ||
| ): string | undefined { | ||
| const isObject = (value: ActionData): value is { inum?: string; permission?: string } => { | ||
| return typeof value === 'object' && value !== null | ||
| } | ||
|
|
||
| const permissionFromAction = isObject(actionData) ? actionData.permission : undefined | ||
| if (permissionFromAction) return permissionFromAction | ||
|
|
||
| const permissionInum = isObject(actionData) ? actionData.inum : actionData | ||
| if (!permissionInum) return undefined | ||
|
|
||
| const found = Array.isArray(apiPermissions) | ||
| ? apiPermissions.find((p) => p?.inum === permissionInum) | ||
| : undefined | ||
| return found?.permission | ||
| } | ||
|
|
||
| /** | ||
| * Find roles that contain the given permission key using the role-permission mapping from the store. | ||
| */ | ||
| export function findRolesForPermission( | ||
| permissionKey: string | undefined, | ||
| rolePermissionMapping: RolePermissionMappingEntry[] | undefined, | ||
| ): string[] { | ||
| if (!permissionKey || !Array.isArray(rolePermissionMapping)) return [] | ||
| return rolePermissionMapping | ||
| .filter((r) => Array.isArray(r?.permissions) && r.permissions.includes(permissionKey)) | ||
| .map((r) => r.role) | ||
| } | ||
|
|
||
| export function buildMappingGuidanceMessage( | ||
| permissionKey: string | undefined, | ||
| mappedRoles: string[] | undefined, | ||
| ): string { | ||
| if (!permissionKey) { | ||
| return 'Unable to delete permission. Permission identifier not found.' | ||
| } | ||
|
|
||
| if (!mappedRoles || mappedRoles.length === 0) { | ||
| return `Unable to delete permission "${permissionKey}". Please check if it's still in use.` | ||
| } | ||
|
|
||
| const rolesList = mappedRoles.join(', ') | ||
| const rolesWord = mappedRoles.length > 1 ? 'roles' : 'role' | ||
| return `Unable to delete permission "${permissionKey}". It is currently mapped to ${rolesWord}: ${rolesList}. Please remove it from the role mapping menu first.` | ||
| } | ||
|
|
||
| export function buildPermissionDeleteErrorMessage( | ||
| actionData: ActionData, | ||
| apiPermissions: ApiPermissionItem[] | undefined, | ||
| rolePermissionMapping: RolePermissionMappingEntry[] | undefined, | ||
| ): string { | ||
| const permissionKey = resolvePermissionKey(actionData, apiPermissions) | ||
| const mappedRoles = findRolesForPermission(permissionKey, rolePermissionMapping) | ||
| return buildMappingGuidanceMessage(permissionKey, mappedRoles) | ||
| } | ||
faisalsiddique4400 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.