mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-01 11:37:58 +00:00
We previously relied on user roles to determine whether to render specific routes in our frontend components. A permissions-based model is replacing this approach. Follow up: #9695 Co-authored-by: Pranav <pranavrajs@gmail.com>
35 lines
834 B
JavaScript
35 lines
834 B
JavaScript
export const hasPermissions = (
|
|
requiredPermissions = [],
|
|
availablePermissions = []
|
|
) => {
|
|
return requiredPermissions.some(permission =>
|
|
availablePermissions.includes(permission)
|
|
);
|
|
};
|
|
|
|
const isPermissionsPresentInRoute = route =>
|
|
route.meta && route.meta.permissions;
|
|
|
|
export const buildPermissionsFromRouter = (routes = []) =>
|
|
routes.reduce((acc, route) => {
|
|
if (route.name) {
|
|
if (!isPermissionsPresentInRoute(route)) {
|
|
// eslint-disable-next-line
|
|
console.error(route);
|
|
throw new Error(
|
|
"The route doesn't have the required permissions defined"
|
|
);
|
|
}
|
|
acc[route.name] = route.meta.permissions;
|
|
}
|
|
|
|
if (route.children) {
|
|
acc = {
|
|
...acc,
|
|
...buildPermissionsFromRouter(route.children),
|
|
};
|
|
}
|
|
|
|
return acc;
|
|
}, {});
|