admin dashboard user managment ui; set users permission actions
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
"use server";
|
||||
|
||||
import { api } from "@/trpc/server";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function setUserPermissions(userId: string, permission: number) {
|
||||
const result = await api.users.setPermission({ userId, permission });
|
||||
if (!result[0]?.id) return { success: false };
|
||||
revalidatePath("/admin/benutzer");
|
||||
return { success: true };
|
||||
}
|
||||
@@ -1,10 +1,31 @@
|
||||
import { hasPermission, Role } from "@/lib/validation/permissions";
|
||||
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
||||
import { z } from "zod";
|
||||
import { permission } from "process";
|
||||
import { users } from "@/server/db/schema";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
export const usersRouter = createTRPCRouter({
|
||||
getAll: protectedProcedure.query(async ({ ctx }) => {
|
||||
const isAdmin = hasPermission(ctx.session.user.role, Role.ADMIN);
|
||||
if (!isAdmin) throw new Error("You are not allowed to get all users");
|
||||
return await ctx.db.query.users.findMany();
|
||||
return await ctx.db.query.users.findMany({
|
||||
orderBy: desc(users.role),
|
||||
});
|
||||
}),
|
||||
|
||||
setPermission: protectedProcedure
|
||||
.input(z.object({ userId: z.string(), permission: z.number() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const isAdmin = hasPermission(ctx.session.user.role, Role.ADMIN);
|
||||
if (!isAdmin)
|
||||
throw new Error("You are not allowed to set user permissions");
|
||||
return await ctx.db
|
||||
.update(users)
|
||||
.set({
|
||||
role: input.permission,
|
||||
})
|
||||
.where(eq(users.id, input.userId))
|
||||
.returning({ id: users.id });
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user