用户删除、重置密码,分配角色.

This commit is contained in:
许晓东
2023-05-09 07:16:55 +08:00
parent 38ca2cfc52
commit da1ddeb1e7
8 changed files with 312 additions and 10 deletions

View File

@@ -20,6 +20,8 @@ public class SysUserDTO {
private String roleIds;
private Boolean resetPassword = false;
public SysUserDO toDO() {
SysUserDO userDO = new SysUserDO();
userDO.setId(this.id);

View File

@@ -18,6 +18,8 @@ public class SysUserVO {
private String roleIds;
private String roleNames;
public static SysUserVO from(SysUserDO userDO) {
SysUserVO userVO = new SysUserVO();
userVO.setId(userDO.getId());

View File

@@ -15,12 +15,15 @@ import com.xuxd.kafka.console.dao.SysPermissionMapper;
import com.xuxd.kafka.console.dao.SysRoleMapper;
import com.xuxd.kafka.console.dao.SysUserMapper;
import com.xuxd.kafka.console.service.UserManageService;
import com.xuxd.kafka.console.utils.RandomStringUtil;
import com.xuxd.kafka.console.utils.UUIDStrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -64,8 +67,18 @@ public class UserManageServiceImpl implements UserManageService {
@Override
public ResponseData addOrUpdateUser(SysUserDTO userDTO) {
if (userDTO.getId() == null) {
if (StringUtils.isEmpty(userDTO.getPassword())) {
userDTO.setPassword(RandomStringUtil.random6Str());
}
SysUserDO userDO = userDTO.toDO();
QueryWrapper<SysUserDO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(true, "username", userDO.getUsername());
SysUserDO exist = userMapper.selectOne(queryWrapper);
if (exist != null) {
return ResponseData.create().failed("用户已存在:" + userDO.getUsername());
}
userDO.setSalt(UUIDStrUtil.random());
userDO.setPassword(UUIDStrUtil.generate(userDTO.getPassword(), userDO.getSalt()));
userMapper.insert(userDTO.toDO());
@@ -76,14 +89,16 @@ public class UserManageServiceImpl implements UserManageService {
return ResponseData.create().failed("Unknown User.");
}
// 判断是否更新密码
String oldPass = UUIDStrUtil.generate(userDO.getPassword(), userDO.getSalt());
if (!userDTO.getPassword().equals(oldPass)) {
if (userDTO.getResetPassword()) {
userDTO.setPassword(RandomStringUtil.random6Str());
userDO.setSalt(UUIDStrUtil.random());
userDO.setPassword(UUIDStrUtil.generate(userDTO.getPassword(), userDO.getSalt()));
}
userDO.setRoleIds(userDTO.getRoleIds());
userDO.setUsername(userDTO.getUsername());
userMapper.updateById(userDO);
}
return ResponseData.create().success();
return ResponseData.create().data(userDTO.getPassword()).success();
}
@Override
@@ -151,7 +166,16 @@ public class UserManageServiceImpl implements UserManageService {
public ResponseData selectUser() {
QueryWrapper<SysUserDO> queryWrapper = new QueryWrapper<>();
List<SysUserDO> userDOS = userMapper.selectList(queryWrapper);
return ResponseData.create().data(userDOS.stream().map(SysUserVO::from).collect(Collectors.toList())).success();
List<SysRoleDO> roleDOS = roleMapper.selectList(null);
Map<Long, SysRoleDO> roleDOMap = roleDOS.stream().collect(Collectors.toMap(SysRoleDO::getId, Function.identity(), (e1, e2) -> e1));
List<SysUserVO> voList = userDOS.stream().map(SysUserVO::from).collect(Collectors.toList());
voList.forEach(vo -> {
if (vo.getRoleIds() != null) {
Long roleId = Long.valueOf(vo.getRoleIds());
vo.setRoleNames(roleDOMap.containsKey(roleId) ? roleDOMap.get(roleId).getRoleName() : null);
}
});
return ResponseData.create().data(voList).success();
}
@Override

View File

@@ -0,0 +1,26 @@
package com.xuxd.kafka.console.utils;
import java.util.Random;
/**
* @author: xuxd
* @date: 2023/5/8 9:19
**/
public class RandomStringUtil {
private final static String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
public static String random6Str() {
return generateRandomString(6);
}
public static String generateRandomString(int length) {
Random random = new Random();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = random.nextInt(ALLOWED_CHARS.length());
sb.append(ALLOWED_CHARS.charAt(index));
}
return sb.toString();
}
}

View File

@@ -0,0 +1,40 @@
<template>
<a-modal
title="消息提示"
:visible="visible"
:width="400"
:mask="false"
:destroyOnClose="true"
:footer="null"
:maskClosable="false"
@cancel="handleCancel"
>
<div>
{{ message }}
</div>
</a-modal>
</template>
<script>
export default {
name: "MessageBox",
props: {
message: {
type: String,
default: "",
},
visible: {
type: Boolean,
default: false,
},
},
methods: {
handleCancel() {
this.visible = false;
this.$emit("closeMessageBox", {});
},
},
};
</script>
<style scoped></style>

View File

@@ -95,7 +95,7 @@ export default {
this.loading = false;
if (res.code == 0) {
this.$message.success(res.msg);
this.$emit("closeCreateUserDialog", { refresh: true });
this.$emit("closeCreateUserDialog", { refresh: true, data: res.data });
} else {
notification.error({
message: "error",

View File

@@ -0,0 +1,138 @@
<template>
<a-modal
title="分配用户角色"
:visible="show"
:width="800"
:mask="false"
:destroyOnClose="true"
:footer="null"
:maskClosable="false"
@cancel="handleCancel"
>
<div>
<a-spin :spinning="loading">
<a-form
:form="form"
:label-col="{ span: 5 }"
:wrapper-col="{ span: 12 }"
@submit="handleSubmit"
>
<a-form-item label="用户名">
<a-input
:disabled="true"
v-decorator="['username', { initialValue: user.username }]"
/>
</a-form-item>
<a-form-item label="角色">
<a-select
show-search
option-filter-prop="children"
v-decorator="[
'roleIds',
{ rules: [{ required: true, message: '请选择一个角色!' }] },
]"
placeholder="请选择一个角色"
>
<a-select-option
v-for="role in roles"
:key="role.id"
:value="role.id"
>
{{ role.roleName }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item :wrapper-col="{ span: 12, offset: 5 }">
<a-button type="primary" html-type="submit"> 提交</a-button>
</a-form-item>
</a-form>
</a-spin>
</div>
</a-modal>
</template>
<script>
import request from "@/utils/request";
import notification from "ant-design-vue/es/notification";
import { UserManageApi } from "@/utils/api";
export default {
name: "UpdateUserRole",
props: {
visible: {
type: Boolean,
default: false,
},
user: {},
},
data() {
return {
show: this.visible,
loading: false,
form: this.$form.createForm(this, { name: "coordinated" }),
roles: [],
};
},
watch: {
visible(v) {
this.show = v;
},
},
methods: {
handleSubmit(e) {
e.preventDefault();
this.form.validateFields((err, values) => {
if (!err) {
const params = Object.assign({}, this.user, values);
params.roleIds = values.roleIds;
this.loading = true;
request({
url: UserManageApi.addOrUpdateUser.url,
method: UserManageApi.addOrUpdateUser.method,
data: params,
}).then((res) => {
this.loading = false;
if (res.code == 0) {
this.$message.success(res.msg);
this.$emit("closeUpdateUserRoleDialog", {
refresh: true,
data: res.data,
});
} else {
notification.error({
message: "error",
description: res.msg,
});
}
});
}
});
},
getRoles() {
this.loading = true;
request({
url: UserManageApi.getRole.url,
method: UserManageApi.getRole.method,
}).then((res) => {
this.loading = false;
if (res.code == 0) {
this.roles = res.data;
} else {
notification.error({
message: "error",
description: res.msg,
});
}
});
},
handleCancel() {
this.$emit("closeUpdateUserRoleDialog", { refresh: false });
},
},
created() {
this.getRoles();
},
};
</script>
<style scoped></style>

View File

@@ -53,8 +53,22 @@
>删除
</a-button>
</a-popconfirm>
<a-button size="small" href="javascript:;" class="operation-btn"
>重置密码
<a-popconfirm
:title="'重置用户: ' + record.username + '密码?'"
ok-text="确认"
cancel-text="取消"
@confirm="resetPassword(record)"
>
<a-button size="small" href="javascript:;" class="operation-btn"
>重置密码
</a-button>
</a-popconfirm>
<a-button
size="small"
href="javascript:;"
class="operation-btn"
@click="openUpdateUserRoleDialog(record)"
>分配角色
</a-button>
</div>
</a-table>
@@ -62,6 +76,16 @@
@closeCreateUserDialog="closeCreateUserDialog"
:visible="showCreateUserDialog"
></CreateUser>
<MessageBox
:visible="showMessageBox"
:message="messageBoxContent"
@closeMessageBox="closeMessageBox"
></MessageBox>
<UpdateUserRole
:visible="showUpdateUserRole"
:user="selectUser"
@closeUpdateUserRoleDialog="closeUpdateUserRoleDialog"
></UpdateUserRole>
</a-spin>
</div>
</template>
@@ -72,10 +96,12 @@ import request from "@/utils/request";
import notification from "ant-design-vue/lib/notification";
import { UserManageApi } from "@/utils/api";
import CreateUser from "@/views/user/CreateUser.vue";
import MessageBox from "@/views/components/MessageBox.vue";
import UpdateUserRole from "@/views/user/UpdateUserRole.vue";
export default {
name: "User",
components: { CreateUser },
components: { CreateUser, MessageBox, UpdateUserRole },
props: {
topicList: {
type: Array,
@@ -89,6 +115,10 @@ export default {
filteredData: [],
filterUsername: "",
showCreateUserDialog: false,
showMessageBox: false,
showUpdateUserRole: false,
messageBoxContent: "",
selectUser: {},
columns: [
{
title: "用户名",
@@ -97,8 +127,8 @@ export default {
},
{
title: "角色",
dataIndex: "roleIds",
key: "roleIds",
dataIndex: "roleNames",
key: "roleNames",
},
{
title: "操作",
@@ -150,8 +180,23 @@ export default {
this.showCreateUserDialog = false;
if (p.refresh) {
this.refresh();
this.messageBoxContent = "用户初始密码:" + p.data;
this.showMessageBox = true;
}
},
openUpdateUserRoleDialog(user) {
this.selectUser = user;
this.showUpdateUserRole = true;
},
closeUpdateUserRoleDialog(p) {
this.showUpdateUserRole = false;
if (p.refresh) {
this.refresh();
}
},
closeMessageBox() {
this.showMessageBox = false;
},
deleteUser(user) {
this.loading = true;
request({
@@ -169,6 +214,27 @@ export default {
}
});
},
resetPassword(record) {
this.loading = true;
const params = Object.assign({}, record);
params.resetPassword = true;
request({
url: UserManageApi.addOrUpdateUser.url,
method: UserManageApi.addOrUpdateUser.method,
data: params,
}).then((res) => {
this.loading = false;
if (res.code == 0) {
this.messageBoxContent = "密码重置成功,新密码:" + res.data;
this.showMessageBox = true;
} else {
notification.error({
message: "error",
description: res.msg,
});
}
});
},
},
created() {
this.handleSearch();
@@ -222,4 +288,8 @@ export default {
margin-bottom: 5px;
margin-top: 5px;
}
.operation-btn {
margin-right: 3%;
}
</style>