@tmlmobilidade/utils
Version:
This package provides a collection of common utility functions used across projects within the organization.
78 lines (77 loc) • 2.78 kB
JavaScript
/* eslint-disable @typescript-eslint/no-explicit-any */
/* * */
import { ALLOW_ALL_FLAG } from '@tmlmobilidade/lib';
import { mergekit } from 'mergekit';
/**
* Get a permission from a list of permissions
* @param permissions - The list of permissions
* @param scope - The scope of the permission
* @param action - The action of the permission
* @returns The permission
*/
export function getPermission(permissions, scope, action) {
return mergekit([...(permissions ?? [])], {
appendArrays: true,
dedupArrays: true,
onlyObjectWithKeyValues: [
{ key: 'scope', value: scope },
{ key: 'action', value: action },
],
});
}
/**
* Check if a permission exists in a list of permissions
* @param permissions - The list of permissions
* @param scope - The scope of the permission
* @param action - The action of the permission
* @returns The permission
*/
export function hasPermission(permissions, scope, action) {
return permissions.find(permission => permission.scope === scope && permission.action === action) !== undefined;
}
/**
* Check if a value exists in a resource of a permission
* @param permissions - The list of permissions
* @param value - The value to check
* @param resource_key - The key of the resource
* @param scope - The scope of the permission
* @param action - The action of the permission
* @returns The permission
*/
export function hasPermissionResource({ action, permissions, resource_key, scope, value }) {
if (!permissions)
return false;
const permission = permissions.find(permission => permission.action === action && permission.scope === scope);
if (!permission)
return false;
// Check if value exists in the permission.resource[resource_key]
const resourceValues = permission.resource?.[resource_key];
if (!resourceValues)
return false;
// If is Array
if (Array.isArray(resourceValues) && resourceValues.includes(ALLOW_ALL_FLAG))
return true;
if (Array.isArray(resourceValues) && resourceValues.includes(value))
return true;
// If is not Array, check if value is equal to resourceValues
if (resourceValues === value)
return true;
return false;
}
/**
* Ensure a permission is granted in a fastify request
* @param request - The FastifyRequest
* @param reply - The FastifyReply
* @param params - The parameters of the permission
* @returns The allowed
*/
export function hasAPIResourcePermission(request, params) {
const allowed = hasPermissionResource({
action: params.action,
permissions: [request.permissions],
resource_key: params.resource_key,
scope: params.scope,
value: params.value,
});
return allowed;
}