feat: edit user role
This commit is contained in:
parent
4592192327
commit
f91c9c2f14
1 changed files with 30 additions and 1 deletions
|
|
@ -19,7 +19,14 @@ import minio from "../services/minio";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import { addUserRoles, createUser, deleteUser, getRoles } from "../services/keycloak";
|
import {
|
||||||
|
addUserRoles,
|
||||||
|
createUser,
|
||||||
|
deleteUser,
|
||||||
|
getRoles,
|
||||||
|
getUserRoles,
|
||||||
|
removeUserRoles,
|
||||||
|
} from "../services/keycloak";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -336,6 +343,27 @@ export class UserController extends Controller {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let list = await getRoles();
|
||||||
|
|
||||||
|
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||||
|
if (Array.isArray(list)) {
|
||||||
|
list = list.filter(
|
||||||
|
(a) =>
|
||||||
|
!["uma_authorization", "offline_access", "default-roles"].some((b) => a.name.includes(b)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const currentRole = await getUserRoles(userId);
|
||||||
|
|
||||||
|
const role = list.find((v) => v.id === body.userRole);
|
||||||
|
|
||||||
|
const resultAddRole = role && (await addUserRoles(userId, [role]));
|
||||||
|
|
||||||
|
if (!resultAddRole) {
|
||||||
|
throw new Error("Failed. Cannot set user's role.");
|
||||||
|
} else {
|
||||||
|
if (Array.isArray(currentRole)) await removeUserRoles(userId, currentRole);
|
||||||
|
}
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, ...rest } = body;
|
||||||
|
|
||||||
const user = await prisma.user.findFirst({
|
const user = await prisma.user.findFirst({
|
||||||
|
|
@ -362,6 +390,7 @@ export class UserController extends Controller {
|
||||||
include: { province: true, district: true, subDistrict: true },
|
include: { province: true, district: true, subDistrict: true },
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
|
userRole: role.name,
|
||||||
code:
|
code:
|
||||||
(lastUserOfType &&
|
(lastUserOfType &&
|
||||||
`${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) ||
|
`${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) ||
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue