mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-05 06:51: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<>();
|
||||
|
||||
@@ -53,7 +53,12 @@
|
||||
/>
|
||||
</el-form-item>
|
||||
<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
|
||||
v-for="role in roles"
|
||||
:key="role.id"
|
||||
@@ -63,7 +68,12 @@
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<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
|
||||
v-for="group in groups"
|
||||
:key="group.id"
|
||||
@@ -132,7 +142,9 @@ export default {
|
||||
message: '密码不能为空'
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
disableSelectRoles: false,
|
||||
disableSelectGroups: false
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
@@ -205,10 +217,18 @@ export default {
|
||||
this.userForm.selectedRoles = []
|
||||
this.userForm.selectedGroups = []
|
||||
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) {
|
||||
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.dialogVisible = true
|
||||
@@ -237,6 +257,9 @@ export default {
|
||||
handleDialogOpen() {
|
||||
this.getRoles()
|
||||
|
||||
this.disableSelectRoles = false
|
||||
this.disableSelectGroups = false
|
||||
|
||||
if (this.isAddNew) {
|
||||
this.userForm.inputUsername = ''
|
||||
this.userForm.inputPassword = ''
|
||||
@@ -245,6 +268,10 @@ export default {
|
||||
this.dialogTitle = '添加用户'
|
||||
} else {
|
||||
this.dialogTitle = '编辑用户'
|
||||
if (this.editUserId === '1') {
|
||||
this.disableSelectRoles = true
|
||||
this.disableSelectGroups = true
|
||||
}
|
||||
}
|
||||
},
|
||||
async handleSubmit() {
|
||||
@@ -258,18 +285,21 @@ export default {
|
||||
roles: [],
|
||||
groups: []
|
||||
}
|
||||
for (const roleId of this.userForm.selectedRoles) {
|
||||
const role = {
|
||||
id: roleId
|
||||
if (this.editUserId !== '1') {
|
||||
for (const roleId of this.userForm.selectedRoles) {
|
||||
const role = {
|
||||
id: roleId
|
||||
}
|
||||
userObject.roles.push(role)
|
||||
}
|
||||
userObject.roles.push(role)
|
||||
}
|
||||
for (const groupId of this.userForm.selectedGroups) {
|
||||
const group = {
|
||||
id: groupId
|
||||
for (const groupId of this.userForm.selectedGroups) {
|
||||
const group = {
|
||||
id: groupId
|
||||
}
|
||||
userObject.groups.push(group)
|
||||
}
|
||||
userObject.groups.push(group)
|
||||
}
|
||||
|
||||
if (this.isAddNew) {
|
||||
request.post('/user', userObject).then((res) => {
|
||||
const response = res.data
|
||||
|
||||
Reference in New Issue
Block a user