Skip to content

Commit 387a681

Browse files
committed
role based auth added
1 parent dee6cc1 commit 387a681

File tree

8 files changed

+174
-15
lines changed

8 files changed

+174
-15
lines changed

backend/constants/PermissionData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module.exports = {
33
EDIT_USER: 2,
44
DELETE_USER: 3,
55
CREATE_USER: 4,
6-
VIEW_DOCTORS: 5
6+
VIEW_DOCTORS: 2
77
};

backend/logs/combined.log

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7971,3 +7971,120 @@
79717971
2024-11-10 19:40:04 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
79727972
2024-11-10 19:40:04 error: Error in checkPermission middleware: Error fetching permissions
79737973
2024-11-10 19:40:15 info: HTTPS Server running on port 3003,
7974+
2024-11-11 06:53:59 info: HTTPS Server running on port 3003,
7975+
2024-11-11 06:54:04 info: GET /api/doctor/getall
7976+
2024-11-11 06:54:04 error: Invalid token: jwt expired
7977+
2024-11-11 06:54:08 info: POST /api/auth/login
7978+
2024-11-11 06:54:09 info: User logged in: [email protected]
7979+
2024-11-11 06:54:22 info: GET /api/doctor/getall
7980+
2024-11-11 06:54:22 info: Token request verified for authMiddleware: function toString() { [native code] }
7981+
2024-11-11 06:54:22 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
7982+
2024-11-11 06:54:22 error: Error in checkPermission middleware: Error fetching permissions
7983+
2024-11-11 06:57:34 info: HTTPS Server running on port 3003,
7984+
2024-11-11 06:57:42 info: GET /api/doctor/getall
7985+
2024-11-11 06:57:42 info: Token request verified for authMiddleware: function toString() { [native code] }
7986+
2024-11-11 06:57:42 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
7987+
2024-11-11 06:57:42 error: Error in checkPermission middleware: Error fetching permissions
7988+
2024-11-11 07:00:27 info: HTTPS Server running on port 3003,
7989+
2024-11-11 07:00:34 info: GET /api/doctor/getall
7990+
2024-11-11 07:00:34 info: Token request verified for authMiddleware: function toString() { [native code] }
7991+
2024-11-11 07:00:34 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
7992+
2024-11-11 07:00:34 error: Error in checkPermission middleware: Error fetching permissions
7993+
2024-11-11 07:01:06 info: HTTPS Server running on port 3003,
7994+
2024-11-11 07:01:11 info: GET /api/doctor/getall
7995+
2024-11-11 07:01:11 info: Token request verified for authMiddleware: function toString() { [native code] }
7996+
2024-11-11 07:01:11 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
7997+
2024-11-11 07:01:11 error: Error in checkPermission middleware: Error fetching permissions
7998+
2024-11-11 07:02:16 info: HTTPS Server running on port 3003,
7999+
2024-11-11 07:02:20 info: GET /api/doctor/getall
8000+
2024-11-11 07:02:20 info: Token request verified for authMiddleware: function toString() { [native code] }
8001+
2024-11-11 07:02:20 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
8002+
2024-11-11 07:02:20 error: Error in checkPermission middleware: Error fetching permissions
8003+
2024-11-11 07:03:43 info: HTTPS Server running on port 3003,
8004+
2024-11-11 07:03:48 info: GET /api/doctor/getall
8005+
2024-11-11 07:03:48 info: Token request verified for authMiddleware: function toString() { [native code] }
8006+
2024-11-11 07:03:48 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
8007+
2024-11-11 07:03:48 error: Error in checkPermission middleware: Error fetching permissions
8008+
2024-11-11 07:04:41 info: HTTPS Server running on port 3003,
8009+
2024-11-11 07:04:46 info: GET /api/doctor/getall
8010+
2024-11-11 07:04:46 info: Token request verified for authMiddleware: function toString() { [native code] }
8011+
2024-11-11 07:04:46 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'permission_id')
8012+
2024-11-11 07:07:08 info: HTTPS Server running on port 3003,
8013+
2024-11-11 07:07:11 info: GET /api/doctor/getall
8014+
2024-11-11 07:07:11 info: Token request verified for authMiddleware: function toString() { [native code] }
8015+
2024-11-11 07:07:12 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'permission_id')
8016+
2024-11-11 07:07:51 info: HTTPS Server running on port 3003,
8017+
2024-11-11 07:08:09 info: HTTPS Server running on port 3003,
8018+
2024-11-11 07:08:13 info: GET /api/doctor/getall
8019+
2024-11-11 07:08:13 info: Token request verified for authMiddleware: function toString() { [native code] }
8020+
2024-11-11 07:08:13 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'permission_id')
8021+
2024-11-11 07:08:33 info: HTTPS Server running on port 3003,
8022+
2024-11-11 07:08:38 info: GET /api/doctor/getall
8023+
2024-11-11 07:08:38 info: Token request verified for authMiddleware: function toString() { [native code] }
8024+
2024-11-11 07:08:38 error: Error fetching permissions for Role ID 1: rolePermissions.forEach is not a function
8025+
2024-11-11 07:08:38 error: Error in checkPermission middleware: Error fetching permissions
8026+
2024-11-11 07:09:28 info: HTTPS Server running on port 3003,
8027+
2024-11-11 07:09:31 info: GET /api/doctor/getall
8028+
2024-11-11 07:09:31 info: Token request verified for authMiddleware: function toString() { [native code] }
8029+
2024-11-11 07:09:31 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8030+
2024-11-11 07:09:31 error: Error in checkPermission middleware: Error fetching permissions
8031+
2024-11-11 07:10:42 info: HTTPS Server running on port 3003,
8032+
2024-11-11 07:10:46 info: GET /api/doctor/getall
8033+
2024-11-11 07:10:46 info: Token request verified for authMiddleware: function toString() { [native code] }
8034+
2024-11-11 07:10:46 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8035+
2024-11-11 07:10:46 error: Error in checkPermission middleware: Error fetching permissions
8036+
2024-11-11 07:11:44 info: HTTPS Server running on port 3003,
8037+
2024-11-11 07:11:48 info: GET /api/doctor/getall
8038+
2024-11-11 07:11:48 info: Token request verified for authMiddleware: function toString() { [native code] }
8039+
2024-11-11 07:11:48 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8040+
2024-11-11 07:11:48 error: Error in checkPermission middleware: Error fetching permissions
8041+
2024-11-11 07:12:16 info: HTTPS Server running on port 3003,
8042+
2024-11-11 07:12:20 info: GET /api/doctor/getall
8043+
2024-11-11 07:12:20 info: Token request verified for authMiddleware: function toString() { [native code] }
8044+
2024-11-11 07:12:20 error: Error fetching permissions for Role ID 1: Unknown column 'Permission.role_id' in 'where clause'
8045+
2024-11-11 07:12:20 error: Error in checkPermission middleware: Error fetching permissions
8046+
2024-11-11 07:13:33 info: HTTPS Server running on port 3003,
8047+
2024-11-11 07:13:37 info: GET /api/doctor/getall
8048+
2024-11-11 07:13:37 info: Token request verified for authMiddleware: function toString() { [native code] }
8049+
2024-11-11 07:13:37 error: Error fetching permissions for Role ID 1: Unknown column 'Permission.role_id' in 'where clause'
8050+
2024-11-11 07:13:37 error: Error in checkPermission middleware: Error fetching permissions
8051+
2024-11-11 07:13:54 info: HTTPS Server running on port 3003,
8052+
2024-11-11 07:13:58 info: GET /api/doctor/getall
8053+
2024-11-11 07:13:58 info: Token request verified for authMiddleware: function toString() { [native code] }
8054+
2024-11-11 07:13:58 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8055+
2024-11-11 07:13:58 error: Error in checkPermission middleware: Error fetching permissions
8056+
2024-11-11 07:14:41 info: HTTPS Server running on port 3003,
8057+
2024-11-11 07:14:47 info: GET /api/doctor/getall
8058+
2024-11-11 07:14:47 info: Token request verified for authMiddleware: function toString() { [native code] }
8059+
2024-11-11 07:14:47 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
8060+
2024-11-11 07:14:47 error: Error in checkPermission middleware: Error fetching permissions
8061+
2024-11-11 07:15:07 info: HTTPS Server running on port 3003,
8062+
2024-11-11 07:15:11 info: GET /api/doctor/getall
8063+
2024-11-11 07:15:11 info: Token request verified for authMiddleware: function toString() { [native code] }
8064+
2024-11-11 07:15:11 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
8065+
2024-11-11 07:15:11 error: Error in checkPermission middleware: Error fetching permissions
8066+
2024-11-11 07:15:24 info: HTTPS Server running on port 3003,
8067+
2024-11-11 07:15:27 info: GET /api/doctor/getall
8068+
2024-11-11 07:15:27 info: Token request verified for authMiddleware: function toString() { [native code] }
8069+
2024-11-11 07:15:27 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8070+
2024-11-11 07:15:27 error: Error in checkPermission middleware: Error fetching permissions
8071+
2024-11-11 07:16:26 info: HTTPS Server running on port 3003,
8072+
2024-11-11 07:16:29 info: GET /api/doctor/getall
8073+
2024-11-11 07:16:29 info: Token request verified for authMiddleware: function toString() { [native code] }
8074+
2024-11-11 07:16:29 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8075+
2024-11-11 07:16:29 error: Error in checkPermission middleware: Error fetching permissions
8076+
2024-11-11 07:17:40 info: HTTPS Server running on port 3003,
8077+
2024-11-11 07:17:46 info: GET /api/doctor/getall
8078+
2024-11-11 07:17:46 info: Token request verified for authMiddleware: function toString() { [native code] }
8079+
2024-11-11 07:17:46 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
8080+
2024-11-11 07:17:46 error: Error in checkPermission middleware: Error fetching permissions
8081+
2024-11-11 07:18:41 info: HTTPS Server running on port 3003,
8082+
2024-11-11 07:18:44 info: GET /api/doctor/getall
8083+
2024-11-11 07:18:44 info: Token request verified for authMiddleware: function toString() { [native code] }
8084+
2024-11-11 07:19:15 info: HTTPS Server running on port 3003,
8085+
2024-11-11 07:20:40 info: HTTPS Server running on port 3003,
8086+
2024-11-11 07:20:43 info: GET /api/doctor/getall
8087+
2024-11-11 07:20:43 info: Token request verified for authMiddleware: function toString() { [native code] }
8088+
2024-11-11 07:20:59 info: HTTPS Server running on port 3003,
8089+
2024-11-11 07:21:04 info: GET /api/doctor/getall
8090+
2024-11-11 07:21:04 info: Token request verified for authMiddleware: function toString() { [native code] }

backend/logs/error.log

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,43 @@
517517
2024-11-10 19:36:29 error: Error in checkPermission middleware: Error fetching permissions
518518
2024-11-10 19:40:04 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
519519
2024-11-10 19:40:04 error: Error in checkPermission middleware: Error fetching permissions
520+
2024-11-11 06:54:04 error: Invalid token: jwt expired
521+
2024-11-11 06:54:22 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
522+
2024-11-11 06:54:22 error: Error in checkPermission middleware: Error fetching permissions
523+
2024-11-11 06:57:42 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
524+
2024-11-11 06:57:42 error: Error in checkPermission middleware: Error fetching permissions
525+
2024-11-11 07:00:34 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
526+
2024-11-11 07:00:34 error: Error in checkPermission middleware: Error fetching permissions
527+
2024-11-11 07:01:11 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
528+
2024-11-11 07:01:11 error: Error in checkPermission middleware: Error fetching permissions
529+
2024-11-11 07:02:20 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
530+
2024-11-11 07:02:20 error: Error in checkPermission middleware: Error fetching permissions
531+
2024-11-11 07:03:48 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
532+
2024-11-11 07:03:48 error: Error in checkPermission middleware: Error fetching permissions
533+
2024-11-11 07:04:46 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'permission_id')
534+
2024-11-11 07:07:12 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'permission_id')
535+
2024-11-11 07:08:13 error: Error in checkPermission middleware: Cannot read properties of undefined (reading 'permission_id')
536+
2024-11-11 07:08:38 error: Error fetching permissions for Role ID 1: rolePermissions.forEach is not a function
537+
2024-11-11 07:08:38 error: Error in checkPermission middleware: Error fetching permissions
538+
2024-11-11 07:09:31 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
539+
2024-11-11 07:09:31 error: Error in checkPermission middleware: Error fetching permissions
540+
2024-11-11 07:10:46 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
541+
2024-11-11 07:10:46 error: Error in checkPermission middleware: Error fetching permissions
542+
2024-11-11 07:11:48 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
543+
2024-11-11 07:11:48 error: Error in checkPermission middleware: Error fetching permissions
544+
2024-11-11 07:12:20 error: Error fetching permissions for Role ID 1: Unknown column 'Permission.role_id' in 'where clause'
545+
2024-11-11 07:12:20 error: Error in checkPermission middleware: Error fetching permissions
546+
2024-11-11 07:13:37 error: Error fetching permissions for Role ID 1: Unknown column 'Permission.role_id' in 'where clause'
547+
2024-11-11 07:13:37 error: Error in checkPermission middleware: Error fetching permissions
548+
2024-11-11 07:13:58 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
549+
2024-11-11 07:13:58 error: Error in checkPermission middleware: Error fetching permissions
550+
2024-11-11 07:14:47 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
551+
2024-11-11 07:14:47 error: Error in checkPermission middleware: Error fetching permissions
552+
2024-11-11 07:15:11 error: Error fetching permissions for Role ID 1: Permission is not associated to RolePermission!
553+
2024-11-11 07:15:11 error: Error in checkPermission middleware: Error fetching permissions
554+
2024-11-11 07:15:27 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
555+
2024-11-11 07:15:27 error: Error in checkPermission middleware: Error fetching permissions
556+
2024-11-11 07:16:29 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
557+
2024-11-11 07:16:29 error: Error in checkPermission middleware: Error fetching permissions
558+
2024-11-11 07:17:46 error: Error fetching permissions for Role ID 1: rolePermissions.map is not a function
559+
2024-11-11 07:17:46 error: Error in checkPermission middleware: Error fetching permissions

backend/middlewares/checkPermissionMiddleware.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const checkPermission = (permissionID) => {
2424
// Check if the required permission exists in any of the roles
2525
let hasPermission = false;
2626
const permissions = await permissionRepository.getPermissionsForRole(user.role_id);
27+
28+
2729
hasPermission = permissions.some(permission => permission.permission_id === permissionID);
2830

2931
if (hasPermission) {

backend/models/definedModels/Permission.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { Model, DataTypes } = require('sequelize');
22
const { createMainDBConnection } = require('../../config/db');
3-
const RolePermission = require('./RolePermission');
43

54
const sequelize = createMainDBConnection();
65

@@ -22,9 +21,8 @@ Permission.init({
2221
sequelize, // Pass the Sequelize instance
2322
modelName: 'Permission',
2423
tableName: 'permissions', // Specify the correct table name
25-
timestamps: true, // Adds createdAt and updatedAt fields
24+
timestamps: false, // Adds createdAt and updatedAt fields
2625
});
2726

28-
Permission.hasMany(RolePermission, { foreignKey: 'permission_id' });
2927

3028
module.exports = Permission;

backend/models/definedModels/Role.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Role.init({
2626
}, {
2727
sequelize, // Pass the Sequelize instance
2828
modelName: 'Role',
29+
timestamps: false,
2930
tableName: 'user_roles', // Specify the correct table name
3031
});
3132

backend/models/definedModels/RolePermission.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RolePermission extends Model {}
1212

1313
// Initialize the RolePermission model with explicit column definitions
1414
RolePermission.init({
15-
roleId: { // Renamed to camelCase to match JS conventions
15+
role_Id: { // Renamed to camelCase to match JS conventions
1616
type: DataTypes.INTEGER,
1717
references: {
1818
model: Role, // Reference the Role model
@@ -21,7 +21,7 @@ RolePermission.init({
2121
allowNull: false, // Ensure this field is not null
2222
},
2323

24-
permissionId: { // Renamed to camelCase to match JS conventions
24+
permission_Id: { // Renamed to camelCase to match JS conventions
2525
type: DataTypes.INTEGER,
2626
references: {
2727
model: Permission, // Reference the Permission model
@@ -36,6 +36,9 @@ RolePermission.init({
3636
timestamps: false, // No need for createdAt/updatedAt fields
3737
});
3838

39-
RolePermission.belongsTo(Permission, { foreignKey: 'permission_id' });
39+
// Define the many-to-many relationship using the junction table
40+
Role.belongsToMany(Permission, { through: RolePermission, foreignKey: 'role_Id' });
41+
Permission.belongsToMany(Role, { through: RolePermission, foreignKey: 'permission_Id' });
42+
4043

4144
module.exports = RolePermission;

backend/usecases/role/getPermissionsForRole.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,25 @@ const logger = require('../../config/logger');
77

88
// Function to get permissions for a specific role
99
const getPermissionsForRole = async (roleId) => {
10-
1110

11+
1212
try {
1313
// Fetch permissions associated with the given roleId
14-
const rolePermissions = await RolePermission.findAll({
14+
const rolePermissions = await Role.findOne({
1515
where: { role_id: roleId }, // Filter by role_id
1616
include: [{
1717
model: Permission, // Include Permission model
1818
attributes: ['permission_name', 'permission_id'] // Fetch relevant permission fields
1919
}]
2020
});
21-
console.log("Role permissons got ", roleId)
2221

23-
if (!rolePermissions || rolePermissions.length === 0) {
22+
23+
if (!rolePermissions.Permissions || rolePermissions.Permissions.length === 0) {
2424
logger.warn(`No permissions found for Role ID ${roleId}`);
2525
return []; // Return empty array if no permissions found
2626
}
27-
28-
// Extract permission data from the result
29-
const permissions = rolePermissions.map(rolePermission => rolePermission.Permission);
30-
return permissions;
27+
return rolePermissions.Permissions;
28+
3129
} catch (error) {
3230
logger.error(`Error fetching permissions for Role ID ${roleId}: ${error.message}`);
3331
throw new Error('Error fetching permissions');

0 commit comments

Comments
 (0)