-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add verifyRoles middleware * Verfiy roles * remove test code
- Loading branch information
Showing
7 changed files
with
53 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Role } from '@prisma/client' | ||
|
||
const allRoles: Record<Role, string[]> = { | ||
[Role.USER]: [], | ||
[Role.ADMIN]: ['manageUsers'], | ||
} | ||
|
||
export const roles = Object.keys(allRoles) as Role[] | ||
|
||
export const roleRights = new Map<Role, string[]>( | ||
Object.entries(allRoles) as [Role, string[]][], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NextFunction, Request, Response } from 'express' | ||
import { Role } from '@prisma/client' | ||
import { errorResponse } from '../utils' | ||
import { roleRights } from '../config/roles' | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace Express { | ||
interface User { | ||
role?: Role | ||
} | ||
} | ||
} | ||
|
||
export const verifyRoles = (...requiredRights: string[]) => { | ||
return (req: Request, res: Response, next: NextFunction) => { | ||
if (requiredRights.length) { | ||
const userRole = req.user?.role | ||
if (!userRole) { | ||
return errorResponse(res, null, 403, 'Forbidden') | ||
} | ||
const userRights = roleRights.get(userRole) || [] | ||
const hasRequiredRights = requiredRights.every((r) => | ||
userRights.includes(r), | ||
) | ||
if (!hasRequiredRights) { | ||
return errorResponse(res, null, 403, 'Forbidden') | ||
} | ||
} | ||
next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters