update
This commit is contained in:
@@ -8,21 +8,58 @@ import { AppModel } from '@/routes/app-manager/index.ts';
|
||||
|
||||
export const checkUsername = (username: string) => {
|
||||
if (username.length > 30) {
|
||||
throw new CustomError(400, 'Username cannot be too long');
|
||||
throw new CustomError(400, '用户名不能过长');
|
||||
}
|
||||
if (!/^[a-zA-Z0-9_@]+$/.test(username)) {
|
||||
throw new CustomError(400, 'Username cannot contain special characters');
|
||||
throw new CustomError(400, '用户名包含非法字符');
|
||||
}
|
||||
if (username.includes(' ')) {
|
||||
throw new CustomError(400, 'Username cannot contain spaces');
|
||||
throw new CustomError(400, '用户名不能包含空格');
|
||||
}
|
||||
};
|
||||
export const checkUsernameShort = (username: string) => {
|
||||
if (username.length < 3) {
|
||||
throw new CustomError(400, 'Username cannot be too short');
|
||||
if (username.length <= 3) {
|
||||
throw new CustomError(400, '用户名不能过短');
|
||||
}
|
||||
};
|
||||
|
||||
export const toChangeName = async (opts: { id: number; newName: string; admin?: boolean, ctx: any }) => {
|
||||
const { id, newName, admin = false, ctx } = opts;
|
||||
if (!admin) {
|
||||
checkUsernameShort(newName);
|
||||
}
|
||||
const user = await User.findByPk(id);
|
||||
if (!user) {
|
||||
ctx.throw(404, 'User not found');
|
||||
}
|
||||
const oldName = user.username;
|
||||
checkUsername(newName);
|
||||
const findUserByUsername = await User.findOne({ where: { username: newName } });
|
||||
if (findUserByUsername) {
|
||||
ctx.throw(400, 'Username already exists');
|
||||
}
|
||||
user.username = newName;
|
||||
const data = user.data || {};
|
||||
data.canChangeUsername = false;
|
||||
user.data = data;
|
||||
try {
|
||||
await user.save({ fields: ['username', 'data'] });
|
||||
// 迁移文件数据
|
||||
await backupUserA(oldName, user.id); // 备份文件数据
|
||||
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
|
||||
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
|
||||
|
||||
if (['org', 'user'].includes(user.type)) {
|
||||
const type = user.type === 'org' ? 'org' : 'user';
|
||||
await User.clearUserToken(user.id, type); // 清除旧token
|
||||
}
|
||||
await AppModel.moveToNewUser(oldName, newName); // 更新用户数据
|
||||
} catch (error) {
|
||||
console.error('迁移文件数据失败', error);
|
||||
ctx.throw(500, 'Failed to change username');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
app
|
||||
.route({
|
||||
path: 'user',
|
||||
@@ -31,34 +68,16 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { id, newName } = ctx.query.data || {};
|
||||
const user = await User.findByPk(id);
|
||||
if (!user) {
|
||||
ctx.throw(404, 'User not found');
|
||||
}
|
||||
const oldName = user.username;
|
||||
checkUsername(newName);
|
||||
const findUserByUsername = await User.findOne({ where: { username: newName } });
|
||||
if (findUserByUsername) {
|
||||
ctx.throw(400, 'Username already exists');
|
||||
}
|
||||
user.username = newName;
|
||||
try {
|
||||
await user.save();
|
||||
// 迁移文件数据
|
||||
await backupUserA(oldName, user.id); // 备份文件数据
|
||||
await mvUserAToUserB(oldName, newName, true); // 迁移文件数据
|
||||
// await mvAppFromUserAToUserB(oldName, newName); // 迁移应用数据
|
||||
|
||||
if (['org', 'user'].includes(user.type)) {
|
||||
const type = user.type === 'org' ? 'org' : 'user';
|
||||
await User.clearUserToken(user.id, type); // 清除旧token
|
||||
if (!id || !newName) {
|
||||
ctx.throw(400, '参数错误');
|
||||
}
|
||||
await AppModel.moveToNewUser(oldName, newName); // 更新用户数据
|
||||
const user = await toChangeName({ id, newName, admin: true, ctx });
|
||||
ctx.body = await user?.getInfo?.();
|
||||
} catch (error) {
|
||||
console.error('迁移文件数据失败', error);
|
||||
console.error('changeName error', error);
|
||||
ctx.throw(500, 'Failed to change username');
|
||||
}
|
||||
ctx.body = user;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user