ENCOA-316 ENCOA-317:
Refactor components to remove Layout wrapper and pass it in the App component , implemented a skeleton feedback while loading page and improved API calls related to Dashboard/User Profile
This commit is contained in:
@@ -36,6 +36,43 @@ export function checkAccess(user: User, types: Type[], permissions?: PermissionT
|
||||
return true;
|
||||
}
|
||||
|
||||
export function groupAllowedEntitiesByPermissions(
|
||||
user: User,
|
||||
entities: EntityWithRoles[],
|
||||
permissions: RolePermission[]
|
||||
): { [key: string]: EntityWithRoles[] } {
|
||||
if (["admin", "developer"].includes(user?.type)) {
|
||||
return permissions.reduce((acc, permission) => {
|
||||
acc[permission] = entities;
|
||||
return acc;
|
||||
}, {} as { [key: string]: EntityWithRoles[] });
|
||||
}
|
||||
|
||||
const userEntityMap = new Map(user.entities.map(e => [e.id, e]));
|
||||
const roleCache = new Map<string, Role | null>();
|
||||
|
||||
return entities.reduce((acc, entity) => {
|
||||
const userEntity = userEntityMap.get(entity.id);
|
||||
const role = userEntity
|
||||
? roleCache.get(userEntity.role) ??
|
||||
(() => {
|
||||
const foundRole = entity.roles.find(r => r.id === userEntity.role) || null;
|
||||
roleCache.set(userEntity.role, foundRole);
|
||||
return foundRole;
|
||||
})()
|
||||
: null;
|
||||
|
||||
permissions.forEach(permission => {
|
||||
if (!acc[permission]) acc[permission] = [];
|
||||
if (role && role.permissions.includes(permission)) {
|
||||
acc[permission].push(entity);
|
||||
}
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, {} as { [key: string]: EntityWithRoles[] });
|
||||
}
|
||||
|
||||
export function findAllowedEntities(user: User, entities: EntityWithRoles[], permission: RolePermission) {
|
||||
if (["admin", "developer"].includes(user?.type)) return entities
|
||||
|
||||
@@ -52,7 +89,6 @@ export function findAllowedEntitiesSomePermissions(user: User, entities: EntityW
|
||||
|
||||
export function doesEntityAllow(user: User, entity: EntityWithRoles, permission: RolePermission) {
|
||||
if (isAdmin(user)) return true
|
||||
|
||||
const userEntity = findBy(user.entities, 'id', entity?.id)
|
||||
if (!userEntity) return false
|
||||
|
||||
|
||||
Reference in New Issue
Block a user