UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.

133 lines (132 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "RoleService", { enumerable: true, get: function() { return RoleService; } }); const _lodash = require("lodash"); const _authorizationconstants = require("../../constants/authorization.constants"); const _models = require("../../models"); const _runtimeexceptions = require("../../exceptions/runtime.exceptions"); class RoleService { appDefaultRole; appDefaultRoleNoLogin; settingsStore; logger; constructor(loggerFactory, appDefaultRole, appDefaultRoleNoLogin, settingsStore){ this.appDefaultRole = appDefaultRole; this.appDefaultRoleNoLogin = appDefaultRoleNoLogin; this.settingsStore = settingsStore; this._roles = []; this.logger = loggerFactory(RoleService.name); } _roles; get roles() { return this._roles; } toDto(role) { return { id: role.id, name: role.name }; } async getAppDefaultRole() { if (await this.settingsStore.getLoginRequired()) { return this.appDefaultRole; } return this.appDefaultRoleNoLogin; } getRolesPermissions(roles) { let permissions = []; if (!roles?.length) return []; for (let role of roles){ const normalizedRole = this.normalizeRoleIdOrName(role); const rolePermissions = this.getRolePermissions(normalizedRole); permissions = (0, _lodash.union)(permissions, rolePermissions); } return permissions; } getRolePermissions(role) { if (!role?.length) { return []; } const normalizedRole = this.normalizeRoleIdOrName(role); if (!normalizedRole?.length) { return []; } return _authorizationconstants.ROLE_PERMS[normalizedRole]; } async getAppDefaultRoleIds() { if (!this._roles?.length) { await this.syncRoles(); } const guestRole = this.getRoleByName(await this.getAppDefaultRole()); return [ guestRole.id ]; } async getSynchronizedRoleByName(roleName) { if (!this._roles?.length) { await this.syncRoles(); } return this.getRoleByName(roleName); } authorizeRole(requiredRole, assignedRoles) { return !!assignedRoles.find((ar)=>{ const normalizedRole = this.normalizeRoleIdOrName(ar); if (!normalizedRole) return false; return normalizedRole === requiredRole; }); } authorizeRoles(requiredRoles, assignedRoles, subset = true) { let isAuthorized = false; if (!requiredRoles?.length) return true; requiredRoles.forEach((rr)=>{ const result = this.authorizeRole(rr, assignedRoles); isAuthorized = subset ? isAuthorized || result : isAuthorized && result; }); return isAuthorized; } getRoleByName(roleName) { const role = this._roles.find((r)=>r.name === roleName); if (!role) throw new _runtimeexceptions.NotFoundException(`Role by provided name not found`); return role; } getManyRoles(roleIds) { return roleIds.map((roleId)=>this.getRole(roleId)); } getRole(roleId) { const role = this._roles.find((r)=>r.id === roleId); if (!role) throw new _runtimeexceptions.NotFoundException(`Role by provided id not found`); return role; } async syncRoles() { this._roles = []; for (let roleName of Object.values(_authorizationconstants.ROLES)){ const storedRole = await _models.Role.findOne({ name: roleName }); if (!storedRole) { const newRole = await _models.Role.create({ name: roleName }); this._roles.push(newRole); } else { this._roles.push(storedRole); } } } normalizeRoleIdOrName(assignedRole) { const roleInstance = this.roles.find((r)=>r.id === assignedRole || r.name === assignedRole); if (!roleInstance) { this.logger.warn(`The role by provided id was not found. Skipping.`); return; } return roleInstance.name; } } //# sourceMappingURL=role.service.js.map