pagamio-frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
118 lines (117 loc) • 4.01 kB
JavaScript
import { RBACConfigStore } from './store';
/**
* Get user roles from the user object
* @param user - The user object
* @param roleKey - The key for role(s) - can be string or array of strings
* @returns Array of roles
*/
function getUserRoles(user, roleKey) {
if (roleKey && user[roleKey]) {
const val = user[roleKey];
return Array.isArray(val) ? val : [val];
}
return [];
}
/**
* Initialize the RBAC system with configuration
* @param options - RBAC configuration options
*/
export const initializeRBAC = (options) => {
const store = RBACConfigStore.getInstance();
store.initialize(options);
};
/**
* Check if a user has a specific permission
* @param user - The user object to check permissions for
* @param permission - The permission to check
* @returns boolean indicating if the user has the permission
*/
export const hasPermission = (user, permission) => {
if (!user)
return false;
const store = RBACConfigStore.getInstance();
const { rbacConfig, allPermissionValue, roleKey } = store.options;
const roles = getUserRoles(user, roleKey);
if (!roles.length)
return false;
for (const role of roles) {
const rolePermissions = rbacConfig[role];
if (!rolePermissions)
continue;
if (rolePermissions.includes(allPermissionValue) || rolePermissions.includes(permission)) {
return true;
}
}
return false;
};
/**
* Check if a user has any of the specified permissions
* @param user - The user object to check permissions for
* @param permissions - Array of permissions to check
* @returns boolean indicating if the user has any of the permissions
*/
export const hasAnyPermission = (user, permissions) => {
return permissions.some((permission) => hasPermission(user, permission));
};
/**
* Check if a user has all of the specified permissions
* @param user - The user object to check permissions for
* @param permissions - Array of permissions to check
* @returns boolean indicating if the user has all of the permissions
*/
export const hasAllPermissions = (user, permissions) => {
return permissions.every((permission) => hasPermission(user, permission));
};
/**
* Get all permissions for a user (union of all roles' permissions)
* @param user - The user object to get permissions for
* @returns Array of permissions the user has
*/
export const getUserPermissions = (user) => {
if (!user)
return [];
const store = RBACConfigStore.getInstance();
const { rbacConfig, allPermissionValue, allPermissions, roleKey } = store.options;
const roles = getUserRoles(user, roleKey);
if (!roles.length)
return [];
let permissionsSet = new Set();
for (const role of roles) {
const rolePermissions = rbacConfig[role];
if (!rolePermissions)
continue;
if (rolePermissions.includes(allPermissionValue)) {
// If any role has ALL, return all permissions
return allPermissions;
}
rolePermissions.forEach((perm) => permissionsSet.add(perm));
}
return Array.from(permissionsSet);
};
/**
* Check if a user has a specific role (matches any of their roles)
* @param user - The user object to check role for
* @param roleName - The role name to check against
* @returns boolean indicating if the user has the specified role
*/
export const hasRole = (user, roleName) => {
if (!user)
return false;
const store = RBACConfigStore.getInstance();
const { roleKey } = store.options;
const roles = getUserRoles(user, roleKey);
if (!roles.length)
return false;
return roles.includes(roleName);
};
/**
* Check if a user has access to IQ Retail
*/
export const hasIQRetailAccess = (user) => {
if (!user)
return false;
// Check permission
const hasPerm = hasPermission(user, 'export:iqretail');
// Check user-level flag
return hasPerm && !!user.hasIQRetailAccess;
};