|
@@ -108,10 +108,25 @@ export class UserController {
|
|
|
message: 'Unauthorized access'
|
|
message: 'Unauthorized access'
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
- const parentId = user.id
|
|
|
|
|
- const { page, size } = query
|
|
|
|
|
|
|
|
|
|
- const result = await this.userService.findAllChildUsers(parentId, page, size)
|
|
|
|
|
|
|
+ const { id, name, page, size } = query
|
|
|
|
|
+
|
|
|
|
|
+ let result
|
|
|
|
|
+
|
|
|
|
|
+ // ADMIN 可以查询所有用户
|
|
|
|
|
+ if (user.role === UserRole.ADMIN) {
|
|
|
|
|
+ result = await this.userService.findAllUsersWithFilter(page, size, id, name)
|
|
|
|
|
+ }
|
|
|
|
|
+ // MANAGER 只能查询自己下面的用户
|
|
|
|
|
+ else if (user.role === UserRole.MANAGER) {
|
|
|
|
|
+ const parentId = user.id
|
|
|
|
|
+ result = await this.userService.findAllChildUsers(parentId, page, size, undefined, name)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return reply.code(403).send({
|
|
|
|
|
+ error: 'Forbidden',
|
|
|
|
|
+ message: 'Access denied. Insufficient Permissions.'
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return reply.send(result)
|
|
return reply.send(result)
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -127,13 +142,29 @@ export class UserController {
|
|
|
async createUser(request: FastifyRequest<{ Body: CreateUserBody }>, reply: FastifyReply) {
|
|
async createUser(request: FastifyRequest<{ Body: CreateUserBody }>, reply: FastifyReply) {
|
|
|
try {
|
|
try {
|
|
|
const { password, name, role } = request.body
|
|
const { password, name, role } = request.body
|
|
|
|
|
+ const currentUser = request.user
|
|
|
|
|
|
|
|
const existingUser = await this.userService.findByName(name)
|
|
const existingUser = await this.userService.findByName(name)
|
|
|
if (existingUser) {
|
|
if (existingUser) {
|
|
|
return reply.code(400).send({ message: '用户名已存在' })
|
|
return reply.code(400).send({ message: '用户名已存在' })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const user = await this.userService.create(password, name, role, request.user.id)
|
|
|
|
|
|
|
+ // 根据当前用户角色决定创建用户的角色
|
|
|
|
|
+ let userRole: UserRole
|
|
|
|
|
+ if (currentUser.role === UserRole.ADMIN) {
|
|
|
|
|
+ // ADMIN 可以创建任意角色
|
|
|
|
|
+ userRole = role || UserRole.USER
|
|
|
|
|
+ } else if (currentUser.role === UserRole.MANAGER) {
|
|
|
|
|
+ // MANAGER 只能创建 USER 角色
|
|
|
|
|
+ userRole = UserRole.USER
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return reply.code(403).send({
|
|
|
|
|
+ error: 'Forbidden',
|
|
|
|
|
+ message: 'Access denied. Insufficient Permissions.'
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const user = await this.userService.create(password, name, userRole, currentUser.id)
|
|
|
|
|
|
|
|
return reply.code(201).send({
|
|
return reply.code(201).send({
|
|
|
user: {
|
|
user: {
|
|
@@ -152,13 +183,17 @@ export class UserController {
|
|
|
async updateUser(request: FastifyRequest<{ Body: UpdateUserBody }>, reply: FastifyReply) {
|
|
async updateUser(request: FastifyRequest<{ Body: UpdateUserBody }>, reply: FastifyReply) {
|
|
|
try {
|
|
try {
|
|
|
const { id, name, password, role } = request.body
|
|
const { id, name, password, role } = request.body
|
|
|
|
|
+ const currentUser = request.user
|
|
|
|
|
|
|
|
|
|
+ // 检查目标用户是否存在
|
|
|
|
|
+ let targetUser
|
|
|
try {
|
|
try {
|
|
|
- await this.userService.findById(id)
|
|
|
|
|
|
|
+ targetUser = await this.userService.findById(id)
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
return reply.code(404).send({ message: '用户不存在' })
|
|
return reply.code(404).send({ message: '用户不存在' })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 检查用户名是否重复
|
|
|
if (name) {
|
|
if (name) {
|
|
|
const existingUser = await this.userService.findByName(name)
|
|
const existingUser = await this.userService.findByName(name)
|
|
|
if (existingUser && existingUser.id !== id) {
|
|
if (existingUser && existingUser.id !== id) {
|
|
@@ -166,7 +201,30 @@ export class UserController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const updatedUser = await this.userService.updateUser(id, { name, password, role })
|
|
|
|
|
|
|
+ let updateData: Partial<typeof targetUser>
|
|
|
|
|
+
|
|
|
|
|
+ if (currentUser.role === UserRole.ADMIN) {
|
|
|
|
|
+ // ADMIN 可以修改所有字段
|
|
|
|
|
+ updateData = { name, password, role }
|
|
|
|
|
+ } else if (currentUser.role === UserRole.MANAGER) {
|
|
|
|
|
+ // MANAGER 只能修改自己下面用户的 name 和 password
|
|
|
|
|
+ const isChildUser = await this.userService.isChildUser(currentUser.id, id)
|
|
|
|
|
+ if (!isChildUser) {
|
|
|
|
|
+ return reply.code(403).send({
|
|
|
|
|
+ error: 'Forbidden',
|
|
|
|
|
+ message: 'You can only update your child users.'
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ // MANAGER 只能修改 name 和 password,不能修改 role
|
|
|
|
|
+ updateData = { name, password }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return reply.code(403).send({
|
|
|
|
|
+ error: 'Forbidden',
|
|
|
|
|
+ message: 'Access denied. Insufficient Permissions.'
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const updatedUser = await this.userService.updateUser(id, updateData)
|
|
|
|
|
|
|
|
return reply.send({
|
|
return reply.send({
|
|
|
user: {
|
|
user: {
|