Fixed unknown roles
This commit is contained in:
@@ -8,15 +8,15 @@ class PermissionManager extends AccessControl {
|
||||
return new PermQuery(this.getGrants(), roleOrRequest);
|
||||
}
|
||||
|
||||
public getRouter(resource: string, opts: Partial<RermRouterOpts>): RequestHandler {
|
||||
public getRouter(resource: string, opts?: Partial<RermRouterOpts>): RequestHandler {
|
||||
return async (req: Request, res, next) => {
|
||||
let query = this.can(req);
|
||||
if (opts.context)
|
||||
query = query.context(opts.context);
|
||||
if (opts.action)
|
||||
query = query.execute(opts.action);
|
||||
if (opts.skipConditions)
|
||||
query = query.skipConditions(opts.skipConditions);
|
||||
if (opts?.context)
|
||||
query = query.context(opts?.context);
|
||||
if (opts?.action)
|
||||
query = query.execute(opts?.action);
|
||||
if (opts?.skipConditions)
|
||||
query = query.skipConditions(opts?.skipConditions);
|
||||
const permission = await query.on(resource);
|
||||
if (permission.granted) {
|
||||
req.permissionDetails = permission;
|
||||
@@ -40,7 +40,7 @@ export class PermQuery extends Query {
|
||||
constructor(grants: unknown, roleOrRequest: Request|string|string[]|IQueryInfo) {
|
||||
function isRequest(obj: unknown): obj is Request {
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
return typeof obj === 'object' && obj && obj.hasOwnProperty('path') || false;
|
||||
return typeof obj === 'object' && obj && obj.hasOwnProperty('res') || false;
|
||||
}
|
||||
if (isRequest(roleOrRequest)) {
|
||||
super(grants, []);
|
||||
@@ -53,7 +53,9 @@ export class PermQuery extends Query {
|
||||
public async on(resource: string, skipConditions?: boolean): Promise<Permission> {
|
||||
if (this.resolveRequest) {
|
||||
const userInfo = await this.resolveRequest.getUserInfo();
|
||||
this.role(userInfo?.groups ?? []);
|
||||
const availableRoles = Object.keys(this._grants);
|
||||
const roles = (userInfo?.groups ?? []).filter(x => availableRoles.includes(x));
|
||||
this.role(roles);
|
||||
}
|
||||
if (
|
||||
typeof this._.role === 'object' && this._.role.includes('noaccess') ||
|
||||
|
||||
Reference in New Issue
Block a user