refactor: middleware
This commit is contained in:
parent
06919a0205
commit
0699835129
1 changed files with 5 additions and 40 deletions
|
|
@ -3,9 +3,11 @@ import { RequestWithUser } from "../interfaces/user";
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import { isSystem } from "../utils/keycloak";
|
import { branchRelationPermInclude, createPermCheck } from "../services/permission";
|
||||||
|
|
||||||
export function permissionCheck(globalAllow: (user: RequestWithUser["user"]) => boolean) {
|
export function permissionCheck(globalAllow: (user: RequestWithUser["user"]) => boolean) {
|
||||||
|
const checker = createPermCheck(globalAllow);
|
||||||
|
|
||||||
return async (req: RequestWithUser, _res: express.Response, next: express.NextFunction) => {
|
return async (req: RequestWithUser, _res: express.Response, next: express.NextFunction) => {
|
||||||
if ("employeeId" in req.params && typeof req.params.employeeId === "string") {
|
if ("employeeId" in req.params && typeof req.params.employeeId === "string") {
|
||||||
const employeeId = req.params.employeeId;
|
const employeeId = req.params.employeeId;
|
||||||
|
|
@ -17,23 +19,7 @@ export function permissionCheck(globalAllow: (user: RequestWithUser["user"]) =>
|
||||||
customer: {
|
customer: {
|
||||||
include: {
|
include: {
|
||||||
registeredBranch: {
|
registeredBranch: {
|
||||||
include: {
|
include: branchRelationPermInclude(req.user),
|
||||||
user: {
|
|
||||||
where: { userId: req.user.sub },
|
|
||||||
},
|
|
||||||
branch: {
|
|
||||||
where: {
|
|
||||||
user: {
|
|
||||||
some: { userId: req.user.sub },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
include: {
|
|
||||||
user: {
|
|
||||||
where: { userId: req.user.sub },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -46,28 +32,7 @@ export function permissionCheck(globalAllow: (user: RequestWithUser["user"]) =>
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq");
|
throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSystem(req.user)) {
|
await checker(req.user, employee.customerBranch.customer.registeredBranch);
|
||||||
const _branch = employee.customerBranch.customer.registeredBranch;
|
|
||||||
const affilationBranch = _branch && _branch.user.length !== 0;
|
|
||||||
const affilationHeadBranch = _branch && _branch.branch.every((v) => v.user.length !== 0);
|
|
||||||
if (!globalAllow(req.user)) {
|
|
||||||
if (!affilationBranch) {
|
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.FORBIDDEN,
|
|
||||||
"You do not have permission to perform this action.",
|
|
||||||
"noPermission",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!affilationBranch && !affilationHeadBranch) {
|
|
||||||
throw new HttpError(
|
|
||||||
HttpStatus.FORBIDDEN,
|
|
||||||
"You do not have permission to perform this action.",
|
|
||||||
"noPermission",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue