Spaces:
Runtime error
Runtime error
Bansari Akhani
bydefault get invoice list and users list in descending order, remove ACL restriction
b695efe
import { Request, Response, NextFunction } from 'express'; | |
import Permission from '../models/permissions'; | |
import RolePermission from '../models/rolePermissions'; | |
import { AuthenticatedRequest } from '../shared/interfaces/user.interface'; | |
import { APP_CONFIG } from '../config/app.config'; | |
export const checkPermission = (requiredPermission: string) => { | |
return async (req: AuthenticatedRequest, res: Response, next: NextFunction) => { | |
try { | |
const userRoleId = req?.user?.role_id; | |
const hasPermission = await RolePermission.findOne({ | |
where: { | |
role_id: userRoleId, | |
}, | |
include: [ | |
{ | |
model: Permission, | |
where: { permission_name: requiredPermission }, | |
required: true, | |
}, | |
], | |
}); | |
if (!hasPermission) { | |
return res | |
.status(403) | |
.json({ message: 'Forbidden: You do not have permission to access this module' }); | |
} | |
next(); | |
} catch (error) { | |
console.error('Error in checkPermission middleware:', error); | |
return res.status(500).json({ message: 'Internal Server Error' }); | |
} | |
}; | |
}; | |