From 9c7901f526aa1fefd35da044b73c1a77e4d67ff0 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 1 Jul 2021 18:01:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=88=86=E9=85=8D=E8=A7=92=E8=89=B2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysUserController.java | 27 ++++ .../ruoyi/system/service/ISysRoleService.java | 10 +- .../ruoyi/system/service/ISysUserService.java | 8 ++ .../service/impl/SysRoleServiceImpl.java | 25 ++++ .../service/impl/SysUserServiceImpl.java | 38 ++++++ ruoyi-ui/src/api/system/user.js | 17 +++ ruoyi-ui/src/assets/styles/ruoyi.scss | 18 +++ ruoyi-ui/src/router/index.js | 13 ++ ruoyi-ui/src/views/system/user/authRole.vue | 117 ++++++++++++++++++ ruoyi-ui/src/views/system/user/index.vue | 49 +++++--- 10 files changed, 306 insertions(+), 16 deletions(-) create mode 100644 ruoyi-ui/src/views/system/user/authRole.vue diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java index 03461ca95..692bd5ccb 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java @@ -202,4 +202,31 @@ public class SysUserController extends BaseController user.setUpdateBy(SecurityUtils.getUsername()); return toAjax(userService.updateUserStatus(user)); } + + /** + * 根据用户编号获取授权角色 + */ + @PreAuthorize("@ss.hasPermi('system:user:query')") + @GetMapping("/authRole/{userId}") + public AjaxResult authRole(@PathVariable("userId") Long userId) + { + AjaxResult ajax = AjaxResult.success(); + SysUser user = userService.selectUserById(userId); + List roles = roleService.selectRolesByUserId(userId); + ajax.put("user", user); + ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); + return ajax; + } + + /** + * 用户授权角色 + */ + @PreAuthorize("@ss.hasPermi('system:user:edit')") + @Log(title = "用户管理", businessType = BusinessType.GRANT) + @PutMapping("/authRole") + public AjaxResult insertAuthRole(Long userId, Long[] roleIds) + { + userService.insertUserAuth(userId, roleIds); + return success(); + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java index 4a4bee261..846ef6e7d 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java @@ -20,7 +20,15 @@ public interface ISysRoleService public List selectRoleList(SysRole role); /** - * 根据用户ID查询角色 + * 根据用户ID查询角色列表 + * + * @param userId 用户ID + * @return 角色列表 + */ + public List selectRolesByUserId(Long userId); + + /** + * 根据用户ID查询角色权限 * * @param userId 用户ID * @return 权限列表 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java index 00afa0011..5bf14880a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java @@ -96,6 +96,14 @@ public interface ISysUserService * @return 结果 */ public int updateUser(SysUser user); + + /** + * 用户授权角色 + * + * @param userId 用户ID + * @param roleIds 角色组 + */ + public void insertUserAuth(Long userId, Long[] roleIds); /** * 修改用户状态 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java index cecc6a2cb..409baf0aa 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java @@ -55,6 +55,31 @@ public class SysRoleServiceImpl implements ISysRoleService return roleMapper.selectRoleList(role); } + /** + * 根据用户ID查询角色 + * + * @param userId 用户ID + * @return 角色列表 + */ + @Override + public List selectRolesByUserId(Long userId) + { + List userRoles = roleMapper.selectRolePermissionByUserId(userId); + List roles = selectRoleAll(); + for (SysRole role : roles) + { + for (SysRole userRole : userRoles) + { + if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) + { + role.setFlag(true); + break; + } + } + } + return roles; + } + /** * 根据用户ID查询权限 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 065ae4818..3da749046 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java @@ -242,6 +242,18 @@ public class SysUserServiceImpl implements ISysUserService return userMapper.updateUser(user); } + /** + * 用户授权角色 + * + * @param userId 用户ID + * @param roleIds 角色组 + */ + public void insertUserAuth(Long userId, Long[] roleIds) + { + userRoleMapper.deleteUserRoleByUserId(userId); + insertUserRole(userId, roleIds); + } + /** * 修改用户状态 * @@ -356,6 +368,32 @@ public class SysUserServiceImpl implements ISysUserService } } + /** + * 新增用户角色信息 + * + * @param userId 用户ID + * @param roleIds 角色组 + */ + public void insertUserRole(Long userId, Long[] roleIds) + { + if (StringUtils.isNotNull(roleIds)) + { + // 新增用户与角色管理 + List list = new ArrayList(); + for (Long roleId : roleIds) + { + SysUserRole ur = new SysUserRole(); + ur.setUserId(userId); + ur.setRoleId(roleId); + list.add(ur); + } + if (list.size() > 0) + { + userRoleMapper.batchUserRole(list); + } + } + } + /** * 通过用户ID删除用户 * diff --git a/ruoyi-ui/src/api/system/user.js b/ruoyi-ui/src/api/system/user.js index 7f8db39ee..85bdc19cf 100644 --- a/ruoyi-ui/src/api/system/user.js +++ b/ruoyi-ui/src/api/system/user.js @@ -125,3 +125,20 @@ export function importTemplate() { method: 'get' }) } + +// 查询授权角色 +export function getAuthRole(userId) { + return request({ + url: '/system/user/authRole/' + userId, + method: 'get' + }) +} + +// 保存授权角色 +export function updateAuthRole(data) { + return request({ + url: '/system/user/authRole', + method: 'put', + params: data + }) +} diff --git a/ruoyi-ui/src/assets/styles/ruoyi.scss b/ruoyi-ui/src/assets/styles/ruoyi.scss index c40c9fb01..5927b6c91 100644 --- a/ruoyi-ui/src/assets/styles/ruoyi.scss +++ b/ruoyi-ui/src/assets/styles/ruoyi.scss @@ -53,6 +53,13 @@ margin-left: 20px; } +.h1, .h2, .h3, .h4, .h5, .h6, h1, h2, h3, h4, h5, h6 { + font-family: inherit; + font-weight: 500; + line-height: 1.1; + color: inherit; +} + .el-dialog:not(.is-fullscreen){ margin-top: 6vh !important; } @@ -120,6 +127,17 @@ width: inherit; } +/** 表格更多操作下拉样式 */ +.el-table .el-dropdown-link { + cursor: pointer; + color: #1890ff; + margin-left: 5px; +} + +.el-table .el-dropdown, .el-icon-arrow-down { + font-size: 12px; +} + .el-tree-node__content > .el-checkbox { margin-right: 8px; } diff --git a/ruoyi-ui/src/router/index.js b/ruoyi-ui/src/router/index.js index 3f14fd3a3..67fca82a1 100644 --- a/ruoyi-ui/src/router/index.js +++ b/ruoyi-ui/src/router/index.js @@ -80,6 +80,19 @@ export const constantRoutes = [ } ] }, + { + path: '/auth', + component: Layout, + hidden: true, + children: [ + { + path: 'role/:userId(\\d+)', + component: (resolve) => require(['@/views/system/user/authRole'], resolve), + name: 'AuthRole', + meta: { title: '分配角色'} + } + ] + }, { path: '/dict', component: Layout, diff --git a/ruoyi-ui/src/views/system/user/authRole.vue b/ruoyi-ui/src/views/system/user/authRole.vue new file mode 100644 index 000000000..e0cd7bceb --- /dev/null +++ b/ruoyi-ui/src/views/system/user/authRole.vue @@ -0,0 +1,117 @@ + + + \ No newline at end of file diff --git a/ruoyi-ui/src/views/system/user/index.vue b/ruoyi-ui/src/views/system/user/index.vue index b2c00eff6..7f419c990 100644 --- a/ruoyi-ui/src/views/system/user/index.vue +++ b/ruoyi-ui/src/views/system/user/index.vue @@ -175,21 +175,19 @@ @click="handleUpdate(scope.row)" v-hasPermi="['system:user:edit']" >修改 - 删除 - 重置 + + + 更多操作 + + + 删除用户 + 重置密码 + 分配角色 + + @@ -561,6 +559,22 @@ export default { this.single = selection.length != 1; this.multiple = !selection.length; }, + // 更多操作触发 + handleCommand(command, row) { + switch (command) { + case "handleDelete": + this.handleDelete(row); + break; + case "handleResetPwd": + this.handleResetPwd(row); + break; + case "handleAuthRole": + this.handleAuthRole(row); + break; + default: + break; + } + }, /** 新增按钮操作 */ handleAdd() { this.reset(); @@ -603,6 +617,11 @@ export default { }); }).catch(() => {}); }, + /** 分配角色操作 */ + handleAuthRole: function(row) { + const userId = row.userId; + this.$router.push("/auth/role/" + userId); + }, /** 提交按钮 */ submitForm: function() { this.$refs["form"].validate(valid => {