rbac crud api
This commit is contained in:
@@ -76,15 +76,204 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.post('/api/rbac/getEntities', async (req, res) => {
|
|
||||||
// res.status(200).json(await service.get('rbacManager').getEntities());
|
|
||||||
})
|
|
||||||
|
|
||||||
app.post('/api/rbac/auths/create', async (req, res) => {
|
// =========================================================
|
||||||
const { auths } = req.body;
|
// 👤 AUTH
|
||||||
// await service.get('rbacManager').createAuths(auths);
|
// =========================================================
|
||||||
res.status(200).json({ ok: true });
|
|
||||||
})
|
app.post('/api/rbac/auth/create', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const user = await rbac.createAuth(req.body);
|
||||||
|
res.json(user);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.put('/api/rbac/auth/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.updateAuth(req.params.id, req.body);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete('/api/rbac/auth/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.deleteAuth(req.params.id);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 👥 GROUPS
|
||||||
|
// =========================================================
|
||||||
|
|
||||||
|
app.post('/api/rbac/group/create', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const group = await rbac.createGroup(req.body);
|
||||||
|
res.json(group);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.put('/api/rbac/group/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.updateGroup(req.params.id, req.body);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete('/api/rbac/group/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.deleteGroup(req.params.id);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 🔗 USER ↔ GROUP
|
||||||
|
// =========================================================
|
||||||
|
|
||||||
|
app.post('/api/rbac/group/add-user', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { authId, groupId } = req.body;
|
||||||
|
await rbac.addUserToGroup(authId, groupId);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/api/rbac/group/remove-user', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { authId, groupId } = req.body;
|
||||||
|
await rbac.removeUserFromGroup(authId, groupId);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 🎭 ROLES
|
||||||
|
// =========================================================
|
||||||
|
|
||||||
|
app.post('/api/rbac/role/get', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const role = await rbac.createRole(req.body);
|
||||||
|
res.json(role);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.put('/api/rbac/role/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.updateRole(req.params.id, req.body);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete('/api/rbac/role/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.deleteRole(req.params.id);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 🔗 ROLE ASSIGNMENTS
|
||||||
|
// =========================================================
|
||||||
|
|
||||||
|
app.post('/api/rbac/role/assign-user', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { authId, roleId } = req.body;
|
||||||
|
await rbac.assignRoleToUser(authId, roleId);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/api/rbac/role/assign-group', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { groupId, roleId } = req.body;
|
||||||
|
await rbac.assignRoleToGroup(groupId, roleId);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 🔐 PERMISSIONS
|
||||||
|
// =========================================================
|
||||||
|
|
||||||
|
app.post('/permission', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const perm = await rbac.createPermission(req.body);
|
||||||
|
res.json(perm);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.put('/permission/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.updatePermission(req.params.id, req.body);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete('/permission/:id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
await rbac.deletePermission(req.params.id);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 🔗 ROLE ↔ PERMISSION
|
||||||
|
// =========================================================
|
||||||
|
|
||||||
|
app.post('/api/rbac/role/add-permission', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { roleId, permissionId } = req.body;
|
||||||
|
await rbac.addPermissionToRole(roleId, permissionId);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/api/rbac/role/remove-permission', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { roleId, permissionId } = req.body;
|
||||||
|
await rbac.removePermissionFromRole(roleId, permissionId);
|
||||||
|
res.json({ ok: true });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.post('/api/plugins/activation', async (req, res) => {
|
app.post('/api/plugins/activation', async (req, res) => {
|
||||||
const { name, state } = req.body;
|
const { name, state } = req.body;
|
||||||
|
|||||||
@@ -237,416 +237,207 @@ class RBACManager {
|
|||||||
|
|
||||||
|
|
||||||
//#region CRUD
|
//#region CRUD
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 👤 AUTH CRUD
|
// 👤 AUTH CRUD
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
async createAuth(data) {
|
async createAuth(data) {
|
||||||
const Auth = this.db.get('authentication');
|
const Auth = this.db.get('authentication');
|
||||||
|
|
||||||
return await Auth.create({
|
return await Auth.create({
|
||||||
sAMAccountName: data.sAMAccountName,
|
sAMAccountName: data.sAMAccountName,
|
||||||
mail: data.mail,
|
mail: data.mail,
|
||||||
sn: data.sn,
|
sn: data.sn,
|
||||||
givenName: data.givenName,
|
givenName: data.givenName,
|
||||||
active: true
|
active: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateAuth(id, data) {
|
async updateAuth(id, data) {
|
||||||
const Auth = this.db.get('authentication');
|
const Auth = this.db.get('authentication');
|
||||||
|
|
||||||
return await Auth.update(data, {
|
return await Auth.update(data, {
|
||||||
where: { ObjectGUID: id }
|
where: { ObjectGUID: id }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteAuth(id) {
|
async deleteAuth(id) {
|
||||||
const Auth = this.db.get('authentication');
|
const Auth = this.db.get('authentication');
|
||||||
|
|
||||||
return await Auth.destroy({
|
return await Auth.destroy({
|
||||||
where: { ObjectGUID: id }
|
where: { ObjectGUID: id }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 👥 GROUP CRUD
|
// 👥 GROUP CRUD
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
async createGroup(data) {
|
async createGroup(data) {
|
||||||
const Group = this.db.get('groupsModel');
|
const Group = this.db.get('groupsModel');
|
||||||
|
|
||||||
return await Group.create({
|
return await Group.create({
|
||||||
Name: data.name,
|
Name: data.name,
|
||||||
Description: data.description || null
|
Description: data.description || null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateGroup(id, data) {
|
async updateGroup(id, data) {
|
||||||
const Group = this.db.get('groupsModel');
|
const Group = this.db.get('groupsModel');
|
||||||
|
|
||||||
return await Group.update(data, {
|
return await Group.update(data, {
|
||||||
where: { ObjectGUID: id }
|
where: { ObjectGUID: id }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteGroup(id) {
|
async deleteGroup(id) {
|
||||||
const Group = this.db.get('groupsModel');
|
const Group = this.db.get('groupsModel');
|
||||||
|
|
||||||
return await Group.destroy({
|
return await Group.destroy({
|
||||||
where: { ObjectGUID: id }
|
where: { ObjectGUID: id }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 🔗 AUTH ↔ GROUP RELATION
|
// 🔗 AUTH ↔ GROUP RELATION
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
async addUserToGroup(authId, groupId) {
|
async addUserToGroup(authId, groupId) {
|
||||||
const AuthGroups = this.db.get('authenticationGroupsModel');
|
const AuthGroups = this.db.get('authenticationGroupsModel');
|
||||||
|
|
||||||
return await AuthGroups.create({
|
return await AuthGroups.create({
|
||||||
Authentication_ObjectGUID: authId,
|
|
||||||
Group_ObjectGUID: groupId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async removeUserFromGroup(authId, groupId) {
|
|
||||||
const AuthGroups = this.db.get('authenticationGroupsModel');
|
|
||||||
|
|
||||||
return await AuthGroups.destroy({
|
|
||||||
where: {
|
|
||||||
Authentication_ObjectGUID: authId,
|
Authentication_ObjectGUID: authId,
|
||||||
Group_ObjectGUID: groupId
|
Group_ObjectGUID: groupId
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// =========================================================
|
async removeUserFromGroup(authId, groupId) {
|
||||||
// 🎭 ROLE CRUD
|
const AuthGroups = this.db.get('authenticationGroupsModel');
|
||||||
// =========================================================
|
|
||||||
|
|
||||||
async createRole(data) {
|
return await AuthGroups.destroy({
|
||||||
const Role = this.db.get('rolesModel');
|
where: {
|
||||||
|
Authentication_ObjectGUID: authId,
|
||||||
return await Role.create({
|
Group_ObjectGUID: groupId
|
||||||
Name: data.name,
|
}
|
||||||
Description: data.description || null
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async updateRole(id, data) {
|
|
||||||
const Role = this.db.get('rolesModel');
|
|
||||||
|
|
||||||
return await Role.update(data, {
|
|
||||||
where: { ID: id }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async deleteRole(id) {
|
|
||||||
const Role = this.db.get('rolesModel');
|
|
||||||
|
|
||||||
return await Role.destroy({
|
|
||||||
where: { ID: id }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// =========================================================
|
|
||||||
// 🔗 ROLE ASSIGNMENTS
|
|
||||||
// =========================================================
|
|
||||||
|
|
||||||
async assignRoleToUser(authId, roleId) {
|
|
||||||
const AuthRoles = this.db.get('authenticationRolesModel');
|
|
||||||
|
|
||||||
return await AuthRoles.create({
|
|
||||||
Authentication_ObjectGUID: authId,
|
|
||||||
Role_ID: roleId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async assignRoleToGroup(groupId, roleId) {
|
|
||||||
const GroupRoles = this.db.get('groupRolesModel');
|
|
||||||
|
|
||||||
return await GroupRoles.create({
|
|
||||||
Group_ObjectGUID: groupId,
|
|
||||||
Role_ID: roleId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async removeRoleFromUser(authId, roleId) {
|
|
||||||
const AuthRoles = this.db.get('authenticationRolesModel');
|
|
||||||
|
|
||||||
return await AuthRoles.destroy({
|
|
||||||
where: {
|
|
||||||
Authentication_ObjectGUID: authId,
|
|
||||||
Role_ID: roleId
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// =========================================================
|
|
||||||
// 🔐 PERMISSION CRUD
|
|
||||||
// =========================================================
|
|
||||||
|
|
||||||
async createPermission(data) {
|
|
||||||
const Permission = this.db.get('permissionModel');
|
|
||||||
|
|
||||||
return await Permission.create({
|
|
||||||
Scope: data.scope,
|
|
||||||
Resource: data.resource,
|
|
||||||
Action: data.action
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async updatePermission(id, data) {
|
|
||||||
const Permission = this.db.get('permissionModel');
|
|
||||||
|
|
||||||
return await Permission.update(data, {
|
|
||||||
where: { ID: id }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async deletePermission(id) {
|
|
||||||
const Permission = this.db.get('permissionModel');
|
|
||||||
|
|
||||||
return await Permission.destroy({
|
|
||||||
where: { ID: id }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// =========================================================
|
|
||||||
// 🔗 ROLE ↔ PERMISSION
|
|
||||||
// =========================================================
|
|
||||||
|
|
||||||
async addPermissionToRole(roleId, permissionId) {
|
|
||||||
const RolePerms = this.db.get('rolePermissionsModel');
|
|
||||||
|
|
||||||
return await RolePerms.create({
|
|
||||||
Role_ID: roleId,
|
|
||||||
Permission_ID: permissionId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async removePermissionFromRole(roleId, permissionId) {
|
|
||||||
const RolePerms = this.db.get('rolePermissionsModel');
|
|
||||||
|
|
||||||
return await RolePerms.destroy({
|
|
||||||
where: {
|
|
||||||
Role_ID: roleId,
|
|
||||||
Permission_ID: permissionId
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//#endregion
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const express = require('express');
|
|
||||||
|
|
||||||
function rbacRoutes(rbac) {
|
|
||||||
const router = express.Router();
|
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 👤 AUTH
|
// 🎭 ROLE CRUD
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
router.post('/auth/create', async (req, res) => {
|
async createRole(data) {
|
||||||
try {
|
const Role = this.db.get('rolesModel');
|
||||||
const user = await rbac.createAuth(req.body);
|
|
||||||
res.json(user);
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.put('/auth/:id', async (req, res) => {
|
return await Role.create({
|
||||||
try {
|
Name: data.name,
|
||||||
await rbac.updateAuth(req.params.id, req.body);
|
Description: data.description || null
|
||||||
res.json({ ok: true });
|
});
|
||||||
} catch (err) {
|
}
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/auth/:id', async (req, res) => {
|
async updateRole(id, data) {
|
||||||
try {
|
const Role = this.db.get('rolesModel');
|
||||||
await rbac.deleteAuth(req.params.id);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// =========================================================
|
return await Role.update(data, {
|
||||||
// 👥 GROUPS
|
where: { ID: id }
|
||||||
// =========================================================
|
});
|
||||||
|
}
|
||||||
|
|
||||||
router.post('/group', async (req, res) => {
|
async deleteRole(id) {
|
||||||
try {
|
const Role = this.db.get('rolesModel');
|
||||||
const group = await rbac.createGroup(req.body);
|
|
||||||
res.json(group);
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.put('/group/:id', async (req, res) => {
|
return await Role.destroy({
|
||||||
try {
|
where: { ID: id }
|
||||||
await rbac.updateGroup(req.params.id, req.body);
|
});
|
||||||
res.json({ ok: true });
|
}
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/group/:id', async (req, res) => {
|
|
||||||
try {
|
|
||||||
await rbac.deleteGroup(req.params.id);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// =========================================================
|
|
||||||
// 🔗 USER ↔ GROUP
|
|
||||||
// =========================================================
|
|
||||||
|
|
||||||
router.post('/group/add-user', async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { authId, groupId } = req.body;
|
|
||||||
await rbac.addUserToGroup(authId, groupId);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/group/remove-user', async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { authId, groupId } = req.body;
|
|
||||||
await rbac.removeUserFromGroup(authId, groupId);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// =========================================================
|
|
||||||
// 🎭 ROLES
|
|
||||||
// =========================================================
|
|
||||||
|
|
||||||
router.post('/role', async (req, res) => {
|
|
||||||
try {
|
|
||||||
const role = await rbac.createRole(req.body);
|
|
||||||
res.json(role);
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.put('/role/:id', async (req, res) => {
|
|
||||||
try {
|
|
||||||
await rbac.updateRole(req.params.id, req.body);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/role/:id', async (req, res) => {
|
|
||||||
try {
|
|
||||||
await rbac.deleteRole(req.params.id);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 🔗 ROLE ASSIGNMENTS
|
// 🔗 ROLE ASSIGNMENTS
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
router.post('/role/assign-user', async (req, res) => {
|
async assignRoleToUser(authId, roleId) {
|
||||||
try {
|
const AuthRoles = this.db.get('authenticationRolesModel');
|
||||||
const { authId, roleId } = req.body;
|
|
||||||
await rbac.assignRoleToUser(authId, roleId);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/role/assign-group', async (req, res) => {
|
return await AuthRoles.create({
|
||||||
try {
|
Authentication_ObjectGUID: authId,
|
||||||
const { groupId, roleId } = req.body;
|
Role_ID: roleId
|
||||||
await rbac.assignRoleToGroup(groupId, roleId);
|
});
|
||||||
res.json({ ok: true });
|
}
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
async assignRoleToGroup(groupId, roleId) {
|
||||||
}
|
const GroupRoles = this.db.get('groupRolesModel');
|
||||||
});
|
|
||||||
|
return await GroupRoles.create({
|
||||||
|
Group_ObjectGUID: groupId,
|
||||||
|
Role_ID: roleId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeRoleFromUser(authId, roleId) {
|
||||||
|
const AuthRoles = this.db.get('authenticationRolesModel');
|
||||||
|
|
||||||
|
return await AuthRoles.destroy({
|
||||||
|
where: {
|
||||||
|
Authentication_ObjectGUID: authId,
|
||||||
|
Role_ID: roleId
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 🔐 PERMISSIONS
|
// 🔐 PERMISSION CRUD
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
router.post('/permission', async (req, res) => {
|
async createPermission(data) {
|
||||||
try {
|
const Permission = this.db.get('permissionModel');
|
||||||
const perm = await rbac.createPermission(req.body);
|
|
||||||
res.json(perm);
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.put('/permission/:id', async (req, res) => {
|
return await Permission.create({
|
||||||
try {
|
Scope: data.scope,
|
||||||
await rbac.updatePermission(req.params.id, req.body);
|
Resource: data.resource,
|
||||||
res.json({ ok: true });
|
Action: data.action
|
||||||
} catch (err) {
|
});
|
||||||
res.status(500).json({ error: err.message });
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.delete('/permission/:id', async (req, res) => {
|
async updatePermission(id, data) {
|
||||||
try {
|
const Permission = this.db.get('permissionModel');
|
||||||
await rbac.deletePermission(req.params.id);
|
|
||||||
res.json({ ok: true });
|
return await Permission.update(data, {
|
||||||
} catch (err) {
|
where: { ID: id }
|
||||||
res.status(500).json({ error: err.message });
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
async deletePermission(id) {
|
||||||
|
const Permission = this.db.get('permissionModel');
|
||||||
|
|
||||||
|
return await Permission.destroy({
|
||||||
|
where: { ID: id }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// =========================================================
|
// =========================================================
|
||||||
// 🔗 ROLE ↔ PERMISSION
|
// 🔗 ROLE ↔ PERMISSION
|
||||||
// =========================================================
|
// =========================================================
|
||||||
|
|
||||||
router.post('/role/add-permission', async (req, res) => {
|
async addPermissionToRole(roleId, permissionId) {
|
||||||
try {
|
const RolePerms = this.db.get('rolePermissionsModel');
|
||||||
const { roleId, permissionId } = req.body;
|
|
||||||
await rbac.addPermissionToRole(roleId, permissionId);
|
|
||||||
res.json({ ok: true });
|
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post('/role/remove-permission', async (req, res) => {
|
return await RolePerms.create({
|
||||||
try {
|
Role_ID: roleId,
|
||||||
const { roleId, permissionId } = req.body;
|
Permission_ID: permissionId
|
||||||
await rbac.removePermissionFromRole(roleId, permissionId);
|
});
|
||||||
res.json({ ok: true });
|
}
|
||||||
} catch (err) {
|
|
||||||
res.status(500).json({ error: err.message });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return router;
|
async removePermissionFromRole(roleId, permissionId) {
|
||||||
}
|
const RolePerms = this.db.get('rolePermissionsModel');
|
||||||
|
|
||||||
module.exports = rbacRoutes;
|
return await RolePerms.destroy({
|
||||||
|
where: {
|
||||||
|
Role_ID: roleId,
|
||||||
|
Permission_ID: permissionId
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//#endregio
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = RBACManager;
|
module.exports = RBACManager;
|
||||||
Reference in New Issue
Block a user