You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently using Loopback4 and node.js in my project. And I am using RBAC with domain model to generate policies. How do I get access to the domain value and use it in the Request Parameter?
#9173
I am currently using Loopback4 and node.js in my project. And I am using RBAC with domain model to generate policies. How do I get access to the domain value and use it in the Request Parameter?
const allowedRoles = metadata.allowedRoles;
if (!allowedRoles) return AuthorizationDecision.ALLOW;
if (allowedRoles.length < 1) return AuthorizationDecision.DENY;
let allow = false;
// An optimization for ONLY searching among the allowed roles' policies
for (const role of allowedRoles) {
const enforcer = await this.enforcerFactory(role);
const allowedByRole = await enforcer.enforce(
request.subject,
request.object,
request.action,
);
debug(`authorizer role: ${role}, result: ${allowedByRole}`);
if (allowedByRole) {
allow = true;
break;
}
}
debug('final result: ', allow);
if (allow) return AuthorizationDecision.ALLOW;
else if (allow === false) return AuthorizationDecision.DENY;
return AuthorizationDecision.ABSTAIN;
}
// Generate the user name according to the naming convention
// in casbin policy
// A user's name would be u${id}
getUserId(id: string): string {
return id;
}
}
//How do I modify this code base on RBAC with domains model?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am currently using Loopback4 and node.js in my project. And I am using RBAC with domain model to generate policies. How do I get access to the domain value and use it in the Request Parameter?
//RBAC model with domains
[request_definition]
r = sub, dom, obj, act
[policy_definition]
p = sub, dom, obj, act
[role_definition]
g = _, _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act
//Casbin.Authorizer.ts
https://github.com/loopbackio/loopback-next/blob/master/examples/access-control-migration/src/components/casbin-authorization/services/casbin.authorizer.ts
import {
AuthorizationContext,
AuthorizationDecision,
AuthorizationMetadata,
AuthorizationRequest,
Authorizer
} from '@loopback/authorization';
import {Provider, inject} from '@loopback/core';
import * as casbin from 'casbin';
import {RESOURCE_ID} from '../../../keys';
const debug = require('debug')('loopback:example:acl');
const DEFAULT_SCOPE = 'execute';
// Class level authorizer
export class CasbinAuthorizationProvider implements Provider {
constructor(
@Inject('casbin.enforcer.factory')
private enforcerFactory: (name: string) => Promise<casbin.Enforcer>,
) {}
value(): Authorizer {
return this.authorize.bind(this);
}
async authorize(
authorizationCtx: AuthorizationContext,
metadata: AuthorizationMetadata,
): Promise {
const subject = this.getUserId(authorizationCtx.principals[0].id);
const resourceId = await authorizationCtx.invocationContext.get(
RESOURCE_ID,
{optional: true},
);
const object = resourceId ?? metadata.resource ?? authorizationCtx.resource;
const request: AuthorizationRequest = {
subject,
object,
action: metadata.scopes?.[0] ?? DEFAULT_SCOPE,
};
}
// Generate the user name according to the naming convention
// in casbin policy
// A user's name would be
u${id}
getUserId(id: string): string {
return id;
}
}
//How do I modify this code base on RBAC with domains model?
Beta Was this translation helpful? Give feedback.
All reactions