@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.
132 lines (131 loc) • 4.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "RoleService", {
enumerable: true,
get: function() {
return RoleService;
}
});
const _baseservice = require("./base.service");
const _entities = require("../../entities");
const _roledto = require("../interfaces/role.dto");
const _lodash = require("lodash");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
const _authorizationconstants = require("../../constants/authorization.constants");
class RoleService extends (0, _baseservice.BaseService)(_entities.Role, _roledto.RoleDto) {
appDefaultRole;
appDefaultRoleNoLogin;
settingsStore;
logger;
constructor(typeormService, loggerFactory, appDefaultRole, appDefaultRoleNoLogin, settingsStore){
super(typeormService), this.appDefaultRole = appDefaultRole, this.appDefaultRoleNoLogin = appDefaultRoleNoLogin, this.settingsStore = settingsStore, this._roles = [];
this.logger = loggerFactory(RoleService.name);
}
_roles;
get roles() {
return this._roles;
}
async getAppDefaultRole() {
if (await this.settingsStore.getLoginRequired()) {
return this.appDefaultRole;
}
return this.appDefaultRoleNoLogin;
}
async getAppDefaultRoleIds() {
if (!this._roles?.length) {
await this.syncRoles();
}
const guestRole = this.getRoleByName(await this.getAppDefaultRole());
return [
guestRole.id
];
}
toDto(role) {
return {
id: role.id,
name: role.name
};
}
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;
}
authorizeRoles(requiredRoles, assignedRoles, subset) {
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;
}
authorizeRole(requiredRole, assignedRoles) {
return !!assignedRoles.find((ar)=>{
const normalizedRole = this.normalizeRoleIdOrName(ar);
if (!normalizedRole) return false;
return normalizedRole === requiredRole;
});
}
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 was not found`);
return role;
}
getRoleByName(roleName) {
const role = this._roles.find((r)=>r.name === roleName);
if (!role) throw new _runtimeexceptions.NotFoundException(`Role by provided name was not found`);
return role;
}
getRolePermissions(role) {
if (!role || !role?.length) {
return [];
}
const normalizedRole = this.normalizeRoleIdOrName(role);
if (!normalizedRole?.length) {
return [];
}
return _authorizationconstants.ROLE_PERMS[normalizedRole];
}
async getSynchronizedRoleByName(roleName) {
if (!this._roles?.length) {
await this.syncRoles();
}
return this.getRoleByName(roleName);
}
async syncRoles() {
this._roles = [];
for (let roleName of Object.values(_authorizationconstants.ROLES)){
const storedRole = await this.repository.findOneBy({
name: roleName
});
if (!storedRole) {
const newRole = await this.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