ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java
@@ -181,9 +181,7 @@ @GetMapping("/authUser/allocatedList") public TableDataInfo allocatedList(SysUser user) { startPage(); List<SysUser> list = userService.selectAllocatedList(user); return getDataTable(list); return userService.selectAllocatedList(user); } /** @@ -193,9 +191,7 @@ @GetMapping("/authUser/unallocatedList") public TableDataInfo unallocatedList(SysUser user) { startPage(); List<SysUser> list = userService.selectUnallocatedList(user); return getDataTable(list); return userService.selectUnallocatedList(user); } /** ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java
@@ -204,12 +204,12 @@ @GetMapping("/authRole/{userId}") public AjaxResult authRole(@PathVariable("userId") Long userId) { AjaxResult ajax = AjaxResult.success(); SysUser user = userService.selectUserById(userId); List<SysRole> roles = roleService.selectRolesByUserId(userId); Map<String, Object> ajax = new HashMap<>(); ajax.put("user", user); ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); return ajax; return AjaxResult.success(ajax); } /** ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java
@@ -30,7 +30,7 @@ * @param user 用户信息 * @return 用户信息集合信息 */ public List<SysUser> selectAllocatedList(SysUser user); public List<SysUser> selectAllocatedList(@Param("page") Page<SysUser> page, @Param("user") SysUser user); /** * 根据条件分页查询未分配用户角色列表 @@ -38,7 +38,7 @@ * @param user 用户信息 * @return 用户信息集合信息 */ public List<SysUser> selectUnallocatedList(SysUser user); public List<SysUser> selectUnallocatedList(@Param("page") Page<SysUser> page, @Param("user") SysUser user); /** * 通过用户名查询用户 ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
@@ -30,7 +30,7 @@ * @param user 用户信息 * @return 用户信息集合信息 */ public List<SysUser> selectAllocatedList(SysUser user); public TableDataInfo<SysUser> selectAllocatedList(SysUser user); /** * 根据条件分页查询未分配用户角色列表 @@ -38,7 +38,7 @@ * @param user 用户信息 * @return 用户信息集合信息 */ public List<SysUser> selectUnallocatedList(SysUser user); public TableDataInfo<SysUser> selectUnallocatedList(SysUser user); /** * 通过用户名查询用户 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java
@@ -66,16 +66,12 @@ * @return 角色列表 */ @Override public List<SysRole> selectRolesByUserId(Long userId) { List<SysRole> userRoles = roleMapper.selectRolePermissionByUserId(userId); public List<SysRole> selectRolesByUserId(Long userId) { List<SysRole> userRoles = baseMapper.selectRolePermissionByUserId(userId); List<SysRole> roles = selectRoleAll(); for (SysRole role : roles) { for (SysRole userRole : userRoles) { if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) { for (SysRole role : roles) { for (SysRole userRole : userRoles) { if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) { role.setFlag(true); break; } @@ -338,9 +334,10 @@ * @return 结果 */ @Override public int deleteAuthUser(SysUserRole userRole) { return userRoleMapper.deleteUserRoleInfo(userRole); public int deleteAuthUser(SysUserRole userRole) { return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() .eq(SysUserRole::getRoleId, userRole.getRoleId()) .eq(SysUserRole::getUserId, userRole.getUserId())); } /** @@ -351,9 +348,10 @@ * @return 结果 */ @Override public int deleteAuthUsers(Long roleId, Long[] userIds) { return userRoleMapper.deleteUserRoleInfos(roleId, userIds); public int deleteAuthUsers(Long roleId, Long[] userIds) { return userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() .eq(SysUserRole::getRoleId, roleId) .in(SysUserRole::getUserId, Arrays.asList(userIds))); } /** @@ -364,17 +362,15 @@ * @return 结果 */ @Override public int insertAuthUsers(Long roleId, Long[] userIds) { public int insertAuthUsers(Long roleId, Long[] userIds) { // 新增用户与角色管理 List<SysUserRole> list = new ArrayList<SysUserRole>(); for (Long userId : userIds) { for (Long userId : userIds) { SysUserRole ur = new SysUserRole(); ur.setUserId(userId); ur.setRoleId(roleId); list.add(ur); } return userRoleMapper.batchUserRole(list); return userRoleMapper.insertAll(list); } } ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
@@ -76,10 +76,9 @@ * @return 用户信息集合信息 */ @Override @DataScope(deptAlias = "d", userAlias = "u") public List<SysUser> selectAllocatedList(SysUser user) { return userMapper.selectAllocatedList(user); @DataScope(deptAlias = "d", userAlias = "u", isUser = true) public TableDataInfo<SysUser> selectAllocatedList(SysUser user) { return PageUtils.buildDataInfo(baseMapper.selectAllocatedList(PageUtils.buildPage(), user)); } /** @@ -89,10 +88,9 @@ * @return 用户信息集合信息 */ @Override @DataScope(deptAlias = "d", userAlias = "u") public List<SysUser> selectUnallocatedList(SysUser user) { return userMapper.selectUnallocatedList(user); @DataScope(deptAlias = "d", userAlias = "u", isUser = true) public TableDataInfo<SysUser> selectUnallocatedList(SysUser user) { return PageUtils.buildDataInfo(baseMapper.selectUnallocatedList(PageUtils.buildPage(), user)); } /** @@ -266,7 +264,8 @@ @Override public void insertUserAuth(Long userId, Long[] roleIds) { userRoleMapper.deleteUserRoleByUserId(userId); userRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>() .eq(SysUserRole::getUserId, userId)); insertUserRole(userId, roleIds); } @@ -383,22 +382,18 @@ * @param userId 用户ID * @param roleIds 角色组 */ public void insertUserRole(Long userId, Long[] roleIds) { if (StringUtils.isNotNull(roleIds)) { public void insertUserRole(Long userId, Long[] roleIds) { if (Validator.isNotNull(roleIds)) { // 新增用户与角色管理 List<SysUserRole> list = new ArrayList<SysUserRole>(); for (Long roleId : roleIds) { for (Long roleId : roleIds) { SysUserRole ur = new SysUserRole(); ur.setUserId(userId); ur.setRoleId(roleId); list.add(ur); } if (list.size() > 0) { userRoleMapper.batchUserRole(list); if (list.size() > 0) { userRoleMapper.insertAll(list); } } } ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml
@@ -148,16 +148,16 @@ left join sys_dept d on u.dept_id = d.dept_id left join sys_user_role ur on u.user_id = ur.user_id left join sys_role r on r.role_id = ur.role_id where u.del_flag = '0' and r.role_id = #{roleId} <if test="userName != null and userName != ''"> AND u.user_name like concat('%', #{userName}, '%') where u.del_flag = '0' and r.role_id = #{user.roleId} <if test="user.userName != null and user.userName != ''"> AND u.user_name like concat('%', #{user.userName}, '%') </if> <if test="phonenumber != null and phonenumber != ''"> AND u.phonenumber like concat('%', #{phonenumber}, '%') <if test="user.phonenumber != null and user.phonenumber != ''"> AND u.phonenumber like concat('%', #{user.phonenumber}, '%') </if> <!-- 数据范围过滤 --> <if test="params.dataScope != null and params.dataScope != ''"> AND ( ${params.dataScope} ) <if test="user.params.dataScope != null and user.params.dataScope != ''"> AND ( ${user.params.dataScope} ) </if> </select> @@ -167,17 +167,17 @@ left join sys_dept d on u.dept_id = d.dept_id left join sys_user_role ur on u.user_id = ur.user_id left join sys_role r on r.role_id = ur.role_id where u.del_flag = '0' and (r.role_id != #{roleId} or r.role_id IS NULL) and u.user_id not in (select u.user_id from sys_user u inner join sys_user_role ur on u.user_id = ur.user_id and ur.role_id = #{roleId}) <if test="userName != null and userName != ''"> AND u.user_name like concat('%', #{userName}, '%') where u.del_flag = '0' and (r.role_id != #{user.roleId} or r.role_id IS NULL) and u.user_id not in (select u.user_id from sys_user u inner join sys_user_role ur on u.user_id = ur.user_id and ur.role_id = #{user.roleId}) <if test="user.userName != null and user.userName != ''"> AND u.user_name like concat('%', #{user.userName}, '%') </if> <if test="phonenumber != null and phonenumber != ''"> AND u.phonenumber like concat('%', #{phonenumber}, '%') <if test="user.phonenumber != null and user.phonenumber != ''"> AND u.phonenumber like concat('%', #{user.phonenumber}, '%') </if> <!-- 数据范围过滤 --> <if test="params.dataScope != null and params.dataScope != ''"> AND ( ${params.dataScope} ) <if test="user.params.dataScope != null and user.params.dataScope != ''"> AND ( ${user.params.dataScope} ) </if> </select> ruoyi-ui/src/views/system/user/authRole.vue
@@ -1,117 +1,117 @@ <template> <div class="app-container"> <h4 class="form-header h4">基本信息</h4> <el-form ref="form" :model="form" label-width="80px"> <el-row> <el-col :span="8" :offset="2"> <el-form-item label="用户昵称" prop="nickName"> <el-input v-model="form.nickName" disabled /> </el-form-item> </el-col> <el-col :span="8" :offset="2"> <el-form-item label="登录账号" prop="phonenumber"> <el-input v-model="form.userName" disabled /> </el-form-item> </el-col> </el-row> </el-form> <h4 class="form-header h4">角色信息</h4> <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange" :data="roles.slice((pageNum-1)*pageSize,pageNum*pageSize)"> <el-table-column label="序号" type="index" align="center"> <template slot-scope="scope"> <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span> </template> </el-table-column> <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column> <el-table-column label="角色编号" align="center" prop="roleId" /> <el-table-column label="角色名称" align="center" prop="roleName" /> <el-table-column label="权限字符" align="center" prop="roleKey" /> <el-table-column label="创建时间" align="center" prop="createTime" width="180"> <template slot-scope="scope"> <span>{{ parseTime(scope.row.createTime) }}</span> </template> </el-table-column> </el-table> <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" /> <el-form label-width="100px"> <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;"> <el-button type="primary" @click="submitForm()">提交</el-button> <el-button @click="close()">返回</el-button> </el-form-item> </el-form> </div> </template> <script> import { getAuthRole, updateAuthRole } from "@/api/system/user"; export default { name: "AuthRole", data() { return { // 遮罩层 loading: true, // 分页信息 total: 0, pageNum: 1, pageSize: 10, // 选中角色编号 roleIds:[], // 角色信息 roles: [], // 用户信息 form: {} }; }, created() { const userId = this.$route.params && this.$route.params.userId; if (userId) { this.loading = true; getAuthRole(userId).then((response) => { this.form = response.user; this.roles = response.roles; this.total = this.roles.length; this.$nextTick(() => { this.roles.forEach((row) => { if (row.flag) { this.$refs.table.toggleRowSelection(row); } }); }); this.loading = false; }); } }, methods: { /** 单击选中行数据 */ clickRow(row) { this.$refs.table.toggleRowSelection(row); }, // 多选框选中数据 handleSelectionChange(selection) { this.roleIds = selection.map((item) => item.roleId); }, // 保存选中的数据编号 getRowKey(row) { return row.roleId; }, /** 提交按钮 */ submitForm() { const userId = this.form.userId; const roleIds = this.roleIds.join(","); updateAuthRole({ userId: userId, roleIds: roleIds }).then((response) => { this.msgSuccess("授权成功"); this.close(); }); }, /** 关闭按钮 */ close() { this.$store.dispatch("tagsView/delView", this.$route); this.$router.push({ path: "/system/user" }); }, }, }; </script> <template> <div class="app-container"> <h4 class="form-header h4">基本信息</h4> <el-form ref="form" :model="form" label-width="80px"> <el-row> <el-col :span="8" :offset="2"> <el-form-item label="用户昵称" prop="nickName"> <el-input v-model="form.nickName" disabled /> </el-form-item> </el-col> <el-col :span="8" :offset="2"> <el-form-item label="登录账号" prop="phonenumber"> <el-input v-model="form.userName" disabled /> </el-form-item> </el-col> </el-row> </el-form> <h4 class="form-header h4">角色信息</h4> <el-table v-loading="loading" :row-key="getRowKey" @row-click="clickRow" ref="table" @selection-change="handleSelectionChange" :data="roles.slice((pageNum-1)*pageSize,pageNum*pageSize)"> <el-table-column label="序号" type="index" align="center"> <template slot-scope="scope"> <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span> </template> </el-table-column> <el-table-column type="selection" :reserve-selection="true" width="55"></el-table-column> <el-table-column label="角色编号" align="center" prop="roleId" /> <el-table-column label="角色名称" align="center" prop="roleName" /> <el-table-column label="权限字符" align="center" prop="roleKey" /> <el-table-column label="创建时间" align="center" prop="createTime" width="180"> <template slot-scope="scope"> <span>{{ parseTime(scope.row.createTime) }}</span> </template> </el-table-column> </el-table> <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" /> <el-form label-width="100px"> <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;"> <el-button type="primary" @click="submitForm()">提交</el-button> <el-button @click="close()">返回</el-button> </el-form-item> </el-form> </div> </template> <script> import { getAuthRole, updateAuthRole } from "@/api/system/user"; export default { name: "AuthRole", data() { return { // 遮罩层 loading: true, // 分页信息 total: 0, pageNum: 1, pageSize: 10, // 选中角色编号 roleIds:[], // 角色信息 roles: [], // 用户信息 form: {} }; }, created() { const userId = this.$route.params && this.$route.params.userId; if (userId) { this.loading = true; getAuthRole(userId).then((response) => { this.form = response.data.user; this.roles = response.data.roles; this.total = this.roles.length; this.$nextTick(() => { this.roles.forEach((row) => { if (row.flag) { this.$refs.table.toggleRowSelection(row); } }); }); this.loading = false; }); } }, methods: { /** 单击选中行数据 */ clickRow(row) { this.$refs.table.toggleRowSelection(row); }, // 多选框选中数据 handleSelectionChange(selection) { this.roleIds = selection.map((item) => item.roleId); }, // 保存选中的数据编号 getRowKey(row) { return row.roleId; }, /** 提交按钮 */ submitForm() { const userId = this.form.userId; const roleIds = this.roleIds.join(","); updateAuthRole({ userId: userId, roleIds: roleIds }).then((response) => { this.msgSuccess("授权成功"); this.close(); }); }, /** 关闭按钮 */ close() { this.$store.dispatch("tagsView/delView", this.$route); this.$router.push({ path: "/system/user" }); }, }, }; </script>