Parcourir la source

用户管理新增分配角色功能

RuoYi il y a 4 ans
Parent
commit
3d47ec2e73

+ 27 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java

@@ -246,4 +246,31 @@ public class SysUserController extends BaseController
246 246
         user.setUpdateBy(SecurityUtils.getUsername());
247 247
         return toAjax(userService.updateUserStatus(user));
248 248
     }
249
+
250
+    /**
251
+     * 根据用户编号获取授权角色
252
+     */
253
+    @PreAuthorize(hasPermi = "system:user:query")
254
+    @GetMapping("/authRole/{userId}")
255
+    public AjaxResult authRole(@PathVariable("userId") Long userId)
256
+    {
257
+        AjaxResult ajax = AjaxResult.success();
258
+        SysUser user = userService.selectUserById(userId);
259
+        List<SysRole> roles = roleService.selectRolesByUserId(userId);
260
+        ajax.put("user", user);
261
+        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
262
+        return ajax;
263
+    }
264
+
265
+    /**
266
+     * 用户授权角色
267
+     */
268
+    @PreAuthorize(hasPermi = "system:user:edit")
269
+    @Log(title = "用户管理", businessType = BusinessType.GRANT)
270
+    @PutMapping("/authRole")
271
+    public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
272
+    {
273
+        userService.insertUserAuth(userId, roleIds);
274
+        return success();
275
+    }
249 276
 }

+ 9 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java

@@ -21,7 +21,15 @@ public interface ISysRoleService
21 21
     public List<SysRole> selectRoleList(SysRole role);
22 22
 
23 23
     /**
24
-     * 根据用户ID查询角色
24
+     * 根据用户ID查询角色列表
25
+     * 
26
+     * @param userId 用户ID
27
+     * @return 角色列表
28
+     */
29
+    public List<SysRole> selectRolesByUserId(Long userId);
30
+
31
+    /**
32
+     * 根据用户ID查询角色权限
25 33
      * 
26 34
      * @param userId 用户ID
27 35
      * @return 权限列表

+ 8 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java

@@ -97,6 +97,14 @@ public interface ISysUserService
97 97
      * @return 结果
98 98
      */
99 99
     public int updateUser(SysUser user);
100
+    
101
+    /**
102
+     * 用户授权角色
103
+     * 
104
+     * @param userId 用户ID
105
+     * @param roleIds 角色组
106
+     */
107
+    public void insertUserAuth(Long userId, Long[] roleIds);
100 108
 
101 109
     /**
102 110
      * 修改用户状态

+ 25 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java

@@ -56,6 +56,31 @@ public class SysRoleServiceImpl implements ISysRoleService
56 56
     }
57 57
 
58 58
     /**
59
+     * 根据用户ID查询角色
60
+     * 
61
+     * @param userId 用户ID
62
+     * @return 角色列表
63
+     */
64
+    @Override
65
+    public List<SysRole> selectRolesByUserId(Long userId)
66
+    {
67
+        List<SysRole> userRoles = roleMapper.selectRolePermissionByUserId(userId);
68
+        List<SysRole> roles = selectRoleAll();
69
+        for (SysRole role : roles)
70
+        {
71
+            for (SysRole userRole : userRoles)
72
+            {
73
+                if (role.getRoleId().longValue() == userRole.getRoleId().longValue())
74
+                {
75
+                    role.setFlag(true);
76
+                    break;
77
+                }
78
+            }
79
+        }
80
+        return roles;
81
+    }
82
+
83
+    /**
59 84
      * 根据用户ID查询权限
60 85
      * 
61 86
      * @param userId 用户ID

+ 38 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java

@@ -243,6 +243,18 @@ public class SysUserServiceImpl implements ISysUserService
243 243
     }
244 244
 
245 245
     /**
246
+     * 用户授权角色
247
+     * 
248
+     * @param userId 用户ID
249
+     * @param roleIds 角色组
250
+     */
251
+    public void insertUserAuth(Long userId, Long[] roleIds)
252
+    {
253
+        userRoleMapper.deleteUserRoleByUserId(userId);
254
+        insertUserRole(userId, roleIds);
255
+    }
256
+
257
+    /**
246 258
      * 修改用户状态
247 259
      * 
248 260
      * @param user 用户信息
@@ -357,6 +369,32 @@ public class SysUserServiceImpl implements ISysUserService
357 369
     }
358 370
 
359 371
     /**
372
+     * 新增用户角色信息
373
+     * 
374
+     * @param userId 用户ID
375
+     * @param roleIds 角色组
376
+     */
377
+    public void insertUserRole(Long userId, Long[] roleIds)
378
+    {
379
+        if (StringUtils.isNotNull(roleIds))
380
+        {
381
+            // 新增用户与角色管理
382
+            List<SysUserRole> list = new ArrayList<SysUserRole>();
383
+            for (Long roleId : roleIds)
384
+            {
385
+                SysUserRole ur = new SysUserRole();
386
+                ur.setUserId(userId);
387
+                ur.setRoleId(roleId);
388
+                list.add(ur);
389
+            }
390
+            if (list.size() > 0)
391
+            {
392
+                userRoleMapper.batchUserRole(list);
393
+            }
394
+        }
395
+    }
396
+
397
+    /**
360 398
      * 通过用户ID删除用户
361 399
      * 
362 400
      * @param userId 用户ID

+ 17 - 0
ruoyi-ui/src/api/system/user.js

@@ -108,3 +108,20 @@ export function uploadAvatar(data) {
108 108
     data: data
109 109
   })
110 110
 }
111
+
112
+// 查询授权角色
113
+export function getAuthRole(userId) {
114
+  return request({
115
+    url: '/system/user/authRole/' + userId,
116
+    method: 'get'
117
+  })
118
+}
119
+
120
+// 保存授权角色
121
+export function updateAuthRole(data) {
122
+  return request({
123
+    url: '/system/user/authRole',
124
+    method: 'put',
125
+    params: data
126
+  })
127
+}

+ 18 - 0
ruoyi-ui/src/assets/styles/ruoyi.scss

@@ -53,6 +53,13 @@
53 53
 	margin-left: 20px;
54 54
 }
55 55
 
56
+.h1, .h2, .h3, .h4, .h5, .h6, h1, h2, h3, h4, h5, h6 {
57
+	font-family: inherit;
58
+	font-weight: 500;
59
+	line-height: 1.1;
60
+	color: inherit;
61
+}
62
+
56 63
 .el-dialog:not(.is-fullscreen){
57 64
 	margin-top: 6vh !important;
58 65
 }
@@ -120,6 +127,17 @@
120 127
 	width: inherit;
121 128
 }
122 129
 
130
+/** 表格更多操作下拉样式 */
131
+.el-table .el-dropdown-link {
132
+	cursor: pointer;
133
+	color: #1890ff;
134
+	margin-left: 5px;
135
+}
136
+
137
+.el-table .el-dropdown, .el-icon-arrow-down {
138
+	font-size: 12px;
139
+}
140
+
123 141
 .el-tree-node__content > .el-checkbox {
124 142
 	margin-right: 8px;
125 143
 }

+ 13 - 0
ruoyi-ui/src/router/index.js

@@ -81,6 +81,19 @@ export const constantRoutes = [
81 81
     ]
82 82
   },
83 83
   {
84
+    path: '/auth',
85
+    component: Layout,
86
+    hidden: true,
87
+    children: [
88
+      {
89
+        path: 'role/:userId(\\d+)',
90
+        component: (resolve) => require(['@/views/system/user/authRole'], resolve),
91
+        name: 'AuthRole',
92
+        meta: { title: '分配角色'}
93
+      }
94
+    ]
95
+  },
96
+  {
84 97
     path: '/dict',
85 98
     component: Layout,
86 99
     hidden: true,

+ 117 - 0
ruoyi-ui/src/views/system/user/authRole.vue

@@ -0,0 +1,117 @@
1
+<template>
2
+  <div class="app-container">
3
+    <h4 class="form-header h4">基本信息</h4>
4
+    <el-form ref="form" :model="form" label-width="80px">
5
+      <el-row>
6
+        <el-col :span="8" :offset="2">
7
+          <el-form-item label="用户昵称" prop="nickName">
8
+            <el-input v-model="form.nickName" disabled />
9
+          </el-form-item>
10
+        </el-col>
11
+        <el-col :span="8" :offset="2">
12
+          <el-form-item label="登录账号" prop="phonenumber">
13
+            <el-input  v-model="form.userName" disabled />
14
+          </el-form-item>
15
+        </el-col>
16
+      </el-row>
17
+    </el-form>
18
+
19
+    <h4 class="form-header h4">角色信息</h4>
20
+    <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange" :data="roles.slice((pageNum-1)*pageSize,pageNum*pageSize)">
21
+      <el-table-column label="序号" type="index" align="center">
22
+        <template slot-scope="scope">
23
+          <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
24
+        </template>
25
+      </el-table-column>
26
+      <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column>
27
+      <el-table-column label="角色编号" align="center" prop="roleId" />
28
+      <el-table-column label="角色名称" align="center" prop="roleName" />
29
+      <el-table-column label="权限字符" align="center" prop="roleKey" />
30
+      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
31
+        <template slot-scope="scope">
32
+          <span>{{ parseTime(scope.row.createTime) }}</span>
33
+        </template>
34
+      </el-table-column>
35
+    </el-table>
36
+    
37
+    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" />
38
+
39
+    <el-form label-width="100px">
40
+      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
41
+        <el-button type="primary" @click="submitForm()">提交</el-button>
42
+        <el-button @click="close()">返回</el-button>
43
+      </el-form-item>
44
+    </el-form>
45
+  </div>
46
+</template>
47
+
48
+<script>
49
+import { getAuthRole, updateAuthRole } from "@/api/system/user";
50
+
51
+export default {
52
+  name: "AuthRole",
53
+  data() {
54
+    return {
55
+       // 遮罩层
56
+      loading: true,
57
+      // 分页信息
58
+      total: 0,
59
+      pageNum: 1,
60
+      pageSize: 10,
61
+      // 选中角色编号
62
+      roleIds:[],
63
+      // 角色信息
64
+      roles: [],
65
+      // 用户信息
66
+      form: {}
67
+    };
68
+  },
69
+  created() {
70
+    const userId = this.$route.params && this.$route.params.userId;
71
+    if (userId) {
72
+      this.loading = true;
73
+      getAuthRole(userId).then((response) => {
74
+        this.form = response.user;
75
+        this.roles = response.roles;
76
+        this.total = this.roles.length;
77
+        this.$nextTick(() => {
78
+          this.roles.forEach((row) => {
79
+            if (row.flag) {
80
+              this.$refs.table.toggleRowSelection(row);
81
+            }
82
+          });
83
+        });
84
+        this.loading = false;
85
+      });
86
+    }
87
+  },
88
+  methods: {
89
+    /** 单击选中行数据 */
90
+    clickRow(row) {
91
+      this.$refs.table.toggleRowSelection(row);
92
+    },
93
+    // 多选框选中数据
94
+    handleSelectionChange(selection) {
95
+      this.roleIds = selection.map((item) => item.roleId);
96
+    },
97
+    // 保存选中的数据编号
98
+    getRowKey(row) {
99
+      return row.roleId;
100
+    },
101
+    /** 提交按钮 */
102
+    submitForm() {
103
+      const userId = this.form.userId;
104
+      const roleIds = this.roleIds.join(",");
105
+      updateAuthRole({ userId: userId, roleIds: roleIds }).then((response) => {
106
+        this.msgSuccess("授权成功");
107
+        this.close();
108
+      });
109
+    },
110
+    /** 关闭按钮 */
111
+    close() {
112
+      this.$store.dispatch("tagsView/delView", this.$route);
113
+      this.$router.push({ path: "/system/user" });
114
+    },
115
+  },
116
+};
117
+</script>

+ 34 - 15
ruoyi-ui/src/views/system/user/index.vue

@@ -174,21 +174,19 @@
174 174
                 @click="handleUpdate(scope.row)"
175 175
                 v-hasPermi="['system:user:edit']"
176 176
               >修改</el-button>
177
-              <el-button
178
-                v-if="scope.row.userId !== 1"
179
-                size="mini"
180
-                type="text"
181
-                icon="el-icon-delete"
182
-                @click="handleDelete(scope.row)"
183
-                v-hasPermi="['system:user:remove']"
184
-              >删除</el-button>
185
-              <el-button
186
-                size="mini"
187
-                type="text"
188
-                icon="el-icon-key"
189
-                @click="handleResetPwd(scope.row)"
190
-                v-hasPermi="['system:user:resetPwd']"
191
-              >重置</el-button>
177
+              <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">
178
+                <span class="el-dropdown-link">
179
+                  <i class="el-icon-d-arrow-right el-icon--right"></i>更多操作
180
+                </span>
181
+                <el-dropdown-menu slot="dropdown">
182
+                  <el-dropdown-item command="handleDelete" v-if="scope.row.userId !== 1" icon="el-icon-delete"
183
+                    v-hasPermi="['system:user:remove']">删除用户</el-dropdown-item>
184
+                  <el-dropdown-item command="handleResetPwd" icon="el-icon-key"
185
+                    v-hasPermi="['system:user:resetPwd']">重置密码</el-dropdown-item>
186
+                  <el-dropdown-item command="handleAuthRole" icon="el-icon-circle-check"
187
+                    v-hasPermi="['system:user:edit']">分配角色</el-dropdown-item>
188
+                </el-dropdown-menu>
189
+              </el-dropdown>
192 190
             </template>
193 191
           </el-table-column>
194 192
         </el-table>
@@ -558,6 +556,22 @@ export default {
558 556
       this.single = selection.length != 1;
559 557
       this.multiple = !selection.length;
560 558
     },
559
+    // 更多操作触发
560
+    handleCommand(command, row) {
561
+      switch (command) {
562
+        case "handleDelete":
563
+          this.handleDelete(row);
564
+          break;
565
+        case "handleResetPwd":
566
+          this.handleResetPwd(row);
567
+          break;
568
+        case "handleAuthRole":
569
+          this.handleAuthRole(row);
570
+          break;
571
+        default:
572
+          break;
573
+      }
574
+    },
561 575
     /** 新增按钮操作 */
562 576
     handleAdd() {
563 577
       this.reset();
@@ -600,6 +614,11 @@ export default {
600 614
           });
601 615
         }).catch(() => {});
602 616
     },
617
+    /** 分配角色操作 */
618
+    handleAuthRole: function(row) {
619
+      const userId = row.userId;
620
+      this.$router.push("/auth/role/" + userId);
621
+    },
603 622
     /** 提交按钮 */
604 623
     submitForm: function() {
605 624
       this.$refs["form"].validate(valid => {