Spaces:
Runtime error
Runtime error
abhishek-akbari01 commited on
Commit ·
0b92d37
1
Parent(s): f756313
fix comments issue
Browse files- src/controllers/permission.controller.ts +0 -41
- src/controllers/roles.controller.ts +44 -1
- src/db/seeders/20240829033937-seed-permissions.js +12 -22
- src/middlewares/checkPermissions.ts +1 -5
- src/routes/auditLogs.routes.ts +2 -2
- src/routes/errorLog.routes.ts +1 -1
- src/routes/invoice.routes.ts +1 -1
- src/routes/permission.routes.ts +1 -67
- src/routes/roles.routes.ts +68 -1
- src/routes/settings.routes.ts +3 -3
- src/routes/users.routes.ts +4 -4
- src/shared/interfaces/rolePermission.interface.ts +8 -13
src/controllers/permission.controller.ts
CHANGED
|
@@ -31,44 +31,3 @@ export const createPermission = async (req: Request, res: Response) => {
|
|
| 31 |
});
|
| 32 |
}
|
| 33 |
};
|
| 34 |
-
|
| 35 |
-
export const assignPermissionsToRole = async (req: Request, res: Response) => {
|
| 36 |
-
try {
|
| 37 |
-
const { roleId } = req.params;
|
| 38 |
-
const { permission_ids } = req.body;
|
| 39 |
-
|
| 40 |
-
// Validate if the role exists
|
| 41 |
-
const role = await Role.findByPk(roleId);
|
| 42 |
-
if (!role) {
|
| 43 |
-
return res.status(404).json({ error: 'Role not found' });
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
// Fetch all permissions that match the given IDs
|
| 47 |
-
const existingPermissions = await Permission.findAll({ where: { id: permission_ids } });
|
| 48 |
-
|
| 49 |
-
// Extract IDs of existing permissions
|
| 50 |
-
const existingPermissionIds = existingPermissions.map(permission => permission.id);
|
| 51 |
-
|
| 52 |
-
// Identify non-existent permissions
|
| 53 |
-
const nonExistentPermissionIds = permission_ids.filter((id: number) => !existingPermissionIds.includes(id));
|
| 54 |
-
|
| 55 |
-
// Assign only the existing permissions to the role
|
| 56 |
-
const rolePermissions = await Promise.all(
|
| 57 |
-
existingPermissionIds.map(async (permission_id: number) => {
|
| 58 |
-
return await RolePermission.create({ role_id: parseInt(roleId, 10), permission_id });
|
| 59 |
-
})
|
| 60 |
-
);
|
| 61 |
-
|
| 62 |
-
return res.status(201).json({
|
| 63 |
-
message: 'Permissions processed successfully',
|
| 64 |
-
assignedPermissions: rolePermissions,
|
| 65 |
-
nonExistentPermissions: nonExistentPermissionIds,
|
| 66 |
-
});
|
| 67 |
-
|
| 68 |
-
} catch (error) {
|
| 69 |
-
logger.error('Error assigning permissions to role:', error);
|
| 70 |
-
return res.status(500).json({
|
| 71 |
-
error: 'Error while assigning permissions to the role.',
|
| 72 |
-
});
|
| 73 |
-
}
|
| 74 |
-
};
|
|
|
|
| 31 |
});
|
| 32 |
}
|
| 33 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/controllers/roles.controller.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { Request, Response } from 'express';
|
|
| 2 |
import Role from '../models/roles';
|
| 3 |
import { FindOptions, Op } from 'sequelize';
|
| 4 |
import { logger } from '../utils/logger';
|
|
|
|
|
|
|
| 5 |
|
| 6 |
const buildRoleWhereClause = (filter: Record<string, any>) => {
|
| 7 |
|
|
@@ -89,4 +91,45 @@ const getAllRoles = async (req: Request, res: Response) => {
|
|
| 89 |
}
|
| 90 |
}
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import Role from '../models/roles';
|
| 3 |
import { FindOptions, Op } from 'sequelize';
|
| 4 |
import { logger } from '../utils/logger';
|
| 5 |
+
import Permission from '../models/permissions';
|
| 6 |
+
import RolePermission from '../models/rolePermissions';
|
| 7 |
|
| 8 |
const buildRoleWhereClause = (filter: Record<string, any>) => {
|
| 9 |
|
|
|
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
+
const assignPermissionsToRole = async (req: Request, res: Response) => {
|
| 95 |
+
try {
|
| 96 |
+
const { roleId } = req.params;
|
| 97 |
+
const { permission_ids } = req.body;
|
| 98 |
+
|
| 99 |
+
// Validate if the role exists
|
| 100 |
+
const role = await Role.findByPk(roleId);
|
| 101 |
+
if (!role) {
|
| 102 |
+
return res.status(404).json({ error: 'Role not found' });
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
// Fetch all permissions that match the given IDs
|
| 106 |
+
const existingPermissions = await Permission.findAll({ where: { id: permission_ids } });
|
| 107 |
+
|
| 108 |
+
// Extract IDs of existing permissions
|
| 109 |
+
const existingPermissionIds = existingPermissions.map(permission => permission.id);
|
| 110 |
+
|
| 111 |
+
// Identify non-existent permissions
|
| 112 |
+
const nonExistentPermissionIds = permission_ids.filter((id: number) => !existingPermissionIds.includes(id));
|
| 113 |
+
|
| 114 |
+
// Assign only the existing permissions to the role
|
| 115 |
+
const rolePermissions = await Promise.all(
|
| 116 |
+
existingPermissionIds.map(async (permission_id: number) => {
|
| 117 |
+
return await RolePermission.create({ role_id: parseInt(roleId, 10), permission_id });
|
| 118 |
+
})
|
| 119 |
+
);
|
| 120 |
+
|
| 121 |
+
return res.status(201).json({
|
| 122 |
+
message: 'Permissions processed successfully',
|
| 123 |
+
assignedPermissions: rolePermissions,
|
| 124 |
+
nonExistentPermissions: nonExistentPermissionIds,
|
| 125 |
+
});
|
| 126 |
+
|
| 127 |
+
} catch (error) {
|
| 128 |
+
logger.error('Error assigning permissions to role:', error);
|
| 129 |
+
return res.status(500).json({
|
| 130 |
+
error: 'Error while assigning permissions to the role.',
|
| 131 |
+
});
|
| 132 |
+
}
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
export { getAllRoles,assignPermissionsToRole }
|
src/db/seeders/20240829033937-seed-permissions.js
CHANGED
|
@@ -7,23 +7,18 @@ module.exports = {
|
|
| 7 |
{ permission_name: 'CREATE_USER', created_at: new Date(), updated_at: new Date() },
|
| 8 |
{ permission_name: 'CREATE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 9 |
{ permission_name: 'GET_ALL_INVOICES', created_at: new Date(), updated_at: new Date() },
|
| 10 |
-
{ permission_name: '
|
| 11 |
{ permission_name: 'UPDATE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 12 |
{ permission_name: 'DELETE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 13 |
{ permission_name: 'APPROVE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 14 |
-
{ permission_name: '
|
| 15 |
-
{ permission_name: 'GET_USER_BY_ID', created_at: new Date(), updated_at: new Date() },
|
| 16 |
{ permission_name: 'GET_ALL_USERS', created_at: new Date(), updated_at: new Date() },
|
| 17 |
-
{ permission_name: '
|
| 18 |
-
{ permission_name: '
|
| 19 |
-
{ permission_name: 'GET_AUDIT_LOGS', created_at: new Date(), updated_at: new Date() },
|
| 20 |
-
{ permission_name: 'GET_AUDIT_LOG_BY_ID', created_at: new Date(), updated_at: new Date() },
|
| 21 |
{ permission_name: 'GET_ERROR_LOGS', created_at: new Date(), updated_at: new Date() },
|
| 22 |
-
{ permission_name: 'GET_ERROR_LOG_BY_ID', created_at: new Date(), updated_at: new Date() },
|
| 23 |
{ permission_name: 'GET_ALL_ROLES', created_at: new Date(), updated_at: new Date() },
|
| 24 |
-
{ permission_name: '
|
| 25 |
-
{ permission_name: '
|
| 26 |
-
{ permission_name: 'UPDATE_SETTING_BY_ID', created_at: new Date(), updated_at: new Date() },
|
| 27 |
{ permission_name: 'GET_INVOICE_ACTIVITY_LOGS', created_at: new Date(), updated_at: new Date() },
|
| 28 |
{ permission_name: 'CREATE_PERMISSION', created_at: new Date(), updated_at: new Date() },
|
| 29 |
{ permission_name: 'ASSIGN_PERMISSIONS_TO_ROLE', created_at: new Date(), updated_at: new Date() },
|
|
@@ -38,23 +33,18 @@ module.exports = {
|
|
| 38 |
'CREATE_USER',
|
| 39 |
'CREATE_INVOICE',
|
| 40 |
'GET_ALL_INVOICES',
|
| 41 |
-
'
|
| 42 |
'UPDATE_INVOICE',
|
| 43 |
'DELETE_INVOICE',
|
| 44 |
'APPROVE_INVOICE',
|
| 45 |
-
'
|
| 46 |
-
'GET_USER_BY_ID',
|
| 47 |
'GET_ALL_USERS',
|
| 48 |
-
'
|
| 49 |
-
'
|
| 50 |
-
'GET_AUDIT_LOGS',
|
| 51 |
-
'GET_AUDIT_LOG_BY_ID',
|
| 52 |
'GET_ERROR_LOGS',
|
| 53 |
-
'GET_ERROR_LOG_BY_ID',
|
| 54 |
'GET_ALL_ROLES',
|
| 55 |
-
'
|
| 56 |
-
'
|
| 57 |
-
'UPDATE_SETTING_BY_ID',
|
| 58 |
'GET_INVOICE_ACTIVITY_LOGS',
|
| 59 |
'CREATE_PERMISSION',
|
| 60 |
'ASSIGN_PERMISSIONS_TO_ROLE',
|
|
|
|
| 7 |
{ permission_name: 'CREATE_USER', created_at: new Date(), updated_at: new Date() },
|
| 8 |
{ permission_name: 'CREATE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 9 |
{ permission_name: 'GET_ALL_INVOICES', created_at: new Date(), updated_at: new Date() },
|
| 10 |
+
{ permission_name: 'GET_SINGLE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 11 |
{ permission_name: 'UPDATE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 12 |
{ permission_name: 'DELETE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 13 |
{ permission_name: 'APPROVE_INVOICE', created_at: new Date(), updated_at: new Date() },
|
| 14 |
+
{ permission_name: 'GET_SINGLE_USER', created_at: new Date(), updated_at: new Date() },
|
|
|
|
| 15 |
{ permission_name: 'GET_ALL_USERS', created_at: new Date(), updated_at: new Date() },
|
| 16 |
+
{ permission_name: 'UPDATE_SINGLE_USER', created_at: new Date(), updated_at: new Date() },
|
| 17 |
+
{ permission_name: 'DELETE_SINGLE_USER', created_at: new Date(), updated_at: new Date() },
|
|
|
|
|
|
|
| 18 |
{ permission_name: 'GET_ERROR_LOGS', created_at: new Date(), updated_at: new Date() },
|
|
|
|
| 19 |
{ permission_name: 'GET_ALL_ROLES', created_at: new Date(), updated_at: new Date() },
|
| 20 |
+
{ permission_name: 'GET_SINGLE_SETTING', created_at: new Date(), updated_at: new Date() },
|
| 21 |
+
{ permission_name: 'UPDATE_SINGLE_SETTING', created_at: new Date(), updated_at: new Date() },
|
|
|
|
| 22 |
{ permission_name: 'GET_INVOICE_ACTIVITY_LOGS', created_at: new Date(), updated_at: new Date() },
|
| 23 |
{ permission_name: 'CREATE_PERMISSION', created_at: new Date(), updated_at: new Date() },
|
| 24 |
{ permission_name: 'ASSIGN_PERMISSIONS_TO_ROLE', created_at: new Date(), updated_at: new Date() },
|
|
|
|
| 33 |
'CREATE_USER',
|
| 34 |
'CREATE_INVOICE',
|
| 35 |
'GET_ALL_INVOICES',
|
| 36 |
+
'GET_SINGLE_INVOICE',
|
| 37 |
'UPDATE_INVOICE',
|
| 38 |
'DELETE_INVOICE',
|
| 39 |
'APPROVE_INVOICE',
|
| 40 |
+
'GET_SINGLE_USER',
|
|
|
|
| 41 |
'GET_ALL_USERS',
|
| 42 |
+
'UPDATE_SINGLE_USER',
|
| 43 |
+
'DELETE_SINGLE_USER',
|
|
|
|
|
|
|
| 44 |
'GET_ERROR_LOGS',
|
|
|
|
| 45 |
'GET_ALL_ROLES',
|
| 46 |
+
'GET_SINGLE_SETTING',
|
| 47 |
+
'UPDATE_SINGLE_SETTING',
|
|
|
|
| 48 |
'GET_INVOICE_ACTIVITY_LOGS',
|
| 49 |
'CREATE_PERMISSION',
|
| 50 |
'ASSIGN_PERMISSIONS_TO_ROLE',
|
src/middlewares/checkPermissions.ts
CHANGED
|
@@ -8,10 +8,6 @@ export const checkPermission = (requiredPermission: string) => {
|
|
| 8 |
try {
|
| 9 |
const userRoleId = req?.user?.role_id;
|
| 10 |
|
| 11 |
-
if (userRoleId === 1) {
|
| 12 |
-
return next();
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
const hasPermission = await RolePermission.findOne({
|
| 16 |
where: {
|
| 17 |
role_id: userRoleId,
|
|
@@ -28,7 +24,7 @@ export const checkPermission = (requiredPermission: string) => {
|
|
| 28 |
if (!hasPermission) {
|
| 29 |
return res
|
| 30 |
.status(403)
|
| 31 |
-
.json({ message: 'Forbidden: You do not have permission to access this
|
| 32 |
}
|
| 33 |
next();
|
| 34 |
} catch (error) {
|
|
|
|
| 8 |
try {
|
| 9 |
const userRoleId = req?.user?.role_id;
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
const hasPermission = await RolePermission.findOne({
|
| 12 |
where: {
|
| 13 |
role_id: userRoleId,
|
|
|
|
| 24 |
if (!hasPermission) {
|
| 25 |
return res
|
| 26 |
.status(403)
|
| 27 |
+
.json({ message: 'Forbidden: You do not have permission to access this module' });
|
| 28 |
}
|
| 29 |
next();
|
| 30 |
} catch (error) {
|
src/routes/auditLogs.routes.ts
CHANGED
|
@@ -71,7 +71,7 @@ auditLogRouter.use(jwtMiddleware);
|
|
| 71 |
* 500:
|
| 72 |
* description: Internal server error
|
| 73 |
*/
|
| 74 |
-
auditLogRouter.get("/",
|
| 75 |
|
| 76 |
/**
|
| 77 |
* @swagger
|
|
@@ -95,6 +95,6 @@ auditLogRouter.get("/", checkPermission(Permission.GET_AUDIT_LOGS), getAuditLogs
|
|
| 95 |
* 500:
|
| 96 |
* description: Internal server error
|
| 97 |
*/
|
| 98 |
-
auditLogRouter.get("/:id",
|
| 99 |
|
| 100 |
export default auditLogRouter;
|
|
|
|
| 71 |
* 500:
|
| 72 |
* description: Internal server error
|
| 73 |
*/
|
| 74 |
+
auditLogRouter.get("/", getAuditLogs);
|
| 75 |
|
| 76 |
/**
|
| 77 |
* @swagger
|
|
|
|
| 95 |
* 500:
|
| 96 |
* description: Internal server error
|
| 97 |
*/
|
| 98 |
+
auditLogRouter.get("/:id", getAuditLogById);
|
| 99 |
|
| 100 |
export default auditLogRouter;
|
src/routes/errorLog.routes.ts
CHANGED
|
@@ -137,6 +137,6 @@ errorLogRouter.get("/", checkPermission(Permission.GET_ERROR_LOGS), getErrorLogs
|
|
| 137 |
* 500:
|
| 138 |
* description: Error fetching error log
|
| 139 |
*/
|
| 140 |
-
errorLogRouter.get("/:id",
|
| 141 |
|
| 142 |
export default errorLogRouter;
|
|
|
|
| 137 |
* 500:
|
| 138 |
* description: Error fetching error log
|
| 139 |
*/
|
| 140 |
+
errorLogRouter.get("/:id", getErrorLogById);
|
| 141 |
|
| 142 |
export default errorLogRouter;
|
src/routes/invoice.routes.ts
CHANGED
|
@@ -257,7 +257,7 @@ invoiceRouter.get('/', checkPermission(Permission.GET_ALL_INVOICES), getAllInvoi
|
|
| 257 |
* 500:
|
| 258 |
* description: Error fetching invoice details
|
| 259 |
*/
|
| 260 |
-
invoiceRouter.get('/:id', checkPermission(Permission.
|
| 261 |
|
| 262 |
/**
|
| 263 |
* @swagger
|
|
|
|
| 257 |
* 500:
|
| 258 |
* description: Error fetching invoice details
|
| 259 |
*/
|
| 260 |
+
invoiceRouter.get('/:id', checkPermission(Permission.GET_SINGLE_INVOICE), getInvoiceById);
|
| 261 |
|
| 262 |
/**
|
| 263 |
* @swagger
|
src/routes/permission.routes.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import express from "express";
|
| 2 |
import { jwtMiddleware } from '../middlewares/authMiddleware';
|
| 3 |
-
import {
|
| 4 |
import { checkPermission } from "../middlewares/checkPermissions";
|
| 5 |
import { Permission } from "../shared/interfaces/rolePermission.interface";
|
| 6 |
|
|
@@ -62,70 +62,4 @@ permissionRouter.use(jwtMiddleware);
|
|
| 62 |
*/
|
| 63 |
permissionRouter.post('/', checkPermission(Permission.CREATE_PERMISSION), createPermission);
|
| 64 |
|
| 65 |
-
/**
|
| 66 |
-
* @swagger
|
| 67 |
-
* components:
|
| 68 |
-
* schemas:
|
| 69 |
-
* RolePermission:
|
| 70 |
-
* type: object
|
| 71 |
-
* required:
|
| 72 |
-
* - role_id
|
| 73 |
-
* - permission_id
|
| 74 |
-
* properties:
|
| 75 |
-
* id:
|
| 76 |
-
* type: integer
|
| 77 |
-
* description: The auto-generated id of the role-permission relationship
|
| 78 |
-
* role_id:
|
| 79 |
-
* type: integer
|
| 80 |
-
* description: The ID of the role
|
| 81 |
-
* permission_id:
|
| 82 |
-
* type: integer
|
| 83 |
-
* description: The ID of the permission
|
| 84 |
-
* example:
|
| 85 |
-
* id: 1
|
| 86 |
-
* role_id: 2
|
| 87 |
-
* permission_id: 3
|
| 88 |
-
*
|
| 89 |
-
* /api/permissions/assignPermission/{roleId}:
|
| 90 |
-
* post:
|
| 91 |
-
* summary: Assign permissions to a role
|
| 92 |
-
* tags: [Roles, Permissions]
|
| 93 |
-
* parameters:
|
| 94 |
-
* - in: path
|
| 95 |
-
* name: roleId
|
| 96 |
-
* schema:
|
| 97 |
-
* type: integer
|
| 98 |
-
* required: true
|
| 99 |
-
* description: The ID of the role
|
| 100 |
-
* requestBody:
|
| 101 |
-
* required: true
|
| 102 |
-
* content:
|
| 103 |
-
* application/json:
|
| 104 |
-
* schema:
|
| 105 |
-
* type: object
|
| 106 |
-
* properties:
|
| 107 |
-
* permission_ids:
|
| 108 |
-
* type: array
|
| 109 |
-
* items:
|
| 110 |
-
* type: integer
|
| 111 |
-
* example:
|
| 112 |
-
* permission_ids: [1, 2, 3]
|
| 113 |
-
* responses:
|
| 114 |
-
* 201:
|
| 115 |
-
* description: Permissions assigned successfully
|
| 116 |
-
* content:
|
| 117 |
-
* application/json:
|
| 118 |
-
* schema:
|
| 119 |
-
* type: array
|
| 120 |
-
* items:
|
| 121 |
-
* $ref: '#/components/schemas/RolePermission'
|
| 122 |
-
* 400:
|
| 123 |
-
* description: Validation errors
|
| 124 |
-
* 404:
|
| 125 |
-
* description: Role or Permission not found
|
| 126 |
-
* 500:
|
| 127 |
-
* description: Error while assigning permissions
|
| 128 |
-
*/
|
| 129 |
-
permissionRouter.post('/assignPermission/:roleId', checkPermission(Permission.ASSIGN_PERMISSIONS_TO_ROLE), assignPermissionsToRole);
|
| 130 |
-
|
| 131 |
export default permissionRouter;
|
|
|
|
| 1 |
import express from "express";
|
| 2 |
import { jwtMiddleware } from '../middlewares/authMiddleware';
|
| 3 |
+
import { createPermission } from "../controllers/permission.controller";
|
| 4 |
import { checkPermission } from "../middlewares/checkPermissions";
|
| 5 |
import { Permission } from "../shared/interfaces/rolePermission.interface";
|
| 6 |
|
|
|
|
| 62 |
*/
|
| 63 |
permissionRouter.post('/', checkPermission(Permission.CREATE_PERMISSION), createPermission);
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
export default permissionRouter;
|
src/routes/roles.routes.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
-
import { getAllRoles } from '../controllers/roles.controller';
|
| 3 |
import { jwtMiddleware } from '../middlewares/authMiddleware';
|
| 4 |
import { checkPermission } from '../middlewares/checkPermissions';
|
| 5 |
import { Permission } from '../shared/interfaces/rolePermission.interface';
|
|
@@ -116,4 +116,71 @@ rolesRouter.use(jwtMiddleware);
|
|
| 116 |
*/
|
| 117 |
rolesRouter.get('/', checkPermission(Permission.GET_ALL_ROLES), getAllRoles);
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
export default rolesRouter;
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
+
import { assignPermissionsToRole, getAllRoles } from '../controllers/roles.controller';
|
| 3 |
import { jwtMiddleware } from '../middlewares/authMiddleware';
|
| 4 |
import { checkPermission } from '../middlewares/checkPermissions';
|
| 5 |
import { Permission } from '../shared/interfaces/rolePermission.interface';
|
|
|
|
| 116 |
*/
|
| 117 |
rolesRouter.get('/', checkPermission(Permission.GET_ALL_ROLES), getAllRoles);
|
| 118 |
|
| 119 |
+
/**
|
| 120 |
+
* @swagger
|
| 121 |
+
* components:
|
| 122 |
+
* schemas:
|
| 123 |
+
* RolePermission:
|
| 124 |
+
* type: object
|
| 125 |
+
* required:
|
| 126 |
+
* - role_id
|
| 127 |
+
* - permission_id
|
| 128 |
+
* properties:
|
| 129 |
+
* id:
|
| 130 |
+
* type: integer
|
| 131 |
+
* description: The auto-generated id of the role-permission relationship
|
| 132 |
+
* role_id:
|
| 133 |
+
* type: integer
|
| 134 |
+
* description: The ID of the role
|
| 135 |
+
* permission_id:
|
| 136 |
+
* type: integer
|
| 137 |
+
* description: The ID of the permission
|
| 138 |
+
* example:
|
| 139 |
+
* id: 1
|
| 140 |
+
* role_id: 2
|
| 141 |
+
* permission_id: 3
|
| 142 |
+
*
|
| 143 |
+
* /api/permissions/assignPermission/{roleId}:
|
| 144 |
+
* post:
|
| 145 |
+
* summary: Assign permissions to a role
|
| 146 |
+
* tags: [Roles, Permissions]
|
| 147 |
+
* parameters:
|
| 148 |
+
* - in: path
|
| 149 |
+
* name: roleId
|
| 150 |
+
* schema:
|
| 151 |
+
* type: integer
|
| 152 |
+
* required: true
|
| 153 |
+
* description: The ID of the role
|
| 154 |
+
* requestBody:
|
| 155 |
+
* required: true
|
| 156 |
+
* content:
|
| 157 |
+
* application/json:
|
| 158 |
+
* schema:
|
| 159 |
+
* type: object
|
| 160 |
+
* properties:
|
| 161 |
+
* permission_ids:
|
| 162 |
+
* type: array
|
| 163 |
+
* items:
|
| 164 |
+
* type: integer
|
| 165 |
+
* example:
|
| 166 |
+
* permission_ids: [1, 2, 3]
|
| 167 |
+
* responses:
|
| 168 |
+
* 201:
|
| 169 |
+
* description: Permissions assigned successfully
|
| 170 |
+
* content:
|
| 171 |
+
* application/json:
|
| 172 |
+
* schema:
|
| 173 |
+
* type: array
|
| 174 |
+
* items:
|
| 175 |
+
* $ref: '#/components/schemas/RolePermission'
|
| 176 |
+
* 400:
|
| 177 |
+
* description: Validation errors
|
| 178 |
+
* 404:
|
| 179 |
+
* description: Role or Permission not found
|
| 180 |
+
* 500:
|
| 181 |
+
* description: Error while assigning permissions
|
| 182 |
+
*/
|
| 183 |
+
rolesRouter.post('/assignPermission/:roleId', checkPermission(Permission.ASSIGN_PERMISSIONS_TO_ROLE), assignPermissionsToRole);
|
| 184 |
+
|
| 185 |
+
|
| 186 |
export default rolesRouter;
|
src/routes/settings.routes.ts
CHANGED
|
@@ -123,7 +123,7 @@ settingRouter.use(jwtMiddleware);
|
|
| 123 |
* 500:
|
| 124 |
* description: Error fetching settings
|
| 125 |
*/
|
| 126 |
-
settingRouter.get('/',
|
| 127 |
|
| 128 |
/**
|
| 129 |
* @swagger
|
|
@@ -150,7 +150,7 @@ settingRouter.get('/', checkPermission(Permission.GET_SETTINGS), getSettings);
|
|
| 150 |
* 500:
|
| 151 |
* description: Error while fetching setting
|
| 152 |
*/
|
| 153 |
-
settingRouter.get('/:id', checkPermission(Permission.
|
| 154 |
|
| 155 |
/**
|
| 156 |
* @swagger
|
|
@@ -193,6 +193,6 @@ settingRouter.get('/:id', checkPermission(Permission.GET_SETTING_BY_ID), getSett
|
|
| 193 |
* 500:
|
| 194 |
* description: Error updating setting
|
| 195 |
*/
|
| 196 |
-
settingRouter.put('/:id', checkPermission(Permission.
|
| 197 |
|
| 198 |
export default settingRouter;
|
|
|
|
| 123 |
* 500:
|
| 124 |
* description: Error fetching settings
|
| 125 |
*/
|
| 126 |
+
settingRouter.get('/', getSettings);
|
| 127 |
|
| 128 |
/**
|
| 129 |
* @swagger
|
|
|
|
| 150 |
* 500:
|
| 151 |
* description: Error while fetching setting
|
| 152 |
*/
|
| 153 |
+
settingRouter.get('/:id', checkPermission(Permission.GET_SINGLE_SETTING), getSettingById);
|
| 154 |
|
| 155 |
/**
|
| 156 |
* @swagger
|
|
|
|
| 193 |
* 500:
|
| 194 |
* description: Error updating setting
|
| 195 |
*/
|
| 196 |
+
settingRouter.put('/:id', checkPermission(Permission.UPDATE_SINGLE_SETTING), updateSettingById);
|
| 197 |
|
| 198 |
export default settingRouter;
|
src/routes/users.routes.ts
CHANGED
|
@@ -120,7 +120,7 @@ userRouter.post("/",checkPermission(Permission.CREATE_USER), validateUser, handl
|
|
| 120 |
* 500:
|
| 121 |
* description: Error while fetching User details
|
| 122 |
*/
|
| 123 |
-
userRouter.get("/me",
|
| 124 |
|
| 125 |
/**
|
| 126 |
* @swagger
|
|
@@ -147,7 +147,7 @@ userRouter.get("/me", checkPermission(Permission.GET_CURRENT_USER), getCurrentUs
|
|
| 147 |
* 500:
|
| 148 |
* description: Error while fetching User details
|
| 149 |
*/
|
| 150 |
-
userRouter.get("/:id", checkPermission(Permission.
|
| 151 |
|
| 152 |
/**
|
| 153 |
* @swagger
|
|
@@ -265,7 +265,7 @@ userRouter.get("/", checkPermission(Permission.GET_ALL_USERS), getAllUsers);
|
|
| 265 |
* 500:
|
| 266 |
* description: Internal server error
|
| 267 |
*/
|
| 268 |
-
userRouter.put("/:id", checkPermission(Permission.
|
| 269 |
|
| 270 |
/**
|
| 271 |
* @swagger
|
|
@@ -288,7 +288,7 @@ userRouter.put("/:id", checkPermission(Permission.UPDATE_USER_BY_ID), validateUs
|
|
| 288 |
* 500:
|
| 289 |
* description: Error while deleting User
|
| 290 |
*/
|
| 291 |
-
userRouter.delete("/:id", checkPermission(Permission.
|
| 292 |
|
| 293 |
|
| 294 |
export default userRouter;
|
|
|
|
| 120 |
* 500:
|
| 121 |
* description: Error while fetching User details
|
| 122 |
*/
|
| 123 |
+
userRouter.get("/me", getCurrentUser);
|
| 124 |
|
| 125 |
/**
|
| 126 |
* @swagger
|
|
|
|
| 147 |
* 500:
|
| 148 |
* description: Error while fetching User details
|
| 149 |
*/
|
| 150 |
+
userRouter.get("/:id", checkPermission(Permission.GET_SINGLE_USER), getUserById);
|
| 151 |
|
| 152 |
/**
|
| 153 |
* @swagger
|
|
|
|
| 265 |
* 500:
|
| 266 |
* description: Internal server error
|
| 267 |
*/
|
| 268 |
+
userRouter.put("/:id", checkPermission(Permission.UPDATE_SINGLE_USER), validateUserUpdate, handleValidationErrors, updateUserById);
|
| 269 |
|
| 270 |
/**
|
| 271 |
* @swagger
|
|
|
|
| 288 |
* 500:
|
| 289 |
* description: Error while deleting User
|
| 290 |
*/
|
| 291 |
+
userRouter.delete("/:id", checkPermission(Permission.DELETE_SINGLE_USER), deleteUserById);
|
| 292 |
|
| 293 |
|
| 294 |
export default userRouter;
|
src/shared/interfaces/rolePermission.interface.ts
CHANGED
|
@@ -2,29 +2,24 @@ export interface RolePermissionInterface {
|
|
| 2 |
id?: number;
|
| 3 |
role_id: number;
|
| 4 |
permission_id: number;
|
| 5 |
-
|
| 6 |
-
|
| 7 |
export const Permission = {
|
| 8 |
"CREATE_USER": "CREATE_USER",
|
| 9 |
"CREATE_INVOICE": "CREATE_INVOICE",
|
| 10 |
"GET_ALL_INVOICES": "GET_ALL_INVOICES",
|
| 11 |
-
"
|
| 12 |
"UPDATE_INVOICE": "UPDATE_INVOICE",
|
| 13 |
"DELETE_INVOICE": "DELETE_INVOICE",
|
| 14 |
"APPROVE_INVOICE": "APPROVE_INVOICE",
|
| 15 |
-
"
|
| 16 |
-
"GET_USER_BY_ID": "GET_USER_BY_ID",
|
| 17 |
"GET_ALL_USERS": "GET_ALL_USERS",
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"GET_AUDIT_LOGS": "GET_AUDIT_LOGS",
|
| 21 |
-
"GET_AUDIT_LOG_BY_ID": "GET_AUDIT_LOG_BY_ID",
|
| 22 |
"GET_ERROR_LOGS": "GET_ERROR_LOGS",
|
| 23 |
-
"GET_ERROR_LOG_BY_ID": "GET_ERROR_LOG_BY_ID",
|
| 24 |
"GET_ALL_ROLES": "GET_ALL_ROLES",
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"UPDATE_SETTING_BY_ID": "UPDATE_SETTING_BY_ID",
|
| 28 |
"GET_INVOICE_ACTIVITY_LOGS": "GET_INVOICE_ACTIVITY_LOGS",
|
| 29 |
"CREATE_PERMISSION": "CREATE_PERMISSION",
|
| 30 |
"ASSIGN_PERMISSIONS_TO_ROLE": "ASSIGN_PERMISSIONS_TO_ROLE"
|
|
|
|
| 2 |
id?: number;
|
| 3 |
role_id: number;
|
| 4 |
permission_id: number;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
export const Permission = {
|
| 8 |
"CREATE_USER": "CREATE_USER",
|
| 9 |
"CREATE_INVOICE": "CREATE_INVOICE",
|
| 10 |
"GET_ALL_INVOICES": "GET_ALL_INVOICES",
|
| 11 |
+
"GET_SINGLE_INVOICE": "GET_SINGLE_INVOICE",
|
| 12 |
"UPDATE_INVOICE": "UPDATE_INVOICE",
|
| 13 |
"DELETE_INVOICE": "DELETE_INVOICE",
|
| 14 |
"APPROVE_INVOICE": "APPROVE_INVOICE",
|
| 15 |
+
"GET_SINGLE_USER": "GET_SINGLE_USER",
|
|
|
|
| 16 |
"GET_ALL_USERS": "GET_ALL_USERS",
|
| 17 |
+
"UPDATE_SINGLE_USER": "UPDATE_SINGLE_USER",
|
| 18 |
+
"DELETE_SINGLE_USER": "DELETE_SINGLE_USER",
|
|
|
|
|
|
|
| 19 |
"GET_ERROR_LOGS": "GET_ERROR_LOGS",
|
|
|
|
| 20 |
"GET_ALL_ROLES": "GET_ALL_ROLES",
|
| 21 |
+
"GET_SINGLE_SETTING": "GET_SINGLE_SETTING",
|
| 22 |
+
"UPDATE_SINGLE_SETTING": "UPDATE_SINGLE_SETTING",
|
|
|
|
| 23 |
"GET_INVOICE_ACTIVITY_LOGS": "GET_INVOICE_ACTIVITY_LOGS",
|
| 24 |
"CREATE_PERMISSION": "CREATE_PERMISSION",
|
| 25 |
"ASSIGN_PERMISSIONS_TO_ROLE": "ASSIGN_PERMISSIONS_TO_ROLE"
|