mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-20 04:54:57 +00:00
This pull request introduces frontend role filtering to allStatusChat getter. The key changes include the addition of a new helper function to get the user's role, updates to the conversation filtering logic to incorporate role and permissions, and the addition of unit tests for the new filtering logic. --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
82 lines
2.5 KiB
JavaScript
82 lines
2.5 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 || [];
|
|
};
|
|
|
|
export const getUserRole = (user, accountId) => {
|
|
const currentAccount = getCurrentAccount(user, accountId) || {};
|
|
if (currentAccount.custom_role_id) {
|
|
return 'custom_role';
|
|
}
|
|
|
|
return currentAccount.role || 'agent';
|
|
};
|
|
|
|
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;
|
|
}, {});
|
|
|
|
/**
|
|
* Filters and transforms items based on user permissions.
|
|
*
|
|
* @param {Object} items - An object containing items to be filtered.
|
|
* @param {Array} userPermissions - Array of permissions the user has.
|
|
* @param {Function} getPermissions - Function to extract required permissions from an item.
|
|
* @param {Function} [transformItem] - Optional function to transform each item after filtering.
|
|
* @returns {Array} Filtered and transformed items.
|
|
*/
|
|
export const filterItemsByPermission = (
|
|
items,
|
|
userPermissions,
|
|
getPermissions,
|
|
transformItem = (key, item) => ({ key, ...item })
|
|
) => {
|
|
// Helper function to check if an item has the required permissions
|
|
const hasRequiredPermissions = item => {
|
|
const requiredPermissions = getPermissions(item);
|
|
return (
|
|
requiredPermissions.length === 0 ||
|
|
hasPermissions(requiredPermissions, userPermissions)
|
|
);
|
|
};
|
|
|
|
return Object.entries(items)
|
|
.filter(([, item]) => hasRequiredPermissions(item)) // Keep only items with required permissions
|
|
.map(([key, item]) => transformItem(key, item)); // Transform each remaining item
|
|
};
|