@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.
73 lines (72 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "PermissionService", {
enumerable: true,
get: function() {
return PermissionService;
}
});
const _models = require("../../models");
const _authorizationconstants = require("../../constants/authorization.constants");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
class PermissionService {
logger;
constructor(loggerFactory){
this.logger = loggerFactory(PermissionService.name);
}
_permissions = [];
get permissions() {
return this._permissions;
}
toDto(permission) {
return {
id: permission.id,
name: permission.name
};
}
authorizePermission(requiredPermission, assignedPermissions) {
return !!assignedPermissions.find((assignedPermission)=>{
const normalizePermission = this.normalizePermission(assignedPermission);
if (!normalizePermission) return false;
return normalizePermission === requiredPermission;
});
}
async getPermissionByName(permissionName) {
const permission = this.permissions.find((r)=>r.name === permissionName);
if (!permission) throw new _runtimeexceptions.NotFoundException("Permission not found");
return permission;
}
async getPermission(permissionId) {
const permission = this.permissions.find((r)=>r.id === permissionId);
if (!permission) throw new _runtimeexceptions.NotFoundException(`Permission by provided id not found`);
return permission;
}
async syncPermissions() {
this._permissions = [];
const permissionDefinition = (0, _authorizationconstants.flattenPermissionDefinition)();
for (let permission of permissionDefinition){
const storedPermission = await _models.Permission.findOne({
name: permission
});
if (!storedPermission) {
const newPermission = await _models.Permission.create({
name: permission
});
this._permissions.push(newPermission);
} else {
this._permissions.push(storedPermission);
}
}
}
normalizePermission(assignedPermission) {
const permissionInstance = this.permissions.find((r)=>r.id === assignedPermission || r.name === assignedPermission);
if (!permissionInstance) {
this.logger.warn("The permission by by provided id did not exist. Skipping");
return;
}
return permissionInstance.name;
}
}
//# sourceMappingURL=permission.service.js.map