Merge pull request 'patches v1.5.3' (#101) from develop into main
Reviewed-on: #101
This commit is contained in:
commit
1a71d32b6d
1 changed files with 48 additions and 0 deletions
|
@ -73,6 +73,23 @@ export default class PermissionHelper {
|
||||||
}, false);
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static canAccessSection(permissions: PermissionObject, section: PermissionSection): boolean {
|
||||||
|
if (permissions?.admin || permissions?.adminByOwner) return true;
|
||||||
|
if (permissions[section] != undefined) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static canAccessSomeSection(
|
||||||
|
permissions: PermissionObject,
|
||||||
|
checks: Array<{
|
||||||
|
section: PermissionSection;
|
||||||
|
}>
|
||||||
|
): boolean {
|
||||||
|
return checks.reduce<boolean>((prev, curr) => {
|
||||||
|
return prev || this.canAccessSection(permissions, curr.section);
|
||||||
|
}, false);
|
||||||
|
}
|
||||||
|
|
||||||
static canValue(permissions: PermissionObject, key: string, emptyIfAdmin: boolean = false): string {
|
static canValue(permissions: PermissionObject, key: string, emptyIfAdmin: boolean = false): string {
|
||||||
if (emptyIfAdmin && (permissions.admin || permissions.adminByOwner)) return "";
|
if (emptyIfAdmin && (permissions.admin || permissions.adminByOwner)) return "";
|
||||||
return permissions?.additional?.[key] ?? "";
|
return permissions?.additional?.[key] ?? "";
|
||||||
|
@ -147,6 +164,37 @@ export default class PermissionHelper {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static sectionAccessPassCheckMiddleware(
|
||||||
|
section: PermissionSection
|
||||||
|
): (req: Request, res: Response, next: Function) => void {
|
||||||
|
return (req: Request, res: Response, next: Function) => {
|
||||||
|
const permissions = req.permissions;
|
||||||
|
const isOwner = req.isOwner;
|
||||||
|
|
||||||
|
if (isOwner || this.canAccessSection(permissions, section)) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
throw new ForbiddenRequestException(`missing permission for ${section}.${module}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static sectionAccessPassCheckSomeMiddleware(
|
||||||
|
checks: Array<{ section: PermissionSection }>
|
||||||
|
): (req: Request, res: Response, next: Function) => void {
|
||||||
|
return (req: Request, res: Response, next: Function) => {
|
||||||
|
const permissions = req.permissions;
|
||||||
|
const isOwner = req.isOwner;
|
||||||
|
|
||||||
|
if (isOwner || this.canAccessSomeSection(permissions, checks)) {
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
let permissionsToPass = checks.map((c) => `${c.section}`).join(" or ");
|
||||||
|
throw new ForbiddenRequestException(`missing permission for ${permissionsToPass}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
static isAdminMiddleware(): (req: Request, res: Response, next: Function) => void {
|
static isAdminMiddleware(): (req: Request, res: Response, next: Function) => void {
|
||||||
return (req: Request, res: Response, next: Function) => {
|
return (req: Request, res: Response, next: Function) => {
|
||||||
const permissions = req.permissions;
|
const permissions = req.permissions;
|
||||||
|
|
Loading…
Add table
Reference in a new issue