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}")
|
@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<>();
|
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.eq(User::getId, id);
|
wrapper.eq(User::getId, id);
|
||||||
if (userService.remove(wrapper)) {
|
if (userService.remove(wrapper)) {
|
||||||
@@ -68,7 +71,7 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping()
|
@PutMapping()
|
||||||
public ResponseResult modifyRole(@RequestBody User user) {
|
public ResponseResult modifyUser(@RequestBody User user) {
|
||||||
if (!StringUtils.hasText(user.getUsername())) {
|
if (!StringUtils.hasText(user.getUsername())) {
|
||||||
return ResponseResult.build(ResponseCode.DATABASE_UPDATE_ERROR, "Username cannot be empty", null);
|
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.annotation.JsonSerialize;
|
||||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,6 +26,8 @@ import lombok.experimental.Accessors;
|
|||||||
* @since 2023-04-30
|
* @since 2023-04-30
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
@TableName("t_group")
|
@TableName("t_group")
|
||||||
public class Group implements Serializable {
|
public class Group implements Serializable {
|
||||||
@@ -51,4 +55,9 @@ public class Group implements Serializable {
|
|||||||
|
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private List<Role> roles;
|
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.cfive.pinnacle.entity.permission.Power;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,6 +30,8 @@ import lombok.experimental.Accessors;
|
|||||||
* @since 2023-04-30
|
* @since 2023-04-30
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
@TableName("t_role")
|
@TableName("t_role")
|
||||||
public class Role implements Serializable {
|
public class Role implements Serializable {
|
||||||
@@ -64,4 +68,9 @@ public class Role implements Serializable {
|
|||||||
@TableField("version")
|
@TableField("version")
|
||||||
@Version
|
@Version
|
||||||
private Integer 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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.cfive.pinnacle.entity.*;
|
import com.cfive.pinnacle.entity.*;
|
||||||
import com.cfive.pinnacle.mapper.UserGroupMapper;
|
import com.cfive.pinnacle.mapper.*;
|
||||||
import com.cfive.pinnacle.mapper.UserMapper;
|
|
||||||
import com.cfive.pinnacle.mapper.UserRoleMapper;
|
|
||||||
import com.cfive.pinnacle.service.IUserService;
|
import com.cfive.pinnacle.service.IUserService;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -53,12 +51,24 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<User> getAllUser() {
|
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
|
@Override
|
||||||
public User getUser(long id) {
|
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
|
@Override
|
||||||
@@ -96,6 +106,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
|
|||||||
String encryptedPassword = passwordEncoder.encode(user.getPasswd());
|
String encryptedPassword = passwordEncoder.encode(user.getPasswd());
|
||||||
user.setPasswd(encryptedPassword);
|
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);
|
userMapper.updateById(user);
|
||||||
User originalUser = getUser(user.getId());
|
User originalUser = getUser(user.getId());
|
||||||
HashSet<Long> newRoleIds = new HashSet<>();
|
HashSet<Long> newRoleIds = new HashSet<>();
|
||||||
|
|||||||
@@ -53,7 +53,12 @@
|
|||||||
/>
|
/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="角色">
|
<el-form-item label="角色">
|
||||||
<el-select v-model="userForm.selectedRoles" multiple style="width: 100%">
|
<el-select
|
||||||
|
:disabled="disableSelectRoles"
|
||||||
|
v-model="userForm.selectedRoles"
|
||||||
|
multiple
|
||||||
|
style="width: 100%"
|
||||||
|
>
|
||||||
<el-option
|
<el-option
|
||||||
v-for="role in roles"
|
v-for="role in roles"
|
||||||
:key="role.id"
|
:key="role.id"
|
||||||
@@ -63,7 +68,12 @@
|
|||||||
</el-select>
|
</el-select>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="用户组">
|
<el-form-item label="用户组">
|
||||||
<el-select v-model="userForm.selectedGroups" multiple style="width: 100%">
|
<el-select
|
||||||
|
:disabled="disableSelectGroups"
|
||||||
|
v-model="userForm.selectedGroups"
|
||||||
|
multiple
|
||||||
|
style="width: 100%"
|
||||||
|
>
|
||||||
<el-option
|
<el-option
|
||||||
v-for="group in groups"
|
v-for="group in groups"
|
||||||
:key="group.id"
|
:key="group.id"
|
||||||
@@ -132,7 +142,9 @@ export default {
|
|||||||
message: '密码不能为空'
|
message: '密码不能为空'
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
disableSelectRoles: false,
|
||||||
|
disableSelectGroups: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@@ -205,10 +217,18 @@ export default {
|
|||||||
this.userForm.selectedRoles = []
|
this.userForm.selectedRoles = []
|
||||||
this.userForm.selectedGroups = []
|
this.userForm.selectedGroups = []
|
||||||
for (const role of row.roles) {
|
for (const role of row.roles) {
|
||||||
this.userForm.selectedRoles.push(role.id)
|
if (role.id === '0') {
|
||||||
|
this.userForm.selectedRoles.push(role.name)
|
||||||
|
} else {
|
||||||
|
this.userForm.selectedRoles.push(role.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (const group of row.groups) {
|
for (const group of row.groups) {
|
||||||
this.userForm.selectedGroups.push(group.id)
|
if (group.id === '0') {
|
||||||
|
this.userForm.selectedGroups.push(group.name)
|
||||||
|
} else {
|
||||||
|
this.userForm.selectedGroups.push(group.id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.isAddNew = false
|
this.isAddNew = false
|
||||||
this.dialogVisible = true
|
this.dialogVisible = true
|
||||||
@@ -237,6 +257,9 @@ export default {
|
|||||||
handleDialogOpen() {
|
handleDialogOpen() {
|
||||||
this.getRoles()
|
this.getRoles()
|
||||||
|
|
||||||
|
this.disableSelectRoles = false
|
||||||
|
this.disableSelectGroups = false
|
||||||
|
|
||||||
if (this.isAddNew) {
|
if (this.isAddNew) {
|
||||||
this.userForm.inputUsername = ''
|
this.userForm.inputUsername = ''
|
||||||
this.userForm.inputPassword = ''
|
this.userForm.inputPassword = ''
|
||||||
@@ -245,6 +268,10 @@ export default {
|
|||||||
this.dialogTitle = '添加用户'
|
this.dialogTitle = '添加用户'
|
||||||
} else {
|
} else {
|
||||||
this.dialogTitle = '编辑用户'
|
this.dialogTitle = '编辑用户'
|
||||||
|
if (this.editUserId === '1') {
|
||||||
|
this.disableSelectRoles = true
|
||||||
|
this.disableSelectGroups = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async handleSubmit() {
|
async handleSubmit() {
|
||||||
@@ -258,18 +285,21 @@ export default {
|
|||||||
roles: [],
|
roles: [],
|
||||||
groups: []
|
groups: []
|
||||||
}
|
}
|
||||||
for (const roleId of this.userForm.selectedRoles) {
|
if (this.editUserId !== '1') {
|
||||||
const role = {
|
for (const roleId of this.userForm.selectedRoles) {
|
||||||
id: roleId
|
const role = {
|
||||||
|
id: roleId
|
||||||
|
}
|
||||||
|
userObject.roles.push(role)
|
||||||
}
|
}
|
||||||
userObject.roles.push(role)
|
for (const groupId of this.userForm.selectedGroups) {
|
||||||
}
|
const group = {
|
||||||
for (const groupId of this.userForm.selectedGroups) {
|
id: groupId
|
||||||
const group = {
|
}
|
||||||
id: groupId
|
userObject.groups.push(group)
|
||||||
}
|
}
|
||||||
userObject.groups.push(group)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isAddNew) {
|
if (this.isAddNew) {
|
||||||
request.post('/user', userObject).then((res) => {
|
request.post('/user', userObject).then((res) => {
|
||||||
const response = res.data
|
const response = res.data
|
||||||
|
|||||||
Reference in New Issue
Block a user