diff --git a/ruoyi-ui/src/api/system/permission.js b/ruoyi-ui/src/api/system/permission.js
index ac3ed091a..b76405640 100644
--- a/ruoyi-ui/src/api/system/permission.js
+++ b/ruoyi-ui/src/api/system/permission.js
@@ -17,6 +17,23 @@ export function assignRoleMenu(data) {
})
}
+// 查询用户拥有的角色数组
+export function listUserRoles(userId) {
+ return request({
+ url: '/system/permission/list-user-roles?userId=' + userId,
+ method: 'get'
+ })
+}
+
+// 赋予用户角色
+export function assignUserRole(data) {
+ return request({
+ url: '/system/permission/assign-user-role',
+ method: 'post',
+ data: data
+ })
+}
+
// 赋予角色数据权限
export function assignRoleDataScope(data) {
return request({
diff --git a/ruoyi-ui/src/api/system/role.js b/ruoyi-ui/src/api/system/role.js
index a4c5d423f..8e026a142 100644
--- a/ruoyi-ui/src/api/system/role.js
+++ b/ruoyi-ui/src/api/system/role.js
@@ -9,6 +9,14 @@ export function listRole(query) {
})
}
+// 查询角色(精简)列表
+export function listSimpleRoles() {
+ return request({
+ url: '/system/role/list-all-simple',
+ method: 'get'
+ })
+}
+
// 查询角色详细
export function getRole(roleId) {
return request({
diff --git a/ruoyi-ui/src/views/system/user/index.vue b/ruoyi-ui/src/views/system/user/index.vue
index 749fd2dbd..1662c2ec5 100644
--- a/ruoyi-ui/src/views/system/user/index.vue
+++ b/ruoyi-ui/src/views/system/user/index.vue
@@ -163,6 +163,13 @@
@click="handleResetPwd(scope.row)"
v-hasPermi="['system:user:resetPwd']"
>重置
+ 分配角色
@@ -290,31 +297,30 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -330,6 +336,8 @@ import {listSimplePosts} from "@/api/system/post";
import {SysCommonStatusEnum} from "@/utils/constants";
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
+import {assignUserRole, listUserRoles} from "@/api/system/permission";
+import {listSimpleRoles} from "@/api/system/role";
export default {
name: "User",
@@ -420,6 +428,8 @@ export default {
}
]
},
+ // 是否显示弹出层(角色权限)
+ openRole: false,
// 枚举
SysCommonStatusEnum: SysCommonStatusEnum,
@@ -499,6 +509,11 @@ export default {
this.open = false;
this.reset();
},
+ // 取消按钮(角色权限)
+ cancelMenu() {
+ this.openRole = false;
+ this.reset();
+ },
// 表单重置
reset() {
this.form = {
@@ -561,6 +576,28 @@ export default {
});
}).catch(() => {});
},
+ /** 分配用户角色操作 */
+ handleRole(row) {
+ this.reset();
+ const id = row.id
+ // 处理了 form 的用户 username 和 nickname 的展示
+ this.form.id = id;
+ this.form.username = row.username;
+ this.form.nickname = row.nickname;
+ // 打开弹窗
+ this.openRole = true;
+ // 获得角色列表
+ listSimpleRoles().then(response => {
+ // 处理 roleOptions 参数
+ this.roleOptions = [];
+ this.roleOptions.push(...response.data);
+ });
+ // 获得角色拥有的菜单集合
+ listUserRoles(id).then(response => {
+ // 设置选中
+ this.form.roleIds = response.data;
+ })
+ },
/** 提交按钮 */
submitForm: function() {
this.$refs["form"].validate(valid => {
@@ -581,6 +618,19 @@ export default {
}
});
},
+ /** 提交按钮(角色权限) */
+ submitRole: function() {
+ if (this.form.id !== undefined) {
+ assignUserRole({
+ userId: this.form.id,
+ roleIds: this.form.roleIds,
+ }).then(response => {
+ this.msgSuccess("分配角色成功");
+ this.openRole = false;
+ this.getList();
+ });
+ }
+ },
/** 删除按钮操作 */
handleDelete(row) {
const ids = row.id || this.ids;
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysPermissionController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysPermissionController.java
index f10bdb76a..3e0f8ae3f 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysPermissionController.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysPermissionController.java
@@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.controller.permission;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.permission.SysPermissionAssignRoleDataScopeReqVO;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.permission.SysPermissionAssignRoleMenuReqVO;
+import cn.iocoder.dashboard.modules.system.controller.permission.vo.permission.SysPermissionAssignUserRoleReqVO;
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -53,22 +54,20 @@ public class SysPermissionController {
return success(true);
}
-// @GetMapping("/list-admin-roles")
-// @ApiOperation("获得管理员拥有的角色编号列表")
-//// @RequiresPermissions("system:permission:assign-admin-role")
-// @ApiImplicitParam(name = "adminId", value = "管理员编号", required = true)
-// public CommonResult> listAdminRoles(Integer adminId) {
-// return success(permissionManager.listAdminRoles(adminId));
-// }
+ @ApiOperation("获得管理员拥有的角色编号列表")
+ @ApiImplicitParam(name = "userId", value = "用户编号", required = true)
+ @GetMapping("/list-user-roles")
+// @RequiresPermissions("system:permission:assign-user-role")
+ public CommonResult> listAdminRoles(@RequestParam("userId") Long userId) {
+ return success(permissionService.listUserRoleIs(userId));
+ }
-// @PostMapping("/assign-admin-role")
-// @ApiOperation("赋予用户角色")
-//// @RequiresPermissions("system:permission:assign-admin-role")
-// public CommonResult assignAdminRole(PermissionAssignAdminRoleDTO assignAdminRoleDTO) {
-// permissionManager.assignAdminRole(assignAdminRoleDTO);
-// return success(true);
-// }
-
-// system:permission:assign-role-data-scope TODO
+ @ApiOperation("赋予用户角色")
+ @PostMapping("/assign-user-role")
+// @RequiresPermissions("system:permission:assign-user-role")
+ public CommonResult assignUserRole(@Validated @RequestBody SysPermissionAssignUserRoleReqVO reqVO) {
+ permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds());
+ return success(true);
+ }
}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java
index f95ac371b..75565c6d6 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java
@@ -1,5 +1,6 @@
package cn.iocoder.dashboard.modules.system.controller.permission;
+import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.*;
@@ -13,6 +14,9 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
@@ -31,6 +35,16 @@ public class SysRoleController {
return success(roleService.pageRole(reqVO));
}
+ @ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色,主要用于前端的下拉选项")
+ @GetMapping("/list-all-simple")
+ public CommonResult> listSimpleRoles() {
+ // 获得角色列表,只要开启状态的
+ List list = roleService.listRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
+ // 排序后,返回个诶前端
+ list.sort(Comparator.comparing(SysRoleDO::getSort));
+ return success(SysRoleConvert.INSTANCE.convertList02(list));
+ }
+
@ApiOperation("创建角色")
@PostMapping("/create")
// @PreAuthorize("@ss.hasPermi('system:role:add')")
@@ -85,29 +99,4 @@ public class SysRoleController {
return success(true);
}
-//
-// /**
-// * 修改保存数据权限
-// */
-// @PreAuthorize("@ss.hasPermi('system:role:edit')")
-// @Log(title = "角色管理", businessType = BusinessType.UPDATE)
-// @PutMapping("/dataScope")
-// public AjaxResult dataScope(@RequestBody SysRole role)
-// {
-// roleService.checkRoleAllowed(role);
-// return toAjax(roleService.authDataScope(role));
-// }
-
-
-//
-// /**
-// * 获取角色选择框列表
-// */
-// @PreAuthorize("@ss.hasPermi('system:role:query')")
-// @GetMapping("/optionselect")
-// public AjaxResult optionselect()
-// {
-// return AjaxResult.success(roleService.selectRoleAll());
-// }
-
}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/permission/SysPermissionAssignUserRoleReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/permission/SysPermissionAssignUserRoleReqVO.java
new file mode 100644
index 000000000..259336b90
--- /dev/null
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/permission/SysPermissionAssignUserRoleReqVO.java
@@ -0,0 +1,22 @@
+package cn.iocoder.dashboard.modules.system.controller.permission.vo.permission;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+import java.util.Collections;
+import java.util.Set;
+
+@ApiModel("赋予用户角色 Request VO")
+@Data
+public class SysPermissionAssignUserRoleReqVO {
+
+ @ApiModelProperty(value = "角色编号", required = true, example = "1")
+ @NotNull(message = "角色编号不能为空")
+ private Long userId;
+
+ @ApiModelProperty(value = "角色编号列表", example = "1,3,5")
+ private Set roleIds = Collections.emptySet(); // 兜底
+
+}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleSimpleRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleSimpleRespVO.java
new file mode 100644
index 000000000..b52a6c50c
--- /dev/null
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRoleSimpleRespVO.java
@@ -0,0 +1,21 @@
+package cn.iocoder.dashboard.modules.system.controller.permission.vo.role;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@ApiModel("角色精简信息 Response VO")
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class SysRoleSimpleRespVO {
+
+ @ApiModelProperty(value = "角色编号", required = true, example = "1024")
+ private Integer id;
+
+ @ApiModelProperty(value = "角色名称", required = true, example = "芋道")
+ private String name;
+
+}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java b/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java
index 7fc2287cb..ced4660e1 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java
@@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.convert.permission;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleRespVO;
+import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleSimpleRespVO;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -10,6 +11,8 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
+import java.util.List;
+
@Mapper
public interface SysRoleConvert {
@@ -24,4 +27,6 @@ public interface SysRoleConvert {
SysRoleDO convert(SysRoleCreateReqVO bean);
+ List convertList02(List list);
+
}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java
index 71ad24557..eb03f1e18 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java
@@ -8,6 +8,10 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
+import javax.annotation.Nullable;
+import java.util.Collection;
+import java.util.List;
+
@Mapper
public interface SysRoleMapper extends BaseMapper {
@@ -27,5 +31,7 @@ public interface SysRoleMapper extends BaseMapper {
return selectOne(new QueryWrapperX().eq("code", code));
}
-
+ default List selectListByStatus(@Nullable Collection statuses) {
+ return selectList(new QueryWrapperX().in("status", statuses));
+ }
}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysUserRoleMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysUserRoleMapper.java
index d6056eaac..f7cff3915 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysUserRoleMapper.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysUserRoleMapper.java
@@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
+import java.util.Collection;
import java.util.List;
+import java.util.stream.Collectors;
@Mapper
public interface SysUserRoleMapper extends BaseMapper {
@@ -14,4 +16,20 @@ public interface SysUserRoleMapper extends BaseMapper {
return selectList(new QueryWrapper().eq("user_id", userId));
}
+ default void insertList(Long userId, Collection roleIds) {
+ List list = roleIds.stream().map(roleId -> {
+ SysUserRoleDO entity = new SysUserRoleDO();
+ entity.setUserId(userId);
+ entity.setRoleId(roleId);
+ return entity;
+ }).collect(Collectors.toList());
+ // TODO 芋艿,mybatis plus 增加批量插入的功能
+ list.forEach(this::insert);
+ }
+
+ default void deleteListByUserIdAndRoleIdIds(Long userId, Collection roleIds) {
+ delete(new QueryWrapper().eq("user_id", userId)
+ .in("role_id", roleIds));
+ }
+
}
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java
index aef198f89..cab499201 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java
@@ -59,6 +59,22 @@ public interface SysPermissionService {
*/
void assignRoleMenu(Long roleId, Set menuIds);
+ /**
+ * 获得用户拥有的角色编号集合
+ *
+ * @param userId 用户编号
+ * @return 角色编号集合
+ */
+ Set listUserRoleIs(Long userId);
+
+ /**
+ * 设置用户角色
+ *
+ * @param userId 角色编号
+ * @param roleIds 角色编号集合
+ */
+ void assignUserRole(Long userId, Set roleIds);
+
/**
* 设置角色的数据权限
*
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java
index 62479de7d..ba9b98c17 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java
@@ -6,6 +6,7 @@ import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRole
import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
+import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@@ -30,6 +31,14 @@ public interface SysRoleService {
*/
SysRoleDO getRoleFromCache(Long id);
+ /**
+ * 获得角色列表
+ *
+ * @param statuses 筛选的状态。允许空,空时不筛选
+ * @return 角色列表
+ */
+ List listRoles(@Nullable Collection statuses);
+
/**
* 获得角色数组,从缓存中
*
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java
index 9a90a46fd..3cef2ac12 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java
@@ -142,6 +142,29 @@ public class SysPermissionServiceImpl implements SysPermissionService {
}
}
+ @Override
+ public Set listUserRoleIs(Long userId) {
+ return CollectionUtils.convertSet(userRoleMapper.selectListByUserId(userId),
+ SysUserRoleDO::getRoleId);
+ }
+
+ @Override
+ public void assignUserRole(Long userId, Set roleIds) {
+ // 获得角色拥有角色编号
+ Set dbRoleIds = CollectionUtils.convertSet(userRoleMapper.selectListByUserId(userId),
+ SysUserRoleDO::getRoleId);
+ // 计算新增和删除的角色编号
+ Collection createRoleIds = CollUtil.subtract(roleIds, dbRoleIds);
+ Collection deleteMenuIds = CollUtil.subtract(dbRoleIds, roleIds);
+ // 执行新增和删除。对于已经授权的角色,不用做任何处理
+ if (!CollectionUtil.isEmpty(createRoleIds)) {
+ userRoleMapper.insertList(userId, createRoleIds);
+ }
+ if (!CollectionUtil.isEmpty(deleteMenuIds)) {
+ userRoleMapper.deleteListByUserIdAndRoleIdIds(userId, deleteMenuIds);
+ }
+ }
+
@Override
public void assignRoleDataScope(Long roleId, Integer dataScope, Set dataScopeDeptIds) {
roleService.updateRoleDataScope(roleId, dataScope, dataScopeDeptIds);
diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java
index ae40775e2..e4795ffb0 100644
--- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java
+++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java
@@ -21,6 +21,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
+import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
@@ -71,6 +72,11 @@ public class SysRoleServiceImpl implements SysRoleService {
return roleCache.get(id);
}
+ @Override
+ public List listRoles(@Nullable Collection statuses) {
+ return roleMapper.selectListByStatus(statuses);
+ }
+
@Override
public List listRolesFromCache(Collection ids) {
if (CollectionUtil.isEmpty(ids)) {