Remove the `user.permissions` field and resolve the permissions directly from the accounts array in the user. This change ensures that the cache or previous values from the last active account don't affect the permissions. In this PR: - Remove user.permissions usage, replace it with getUserPermissions method. - Remove json.permissions from user.json.jbuilder
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
export const hasPermissions = (
|
|
requiredPermissions = [],
|
|
availablePermissions = []
|
|
) => {
|
|
return requiredPermissions.some(permission =>
|
|
availablePermissions.includes(permission)
|
|
);
|
|
};
|
|
|
|
export const getCurrentAccount = ({ accounts } = {}, accountId = null) => {
|
|
return accounts.find(account => Number(account.id) === Number(accountId));
|
|
};
|
|
|
|
export const getUserPermissions = (user, accountId) => {
|
|
const currentAccount = getCurrentAccount(user, accountId) || {};
|
|
return currentAccount.permissions || [];
|
|
};
|
|
|
|
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;
|
|
}, {});
|