mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-09-12 12:03:06 +08:00
Addressing review comments
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
import { getToken } from "next-auth/jwt";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps any API Route handler and verifies that the user has the appropriate
|
||||||
|
* role before running the handler. Returns a 403 otherwise.
|
||||||
|
*/
|
||||||
|
const withRole = (role: string, handler: (arg0: NextApiRequest, arg1: NextApiResponse<any>) => any) => {
|
||||||
|
return async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
|
const token = await getToken({ req });
|
||||||
|
if (!token || token.role !== role) {
|
||||||
|
res.status(403).end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return handler(req, res);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default withRole;
|
||||||
@@ -52,7 +52,7 @@ const ManageUser = ({ user }) => {
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
<title>Open Assistant</title>
|
<title>Manage Users - Open Assistant</title>
|
||||||
<meta
|
<meta
|
||||||
name="description"
|
name="description"
|
||||||
content="Conversational AI for everyone. An open source project to create a chat enabled GPT LLM run by LAION and contributors around the world."
|
content="Conversational AI for everyone. An open source project to create a chat enabled GPT LLM run by LAION and contributors around the world."
|
||||||
|
|||||||
@@ -1,19 +1,13 @@
|
|||||||
import { getToken } from "next-auth/jwt";
|
import { getToken } from "next-auth/jwt";
|
||||||
|
import withRole from "src/lib/auth";
|
||||||
import prisma from "src/lib/prismadb";
|
import prisma from "src/lib/prismadb";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update's the user's data in the database. Accessible only to admins.
|
* Update's the user's data in the database. Accessible only to admins.
|
||||||
*/
|
*/
|
||||||
const handler = async (req, res) => {
|
const handler = withRole("admin", async (req, res) => {
|
||||||
const token = await getToken({ req });
|
|
||||||
|
|
||||||
// Return nothing if the user isn't registered or if the user isn't an admin.
|
|
||||||
if (!token || token.role !== "admin") {
|
|
||||||
res.status(403).end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { id, role } = JSON.parse(req.body);
|
const { id, role } = JSON.parse(req.body);
|
||||||
|
|
||||||
await prisma.user.update({
|
await prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
@@ -24,6 +18,6 @@ const handler = async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).end();
|
res.status(200).end();
|
||||||
};
|
});
|
||||||
|
|
||||||
export default handler;
|
export default handler;
|
||||||
|
|||||||
@@ -1,21 +1,14 @@
|
|||||||
import { getToken } from "next-auth/jwt";
|
import { getToken } from "next-auth/jwt";
|
||||||
import client from "src/lib/prismadb";
|
import withRole from "src/lib/auth";
|
||||||
|
import prisma from "src/lib/prismadb";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of user results from the database when the requesting user is
|
* Returns a list of user results from the database when the requesting user is
|
||||||
* a logged in admin.
|
* a logged in admin.
|
||||||
*/
|
*/
|
||||||
const handler = async (req, res) => {
|
const handler = withRole("admin", async (req, res) => {
|
||||||
const token = await getToken({ req });
|
|
||||||
|
|
||||||
// Return nothing if the user isn't registered or if the user isn't an admin.
|
|
||||||
if (!token || token.role !== "admin") {
|
|
||||||
res.status(403).end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch 20 users.
|
// Fetch 20 users.
|
||||||
const users = await client.user.findMany({
|
const users = await prisma.user.findMany({
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
role: true,
|
role: true,
|
||||||
@@ -26,6 +19,6 @@ const handler = async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).json(users);
|
res.status(200).json(users);
|
||||||
};
|
});
|
||||||
|
|
||||||
export default handler;
|
export default handler;
|
||||||
|
|||||||
Reference in New Issue
Block a user