@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
101 lines (100 loc) • 3.37 kB
JavaScript
import { NotFoundException } from "../../exceptions/runtime.exceptions.js";
import { BaseService } from "./base.service.js";
import { Role } from "../../entities/role.entity.js";
import { ROLES, ROLE_PERMS } from "../../constants/authorization.constants.js";
import "../../entities/index.js";
import { RoleDto } from "../interfaces/role.dto.js";
//#region src/services/orm/role.service.ts
var RoleService = class extends BaseService(Role, RoleDto) {
constructor(typeormService, appDefaultRole, appDefaultRoleNoLogin, settingsStore) {
super(typeormService);
this.appDefaultRole = appDefaultRole;
this.appDefaultRoleNoLogin = appDefaultRoleNoLogin;
this.settingsStore = settingsStore;
}
_roles = [];
get roles() {
return this._roles;
}
async getAppDefaultRole() {
if (await this.settingsStore.getLoginRequired()) return this.appDefaultRole;
return this.appDefaultRoleNoLogin;
}
async getAppDefaultRoleNames() {
if (!this._roles?.length) await this.syncRoles();
return [await this.getAppDefaultRole()];
}
toDto(role) {
return {
id: role.id,
name: role.name
};
}
getRolesPermissions(roleNames) {
let permissions = [];
if (!roleNames?.length) return [];
for (const roleName of roleNames) {
const rolePermissions = this.getRolePermissions(roleName);
permissions = [...new Set([...permissions, ...rolePermissions])];
}
return permissions;
}
/**
* Checks if any of the required roles are in the assigned roles
* @param requiredRoles list of role names that would grant access
* @param assignedRoles user's assigned role names
* @param subset if true, any match grants access (OR); if false, all must match (AND)
*/
authorizeRoles(requiredRoles, assignedRoles, subset) {
if (!requiredRoles?.length) return true;
let isAuthorized = !subset;
for (const requiredRole of requiredRoles) {
const result = this.authorizeRole(requiredRole, assignedRoles);
isAuthorized = subset ? isAuthorized || result : isAuthorized && result;
}
return isAuthorized;
}
authorizeRole(requiredRole, assignedRoles) {
return assignedRoles.includes(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 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 NotFoundException(`Role by provided name was not found`);
return role;
}
getRolePermissions(roleName) {
if (!roleName) return [];
return ROLE_PERMS[roleName] ?? [];
}
roleIdsToRoleNames(roleIds) {
return roleIds.map((roleId) => {
return this._roles.find((r) => r.id === roleId)?.name;
}).filter((name) => name !== void 0);
}
async getSynchronizedRoleByName(roleName) {
if (!this._roles?.length) await this.syncRoles();
return this.getRoleByName(roleName);
}
async syncRoles() {
this._roles = [];
for (const roleName of Object.values(ROLES)) {
const storedRole = await this.repository.findOneBy({ name: roleName });
if (storedRole) this._roles.push(storedRole);
else {
const newRole = await this.create({ name: roleName });
this._roles.push(newRole);
}
}
}
};
//#endregion
export { RoleService };
//# sourceMappingURL=role.service.js.map