UNPKG

@enonic/mock-xp

Version:

Mock Enonic XP API JavaScript Library

60 lines (59 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Context = void 0; var tslib_1 = require("tslib"); var constants_1 = require("../constants"); var Context = (function () { function Context(_a) { var branch = _a.branch, repository = _a.repository, _b = _a.attributes, attributes = _b === void 0 ? {} : _b, _c = _a.principals, principals = _c === void 0 ? [] : _c, server = _a.server, user = _a.user; var _this = this; this.principals = []; if (repository.startsWith('com.enonic.cms.') && !['draft', 'master'].includes(branch)) { throw new Error("Invalid branch: ".concat(branch, " for repository: ").concat(repository, "!")); } this.server = server; this.auth = server.auth; this.branch = branch; this.repository = repository; this.attributes = tslib_1.__assign({}, attributes); this.principals = tslib_1.__spreadArray([], tslib_1.__read(principals), false); if (user === null || user === void 0 ? void 0 : user.login) { var _d = user.idProvider, idProvider = _d === void 0 ? 'system' : _d, login = user.login; this.user = this.auth.getUserByName({ idProvider: idProvider || 'system', name: login }); if (idProvider === 'system' && login === 'su') { this.addPrincipal(constants_1.ROLE_SYSTEM_ADMIN); } [ constants_1.ROLE_SYSTEM_EVERYONE, this.user.key ].forEach(function (principalKey) { _this.addPrincipal(principalKey); }); } else { this.addPrincipal(constants_1.USER_SYSTEM_ANONYMOUS); } } Context.prototype.addPrincipal = function (principal) { if (!this.principals.includes(principal)) { this.principals.push(principal); } return this; }; Context.prototype.get = function () { return { attributes: this.attributes, authInfo: { principals: this.principals, user: this.user || null, }, branch: this.branch, repository: this.repository, }; }; return Context; }()); exports.Context = Context;