mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-02 08:06:38 +08:00
update user admin
This commit is contained in:
parent
fe48b5acfc
commit
a64f52ecbb
@ -95,7 +95,16 @@ async function handle(
|
|||||||
if (method === "PUT") {
|
if (method === "PUT") {
|
||||||
try {
|
try {
|
||||||
const userId = params.path[0];
|
const userId = params.path[0];
|
||||||
return await changeUserInfo(userId, await req.json());
|
let new_user_info: Partial<User> = Object.entries(
|
||||||
|
await req.json(),
|
||||||
|
).reduce((acc, [key, value]) => {
|
||||||
|
if (value !== null) {
|
||||||
|
// @ts-ignore
|
||||||
|
acc[key] = value;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
return await changeUserInfo(userId, new_user_info);
|
||||||
} catch {
|
} catch {
|
||||||
return NextResponse.json({ error: "未知错误" }, { status: 500 });
|
return NextResponse.json({ error: "未知错误" }, { status: 500 });
|
||||||
}
|
}
|
||||||
@ -103,16 +112,18 @@ async function handle(
|
|||||||
return NextResponse.json({ error: "当前方法不支持" }, { status: 405 });
|
return NextResponse.json({ error: "当前方法不支持" }, { status: 405 });
|
||||||
}
|
}
|
||||||
|
|
||||||
async function changeUserInfo(id: string, info: User) {
|
async function changeUserInfo(id: string, info: Partial<User>) {
|
||||||
const hashDPassword = hashPassword(info?.password ?? "");
|
if (info.password) {
|
||||||
console.log("-----------", id, info, hashDPassword);
|
info["password"] = hashPassword(info.password);
|
||||||
if (hashDPassword) {
|
}
|
||||||
|
// console.log("-----------", id, info, hashDPassword);
|
||||||
|
if (info) {
|
||||||
await prisma.user.update({
|
await prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
password: hashDPassword,
|
...info,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return NextResponse.json({ result: "ok" });
|
return NextResponse.json({ result: "ok" });
|
||||||
|
0
app/api/test/route.ts
Normal file
0
app/api/test/route.ts
Normal file
@ -15,6 +15,7 @@ import {
|
|||||||
Form,
|
Form,
|
||||||
} from "antd";
|
} from "antd";
|
||||||
import type { GetRef, TableColumnsType } from "antd";
|
import type { GetRef, TableColumnsType } from "antd";
|
||||||
|
import { LockOutlined } from "@ant-design/icons";
|
||||||
// import { headers } from 'next/headers'
|
// import { headers } from 'next/headers'
|
||||||
import type { NotificationArgsProps } from "antd";
|
import type { NotificationArgsProps } from "antd";
|
||||||
|
|
||||||
@ -97,10 +98,100 @@ function UsersTable({ users, setUsers, loading }: UserInterface) {
|
|||||||
setNewPassword(e.target.value.trim());
|
setNewPassword(e.target.value.trim());
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleUserEdit = (
|
const handleUserEdit = (method: "POST" | "PUT", record: User | undefined) => {
|
||||||
method: "POST" | "PUT",
|
editUserModal.confirm({
|
||||||
record: User | undefined,
|
title: "编辑用户",
|
||||||
) => {};
|
content: (
|
||||||
|
<Form
|
||||||
|
form={editUserForm}
|
||||||
|
labelCol={{ span: 7 }}
|
||||||
|
wrapperCol={{ span: 28 }}
|
||||||
|
layout="horizontal"
|
||||||
|
autoComplete="off"
|
||||||
|
initialValues={record}
|
||||||
|
preserve={false}
|
||||||
|
>
|
||||||
|
<Form.Item name="id" label="id" rules={[{ required: true }]}>
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="name" label="name">
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="username" label="username">
|
||||||
|
<Input autoComplete="off" />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="gh_username" label="gh_username">
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="email" label="email">
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="emailVerified" label="emailVerified">
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="createdAt" label="createdAt">
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="updatedAt" label="updatedAt">
|
||||||
|
<Input disabled />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item
|
||||||
|
name="allowToLogin"
|
||||||
|
label="allowToLogin"
|
||||||
|
valuePropName="checked"
|
||||||
|
>
|
||||||
|
<Checkbox />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="isAdmin" label="isAdmin" valuePropName="checked">
|
||||||
|
<Checkbox />
|
||||||
|
</Form.Item>
|
||||||
|
|
||||||
|
<Form.Item name="password" label="password">
|
||||||
|
<Input
|
||||||
|
prefix={<LockOutlined className="site-form-item-icon" />}
|
||||||
|
type="password"
|
||||||
|
placeholder="Password"
|
||||||
|
autoComplete="new-password"
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
),
|
||||||
|
onOk: () => {
|
||||||
|
const setting_key = method === "PUT" ? record?.id : "";
|
||||||
|
editUserForm.validateFields().then((values) => {
|
||||||
|
const dataToSubmit = {
|
||||||
|
username: values.username ?? null,
|
||||||
|
email: values.email ?? null,
|
||||||
|
allowToLogin: values.allowToLogin ?? true,
|
||||||
|
isAdmin: values.isAdmin ?? false,
|
||||||
|
password: values.password ?? null,
|
||||||
|
};
|
||||||
|
fetch(`/api/admin/users/${values.id}`, {
|
||||||
|
method: method,
|
||||||
|
credentials: "include",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(dataToSubmit),
|
||||||
|
})
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((result) => {
|
||||||
|
if (result["result"] == "ok") {
|
||||||
|
openNotification("info", {
|
||||||
|
message: "修改信息",
|
||||||
|
description: `${values.id} 信息修改成功`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log("e", error);
|
||||||
|
openNotification("error", {
|
||||||
|
message: "修改信息",
|
||||||
|
description: `${values.id} 信息修改失败`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const confirmPassword = async (id: string) => {
|
const confirmPassword = async (id: string) => {
|
||||||
console.log("-----", newPassword, id);
|
console.log("-----", newPassword, id);
|
||||||
@ -240,7 +331,9 @@ function UsersTable({ users, setUsers, loading }: UserInterface) {
|
|||||||
key: "id",
|
key: "id",
|
||||||
render: (_, record) => (
|
render: (_, record) => (
|
||||||
<Space size="small">
|
<Space size="small">
|
||||||
<a>编辑</a>
|
<a type="link" onClick={() => handleUserEdit("PUT", record)}>
|
||||||
|
编辑
|
||||||
|
</a>
|
||||||
<Popconfirm
|
<Popconfirm
|
||||||
id="user-admin-table-pop_confirm"
|
id="user-admin-table-pop_confirm"
|
||||||
title="设置密码"
|
title="设置密码"
|
||||||
|
@ -10,7 +10,7 @@ import {createTransport} from "nodemailer";
|
|||||||
import { comparePassword, hashPassword } from "@/lib/utils";
|
import { comparePassword, hashPassword } from "@/lib/utils";
|
||||||
import {getCurStartEnd} from "@/app/utils/custom";
|
import {getCurStartEnd} from "@/app/utils/custom";
|
||||||
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;
|
||||||
type PartialUser = Partial<User>;
|
|
||||||
|
|
||||||
|
|
||||||
export const authOptions: NextAuthOptions = {
|
export const authOptions: NextAuthOptions = {
|
||||||
@ -89,7 +89,7 @@ export const authOptions: NextAuthOptions = {
|
|||||||
// 判断姓名格式是否符合要求,不符合则拒绝
|
// 判断姓名格式是否符合要求,不符合则拒绝
|
||||||
if (username && isName(username)) {
|
if (username && isName(username)) {
|
||||||
// Any object returned will be saved in `user` property of the JWT
|
// Any object returned will be saved in `user` property of the JWT
|
||||||
let user: PartialUser = {}
|
let user: Partial<User> = {}
|
||||||
if (isEmail(username)) {
|
if (isEmail(username)) {
|
||||||
user['email'] = username;
|
user['email'] = username;
|
||||||
} else {
|
} else {
|
||||||
@ -228,7 +228,7 @@ async function getSetting(key: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function existUser(user: PartialUser ) {
|
async function existUser(user: Partial<User> ) {
|
||||||
const conditions = [];
|
const conditions = [];
|
||||||
if (user?.name) {
|
if (user?.name) {
|
||||||
conditions.push({ name: user.name });
|
conditions.push({ name: user.name });
|
||||||
@ -243,7 +243,7 @@ async function existUser(user: PartialUser ) {
|
|||||||
}) : null
|
}) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function insertUser(user: PartialUser ) {
|
export async function insertUser(user: Partial<User> ) {
|
||||||
console.log('------------', user)
|
console.log('------------', user)
|
||||||
try {
|
try {
|
||||||
return await prisma.user.create({
|
return await prisma.user.create({
|
||||||
|
Loading…
Reference in New Issue
Block a user