import { Request , Response , NextFunction } from 'express' ; import { Role } from '../typeorm/entities/users/types' ; import { CustomError } from '../utils/response/custom-error/CustomError' ; export const checkRole = ( roles , isSelfAllowed = false ) => { return async ( req , res , next ) => { const { id , role } = req . jwtPayload ; const { id : requestId } = req . params ; let errorSelfAllowed = null ; if ( isSelfAllowed ) { if ( id === parseInt ( requestId ) ) { return next ( ) ; } errorSelfAllowed = 'Self allowed action.' ; } if ( roles . indexOf ( role ) === - 1 ) { const errors = [ 'Unauthorized - Insufficient user rights' , `Current role: ${ role }. Required role: ${ roles . toString ( ) }` , ] ; if ( errorSelfAllowed ) { errors . push ( errorSelfAllowed ) ; } const customError = new CustomError ( 401 , 'Unauthorized' , 'Unauthorized - Insufficient user rights' , errors ) ; return next ( customError ) ; } return next ( ) ; } ; } ;