refactor(user.service): 新增内置管理员账号保护逻辑
在用户列表查询、更新和更新状态接口中,添加对id为1的内置管理员账号的保护: 1. 列表查询时自动排除内置管理员账号 2. 禁止编辑内置管理员账号 3. 禁止修改内置管理员账号状态
This commit is contained in:
1 parent
2793945385
commit
25f8d3f6f9
1 file changed
+22
-1
@@ -21,7 +21,10 @@ export class UserService {
|
||||
async list(query: { page?: any; pageSize?: any; keyword?: string; status?: number; roleId?: number }): Promise<any> {
|
||||
const page = Number(query.page) || 1;
|
||||
const pageSize = Number(query.pageSize) || 10;
|
||||
const where: any = {};
|
||||
// 排除内置管理员账号(id=1)
|
||||
const where: any = {
|
||||
id: { [Op.ne]: 1 }
|
||||
};
|
||||
|
||||
if (query.keyword) {
|
||||
where[Op.or] = [{ username: { [Op.like]: `%${query.keyword}%` } }, { realName: { [Op.like]: `%${query.keyword}%` } }, { phone: { [Op.like]: `%${query.keyword}%` } }];
|
||||
@@ -154,6 +157,15 @@ export class UserService {
|
||||
}
|
||||
|
||||
async update(id: string, data: any): Promise<any> {
|
||||
// 禁止编辑内置管理员账号
|
||||
if (Number(id) === 1) {
|
||||
return {
|
||||
code: 400,
|
||||
message: "内置管理员账号不允许编辑",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
const user = await this.userModel.findByPk(id);
|
||||
|
||||
if (!user) {
|
||||
@@ -282,6 +294,15 @@ export class UserService {
|
||||
}
|
||||
|
||||
async updateStatus(id: string, status: number): Promise<any> {
|
||||
// 禁止修改内置管理员账号状态
|
||||
if (Number(id) === 1) {
|
||||
return {
|
||||
code: 400,
|
||||
message: "内置管理员账号状态不允许修改",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
const user = await this.userModel.findByPk(id);
|
||||
|
||||
if (!user) {
|
||||
|
||||
Reference in new issue
Block a user