check if user is creator and prevent role change for him
This commit is contained in:
parent
d16d669b37
commit
3cb6e576ce
|
|
@ -24,6 +24,7 @@ model User {
|
||||||
enum Role {
|
enum Role {
|
||||||
USER
|
USER
|
||||||
ADMIN
|
ADMIN
|
||||||
|
CREATOR
|
||||||
}
|
}
|
||||||
|
|
||||||
model Post {
|
model Post {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { changeUserRoles, getUserById } from "@/controller/UserController";
|
||||||
export default async (req: Request, res: Response) => {
|
export default async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const user = await getUserById(req.userId);
|
const user = await getUserById(req.userId);
|
||||||
if (user?.role !== "ADMIN") {
|
if (user?.role !== "ADMIN" && user?.role !== "CREATOR") {
|
||||||
return res.status(403).send({ error: "Forbidden" });
|
return res.status(403).send({ error: "Forbidden" });
|
||||||
}
|
}
|
||||||
const id = parseInt(req.params.id);
|
const id = parseInt(req.params.id);
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,9 @@ const newUser = async (user: User) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const changeUserRoles = async (id: number, role: Role) => {
|
export const changeUserRoles = async (id: number, role: Role) => {
|
||||||
|
if (role === 'CREATOR') {
|
||||||
|
return new Error('You cannot change user role to CREATOR');
|
||||||
|
}
|
||||||
const currentUser = await prisma.user.findUnique({
|
const currentUser = await prisma.user.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
|
|
@ -74,6 +77,9 @@ export const changeUserRoles = async (id: number, role: Role) => {
|
||||||
if (currentUser.role === role) {
|
if (currentUser.role === role) {
|
||||||
throw new Error('User already has this role');
|
throw new Error('User already has this role');
|
||||||
}
|
}
|
||||||
|
if (currentUser.role === 'CREATOR') {
|
||||||
|
throw new Error('You cannot change role of user with CREATOR role');
|
||||||
|
}
|
||||||
|
|
||||||
const updatedUser = await prisma.user.update({
|
const updatedUser = await prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue