UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

72 lines (71 loc) • 2.9 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Role = void 0; const constants_1 = require("../constants"); const component_1 = require("../decorators/component"); const capabilities_1 = require("./capabilities"); const components_1 = require("./components"); const config_1 = require("../config"); let Role = class Role { components; config; primaryName; names = new Set(); capabilities = new Set(); constructor(components, config, primaryName = "anonymous", capabilities = []) { this.components = components; this.config = config; this.primaryName = primaryName; this.primaryName = this.primaryName.toLowerCase(); this.addNames([this.primaryName]); if (Array.isArray(capabilities)) { this.add(capabilities); } } addNames(names) { names.map((name) => this.names.add(name.toLowerCase())); } // is_super_admin isSuperAdmin() { return ((this.config.isMultiSite() && this.is("superadmin")) || (!this.config.isMultiSite() && this.has("delete_users"))); } isAdmin() { return this.is("administrator") || this.isSuperAdmin(); } is(roleName) { return this.names.has(roleName); } add(capabilities) { capabilities.map((capability) => this.capabilities.add(capability)); } has(cap) { return this.capabilities.has(cap); } async can(action, user, ...args) { if (typeof action === "undefined") { return false; } const capabilities = this.components.get(capabilities_1.Capabilities); const results = await capabilities.check(action, user, ...args); if (results.includes(constants_1.DO_NOT_ALLOW)) { return false; } return results.length == results.filter((cap) => this.has(cap)).length; } }; exports.Role = Role; exports.Role = Role = __decorate([ (0, component_1.component)({ scope: constants_1.Scope.Transient }), __metadata("design:paramtypes", [components_1.Components, config_1.Config, Object, Array]) ], Role);