UNPKG

@webda/core

Version:

Expose API with Lambda

35 lines 1.09 kB
import { CoreModel, WebdaError } from "../index.js"; class RoleModel extends CoreModel { isPermissive() { return false; } async getRoles(ctx) { if (!ctx.getCurrentUserId()) { throw new WebdaError.Forbidden("No user"); } // If roles are cached in session if (ctx.getSession().roles) { return ctx.getSession().roles; } let user = await ctx.getCurrentUser(); // Cache roles in session ctx.getSession().roles = user.getRoles(); return ctx.getSession().roles; } async canAct(ctx, action) { // If this action doesn't require role if (!this.getRolesMap()[action]) { if (this.isPermissive()) { return true; } return "No permission for this action defined"; } let roles = await this.getRoles(ctx); if (roles.indexOf(this.getRolesMap()[action]) >= 0) { return true; } return "No permission"; } } export { RoleModel }; //# sourceMappingURL=rolemodel.js.map