forked from informatici/openhospital-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
208 additions
and
75 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
File renamed without changes.
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
29 changes: 29 additions & 0 deletions
29
src/components/accessories/admin/users/editPermissions/AreaAccess.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,29 @@ | ||
import React from "react"; | ||
|
||
import { PermissionDTO } from "../../../../../generated"; | ||
import { PermissionCheckbox } from "./PermissionCheckbox"; | ||
|
||
interface IProps { | ||
permissions: PermissionDTO[]; | ||
thisGroupId: string; | ||
onChange: (permission: PermissionDTO) => void; | ||
} | ||
export const AreaAccess = ({ permissions, thisGroupId, onChange }: IProps) => { | ||
return ( | ||
<ul> | ||
{permissions | ||
.filter( | ||
(perm: PermissionDTO) => perm.name && /\.access$/.test(perm.name) | ||
) | ||
.map((perm, index) => ( | ||
<li key={index}> | ||
<PermissionCheckbox | ||
permission={perm} | ||
onChange={onChange} | ||
thisGroupId={thisGroupId} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; |
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
55 changes: 55 additions & 0 deletions
55
src/components/accessories/admin/users/editPermissions/GroupPermissionsEditor.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,55 @@ | ||
import React, { useEffect, useState } from "react"; | ||
|
||
import { PermissionDTO } from "../../../../../generated"; | ||
import { AclTable } from "./AclTable"; | ||
import { AreaAccess } from "./AreaAccess"; | ||
|
||
interface IProps { | ||
permissions: PermissionDTO[]; | ||
thisGroupId: string; | ||
} | ||
export const GroupPermissionsEditor = ({ | ||
permissions, | ||
thisGroupId, | ||
}: IProps) => { | ||
const [permissionsStack, setPermissionsStack] = useState<PermissionDTO[]>([]); | ||
const [permissionsState, setPermissionsState] = useState<PermissionDTO[]>([]); | ||
|
||
useEffect(() => { | ||
setPermissionsState(permissions); | ||
}, [permissions]); | ||
|
||
const handleChange = (newPermission: PermissionDTO) => { | ||
const otherPermissions = permissionsStack.filter( | ||
({ id }) => id !== newPermission.id | ||
); | ||
setPermissionsStack([...otherPermissions, newPermission]); | ||
const newState = permissionsState.map((perm) => | ||
perm.id === newPermission.id ? newPermission : perm | ||
); | ||
setPermissionsState(newState); | ||
}; | ||
|
||
return ( | ||
<> | ||
<h2>Areas access</h2> | ||
<AreaAccess | ||
permissions={permissionsState} | ||
thisGroupId={thisGroupId} | ||
onChange={handleChange} | ||
/> | ||
|
||
<h2>Access-control list</h2> | ||
<AclTable | ||
permissions={permissionsState} | ||
userGroupId={thisGroupId} | ||
onChange={handleChange} | ||
/> | ||
{permissionsStack.length && ( | ||
<p> | ||
Editing permissions: {permissionsStack.map(({ id }) => id).join(",")} | ||
</p> | ||
)} | ||
</> | ||
); | ||
}; |
14 changes: 8 additions & 6 deletions
14
...in/users/editGroup/PermissionCheckbox.tsx → ...rs/editPermissions/PermissionCheckbox.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
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
57 changes: 57 additions & 0 deletions
57
src/components/accessories/admin/users/editPermissions/permission.utils.ts
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,57 @@ | ||
import { PermissionDTO } from "../../../../../generated"; | ||
|
||
export enum Crud { | ||
CREATE = "create", | ||
READ = "read", | ||
UPDATE = "update", | ||
DELETE = "delete", | ||
} | ||
|
||
export const groupPermissions = ( | ||
permissions: PermissionDTO[] | ||
): Map<string, Record<Crud, PermissionDTO>> => { | ||
let permissionNames = new Map(); | ||
for (let i = 0; i < permissions.length; i++) { | ||
const matches = | ||
permissions[i].name && | ||
/([a-z]+)\.(create|read|update|delete)$/.exec(permissions[i].name || ""); | ||
// no match: skip | ||
if (!matches) continue; | ||
const [, key, access] = matches; | ||
|
||
if (!!permissionNames.get(key)?.[access]) { | ||
throw new Error(`duplicate permission ${key}.${access}`); | ||
} | ||
|
||
permissionNames.set(key, { | ||
...permissionNames.get(key), | ||
[access]: permissions[i], | ||
}); | ||
} | ||
return permissionNames; | ||
}; | ||
|
||
export const computeNewPermission = ( | ||
thisGroupId: string, | ||
permission: PermissionDTO, | ||
val: boolean | ||
): PermissionDTO => { | ||
const getUserGroups = () => { | ||
if (val) { | ||
if (permission.userGroupIds.includes(thisGroupId)) | ||
return permission.userGroupIds; | ||
return [...permission.userGroupIds, thisGroupId]; | ||
} | ||
let userGroupIds: string[] = []; | ||
for (let index = 0; index < permission.userGroupIds.length; index++) { | ||
if (permission.userGroupIds[index] !== thisGroupId) | ||
userGroupIds = [...userGroupIds, permission.userGroupIds[index]]; | ||
} | ||
return userGroupIds; | ||
}; | ||
|
||
return { | ||
...permission, | ||
userGroupIds: getUserGroups(), | ||
}; | ||
}; |
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