refactor: separate function

This commit is contained in:
Methapon Metanipat 2024-09-05 09:01:28 +07:00
parent 05a959ee55
commit 42cce880cd
3 changed files with 7 additions and 7 deletions

View file

@ -19,6 +19,7 @@ import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "../interfaces/user";
import minio, { presignedGetObjectIfExist } from "../services/minio";
import { isSystem } from "../utils/keycloak";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -27,10 +28,6 @@ if (!process.env.MINIO_BUCKET) {
const MINIO_BUCKET = process.env.MINIO_BUCKET;
const MANAGE_ROLES = ["system", "head_of_admin"];
function isSystem(user: RequestWithUser["user"]) {
return user.roles.includes("system");
}
function globalAllow(user: RequestWithUser["user"]) {
return MANAGE_ROLES.some((v) => user.roles?.includes(v));
}

View file

@ -28,6 +28,7 @@ import {
getUserRoles,
removeUserRoles,
} from "../services/keycloak";
import { isSystem } from "../utils/keycloak";
if (!process.env.MINIO_BUCKET) {
throw Error("Require MinIO bucket.");
@ -36,9 +37,6 @@ if (!process.env.MINIO_BUCKET) {
const MINIO_BUCKET = process.env.MINIO_BUCKET;
const MANAGE_ROLES = ["system", "head_of_admin", "admin", "branch_manager"];
function isSystem(user: RequestWithUser["user"]) {
return user.roles.includes("system");
}
function globalAllow(user: RequestWithUser["user"]) {
const listAllowed = ["system", "head_of_admin"];
return user.roles?.some((v) => listAllowed.includes(v)) || false;

5
src/utils/keycloak.ts Normal file
View file

@ -0,0 +1,5 @@
import { RequestWithUser } from "../interfaces/user";
export function isSystem(user: RequestWithUser["user"]) {
return user.roles.includes("system");
}