mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-05 15:01:23 +08:00
Protected super admin
This commit is contained in:
@@ -57,7 +57,10 @@ public class UserController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseResult deleteRole(@PathVariable Long id) {
|
||||
public ResponseResult deleteUser(@PathVariable Long id) {
|
||||
if (id == 1L) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_DELETE_ERROR, "Unable to remove super admin", null);
|
||||
}
|
||||
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(User::getId, id);
|
||||
if (userService.remove(wrapper)) {
|
||||
@@ -68,7 +71,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@PutMapping()
|
||||
public ResponseResult modifyRole(@RequestBody User user) {
|
||||
public ResponseResult modifyUser(@RequestBody User user) {
|
||||
if (!StringUtils.hasText(user.getUsername())) {
|
||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Username cannot be empty", null);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
@@ -24,6 +26,8 @@ import lombok.experimental.Accessors;
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
@TableName("t_group")
|
||||
public class Group implements Serializable {
|
||||
@@ -51,4 +55,9 @@ public class Group implements Serializable {
|
||||
|
||||
@TableField(exist = false)
|
||||
private List<Role> roles;
|
||||
|
||||
public Group(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ import com.cfive.pinnacle.entity.permission.Operation;
|
||||
import com.cfive.pinnacle.entity.permission.Power;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
@@ -28,6 +30,8 @@ import lombok.experimental.Accessors;
|
||||
* @since 2023-04-30
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
@TableName("t_role")
|
||||
public class Role implements Serializable {
|
||||
@@ -64,4 +68,9 @@ public class Role implements Serializable {
|
||||
@TableField("version")
|
||||
@Version
|
||||
private Integer version;
|
||||
|
||||
public Role(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,7 @@ package com.cfive.pinnacle.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.cfive.pinnacle.entity.*;
|
||||
import com.cfive.pinnacle.mapper.UserGroupMapper;
|
||||
import com.cfive.pinnacle.mapper.UserMapper;
|
||||
import com.cfive.pinnacle.mapper.UserRoleMapper;
|
||||
import com.cfive.pinnacle.mapper.*;
|
||||
import com.cfive.pinnacle.service.IUserService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -53,12 +51,24 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
||||
|
||||
@Override
|
||||
public List<User> getAllUser() {
|
||||
return userMapper.getAll();
|
||||
List<User> users = userMapper.getAll();
|
||||
users.forEach(user -> {
|
||||
if (user.getId() == 1L) {
|
||||
user.setRoles(List.of(new Role(0L, "超级管理员")));
|
||||
user.setGroups(List.of(new Group(0L, "超级管理员")));
|
||||
}
|
||||
});
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUser(long id) {
|
||||
return userMapper.getOneById(id);
|
||||
User user = userMapper.getOneById(id);
|
||||
if (user.getId() == 1L) {
|
||||
user.setRoles(List.of(new Role(0L, "超级管理员")));
|
||||
user.setGroups(List.of(new Group(0L, "超级管理员")));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,6 +106,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
||||
String encryptedPassword = passwordEncoder.encode(user.getPasswd());
|
||||
user.setPasswd(encryptedPassword);
|
||||
}
|
||||
|
||||
// Protect administrator
|
||||
if (user.getId() == 1L) {
|
||||
user.setDepartmentId(null);
|
||||
user.setEnable(1);
|
||||
user.setDeleted(0L);
|
||||
userMapper.updateById(user);
|
||||
return true;
|
||||
}
|
||||
|
||||
userMapper.updateById(user);
|
||||
User originalUser = getUser(user.getId());
|
||||
HashSet<Long> newRoleIds = new HashSet<>();
|
||||
|
||||
Reference in New Issue
Block a user