mirror of
https://github.com/leion8310/SpringBoot-Shiro-Vue.git
synced 2026-10-18 03:53:38 +00:00
增加角色的管理功能
可以分配菜单权限
This commit is contained in:
@@ -3,6 +3,7 @@ package com.heeexy.example.controller;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.heeexy.example.service.UserService;
|
||||
import com.heeexy.example.util.CommonUtil;
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -46,9 +47,68 @@ public class UserController {
|
||||
return userService.updateUser(requestJson);
|
||||
}
|
||||
|
||||
@RequiresPermissions("user:update")
|
||||
@RequiresPermissions(value = {"user:add", "user:update"}, logical = Logical.OR)
|
||||
@GetMapping("/getAllRoles")
|
||||
public JSONObject getAllRoles() {
|
||||
return userService.getAllRoles();
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions("role:list")
|
||||
@GetMapping("/listRole")
|
||||
public JSONObject listRole() {
|
||||
return userService.listRole();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有权限, 给角色分配权限时调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions("role:list")
|
||||
@GetMapping("/listAllPermission")
|
||||
public JSONObject listAllPermission() {
|
||||
return userService.listAllPermission();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增角色
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions("role:add")
|
||||
@PostMapping("/addRole")
|
||||
public JSONObject addRole(@RequestBody JSONObject requestJson) {
|
||||
CommonUtil.hasAllRequired(requestJson, "roleName,permissions");
|
||||
return userService.addRole(requestJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改角色
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions("role:update")
|
||||
@PostMapping("/updateRole")
|
||||
public JSONObject updateRole(@RequestBody JSONObject requestJson) {
|
||||
CommonUtil.hasAllRequired(requestJson, "roleId,roleName,permissions");
|
||||
return userService.updateRole(requestJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param requestJson
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions("role:delete")
|
||||
@PostMapping("/deleteRole")
|
||||
public JSONObject deleteRole(@RequestBody JSONObject requestJson) {
|
||||
CommonUtil.hasAllRequired(requestJson, "roleId");
|
||||
return userService.deleteRole(requestJson);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.heeexy.example.dao;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -49,4 +50,76 @@ public interface UserDao {
|
||||
* @return
|
||||
*/
|
||||
int updateUser(JSONObject jsonObject);
|
||||
/**
|
||||
* 角色列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<JSONObject> listRole();
|
||||
|
||||
/**
|
||||
* 查询所有权限, 给角色分配权限时调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<JSONObject> listAllPermission();
|
||||
|
||||
/**
|
||||
* 新增角色
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
int insertRole(JSONObject jsonObject);
|
||||
|
||||
/**
|
||||
* 批量插入角色的权限
|
||||
*
|
||||
* @param roleId 角色ID
|
||||
* @param permissions 权限
|
||||
* @return
|
||||
*/
|
||||
int insertRolePermission(@Param("roleId") String roleId, @Param("permissions") List<Integer> permissions);
|
||||
|
||||
/**
|
||||
* 将角色曾经拥有而修改为不再拥有的权限 delete_status改为'2'
|
||||
*
|
||||
* @param roleId
|
||||
* @param permissions
|
||||
* @return
|
||||
*/
|
||||
int removeOldPermission(@Param("roleId") String roleId, @Param("permissions") List<Integer> permissions);
|
||||
|
||||
/**
|
||||
* 修改角色名称
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
int updateRoleName(JSONObject jsonObject);
|
||||
|
||||
/**
|
||||
* 查询某角色的全部数据
|
||||
* 在删除和修改角色时调用
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
JSONObject getRoleAllInfo(JSONObject jsonObject);
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
int removeRole(JSONObject jsonObject);
|
||||
|
||||
/**
|
||||
* 删除本角色全部权限
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
int removeRoleAllPermission(JSONObject jsonObject);
|
||||
}
|
||||
|
||||
@@ -43,10 +43,16 @@ public interface UserService {
|
||||
/**
|
||||
* 角色列表
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
JSONObject listRole(JSONObject jsonObject);
|
||||
JSONObject listRole();
|
||||
|
||||
/**
|
||||
* 查询所有权限, 给角色分配权限时调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
JSONObject listAllPermission();
|
||||
|
||||
/**
|
||||
* 添加角色
|
||||
@@ -63,4 +69,12 @@ public interface UserService {
|
||||
* @return
|
||||
*/
|
||||
JSONObject updateRole(JSONObject jsonObject);
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
JSONObject deleteRole(JSONObject jsonObject);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,15 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.heeexy.example.dao.UserDao;
|
||||
import com.heeexy.example.service.UserService;
|
||||
import com.heeexy.example.util.CommonUtil;
|
||||
import com.heeexy.example.util.constants.ErrorEnum;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author: hxy
|
||||
@@ -72,12 +77,23 @@ public class UserServiceImpl implements UserService {
|
||||
/**
|
||||
* 角色列表
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public JSONObject listRole(JSONObject jsonObject) {
|
||||
return null;
|
||||
public JSONObject listRole() {
|
||||
List<JSONObject> roles = userDao.listRole();
|
||||
return CommonUtil.successPage(roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有权限, 给角色分配权限时调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public JSONObject listAllPermission() {
|
||||
List<JSONObject> permissions = userDao.listAllPermission();
|
||||
return CommonUtil.successPage(permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,9 +102,12 @@ public class UserServiceImpl implements UserService {
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public JSONObject addRole(JSONObject jsonObject) {
|
||||
return null;
|
||||
userDao.insertRole(jsonObject);
|
||||
userDao.insertRolePermission(jsonObject.getString("roleId"), (List<Integer>) jsonObject.get("permissions"));
|
||||
return CommonUtil.successJson();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,8 +116,92 @@ public class UserServiceImpl implements UserService {
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public JSONObject updateRole(JSONObject jsonObject) {
|
||||
return null;
|
||||
String roleId = jsonObject.getString("roleId");
|
||||
System.out.println("jsonObject的值为:" + jsonObject);
|
||||
List<Integer> newPerms = (List<Integer>) jsonObject.get("permissions");
|
||||
System.out.println("ids的值为:" + newPerms);
|
||||
JSONObject roleInfo = userDao.getRoleAllInfo(jsonObject);
|
||||
Set<Integer> oldPerms = (Set<Integer>) roleInfo.get("permissionIds");
|
||||
//修改角色名称
|
||||
dealRoleName(jsonObject, roleInfo);
|
||||
//添加新权限
|
||||
saveNewPermission(roleId, newPerms, oldPerms);
|
||||
//移除旧的不再拥有的权限
|
||||
removeOldPermission(roleId, newPerms, oldPerms);
|
||||
return CommonUtil.successJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改角色名称
|
||||
*
|
||||
* @param paramJson
|
||||
* @param roleInfo
|
||||
*/
|
||||
private void dealRoleName(JSONObject paramJson, JSONObject roleInfo) {
|
||||
String roleName = paramJson.getString("roleName");
|
||||
if (!roleName.equals(roleInfo.getString("roleName"))) {
|
||||
userDao.updateRoleName(paramJson);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 为角色添加新权限
|
||||
*
|
||||
* @param newPerms
|
||||
* @param oldPerms
|
||||
*/
|
||||
private void saveNewPermission(String roleId, Collection<Integer> newPerms, Collection<Integer> oldPerms) {
|
||||
List<Integer> waitInsert = new ArrayList<>();
|
||||
for (Integer newPerm : newPerms) {
|
||||
if (!oldPerms.contains(newPerm)) {
|
||||
waitInsert.add(newPerm);
|
||||
}
|
||||
}
|
||||
System.out.println("waitInsert的值为:" + waitInsert);
|
||||
if (waitInsert.size() > 0) {
|
||||
userDao.insertRolePermission(roleId, waitInsert);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色 旧的 不再拥有的权限
|
||||
*
|
||||
* @param roleId
|
||||
* @param newPerms
|
||||
* @param oldPerms
|
||||
*/
|
||||
private void removeOldPermission(String roleId, Collection<Integer> newPerms, Collection<Integer> oldPerms) {
|
||||
List<Integer> waitRemove = new ArrayList<>();
|
||||
for (Integer oldPerm : oldPerms) {
|
||||
if (!newPerms.contains(oldPerm)) {
|
||||
waitRemove.add(oldPerm);
|
||||
}
|
||||
}
|
||||
System.out.println("waitRemove的值为:" + waitRemove);
|
||||
if (waitRemove.size() > 0) {
|
||||
userDao.removeOldPermission(roleId, waitRemove);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param jsonObject
|
||||
* @return
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public JSONObject deleteRole(JSONObject jsonObject) {
|
||||
JSONObject roleInfo = userDao.getRoleAllInfo(jsonObject);
|
||||
List<JSONObject> users = (List<JSONObject>) roleInfo.get("users");
|
||||
if (users != null && users.size() > 0) {
|
||||
return CommonUtil.errorJson(ErrorEnum.E_10008);
|
||||
}
|
||||
userDao.removeRole(jsonObject);
|
||||
userDao.removeRoleAllPermission(jsonObject);
|
||||
return CommonUtil.successJson();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ public enum ErrorEnum {
|
||||
E_500("500", "请求方式有误,请检查 GET/POST"),
|
||||
E_501("501", "请求路径不存在"),
|
||||
E_502("502", "权限不足"),
|
||||
E_10008("10008", "角色删除失败,尚有用户属于此角色"),
|
||||
|
||||
E_20011("20011", "登陆已过期,请重新登陆"),
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.heeexy.example.util.model;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,51 @@ public class One2Many extends JSONObject {
|
||||
private Set<String> roleList;
|
||||
private Set<String> menuList;
|
||||
private Set<String> permissionList;
|
||||
private Set<Integer> permissionIds;
|
||||
private List<JSONObject> picList;
|
||||
private List<JSONObject> menus;
|
||||
private List<JSONObject> users;
|
||||
private List<JSONObject> permissions;
|
||||
|
||||
public Set<Integer> getPermissionIds() {
|
||||
return permissionIds;
|
||||
}
|
||||
|
||||
public void setPermissionIds(Set<Integer> permissionIds) {
|
||||
this.permissionIds = permissionIds;
|
||||
}
|
||||
|
||||
public List<JSONObject> getPicList() {
|
||||
return picList;
|
||||
}
|
||||
|
||||
public void setPicList(List<JSONObject> picList) {
|
||||
this.picList = picList;
|
||||
}
|
||||
|
||||
public List<JSONObject> getMenus() {
|
||||
return menus;
|
||||
}
|
||||
|
||||
public void setMenus(List<JSONObject> menus) {
|
||||
this.menus = menus;
|
||||
}
|
||||
|
||||
public List<JSONObject> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(List<JSONObject> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
public List<JSONObject> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(List<JSONObject> permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
public Set<String> getRoleList() {
|
||||
return roleList;
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
<select id="listUser" resultMap="userMap">
|
||||
SELECT
|
||||
u.*,
|
||||
r.role_name roleName,
|
||||
CONCAT(p.menu_name, p.permission_name) permissionName
|
||||
r.role_name roleName,
|
||||
CONCAT(p.menu_name, p.permission_name) permissionName
|
||||
FROM (
|
||||
SELECT
|
||||
id userId,
|
||||
@@ -58,8 +58,8 @@
|
||||
|
||||
<insert id="addUser" useGeneratedKeys="true" keyProperty="userId">
|
||||
INSERT INTO sys_user
|
||||
(username, password, nickname, role_id) VALUES
|
||||
(#{username}, #{password}, #{nickname}, #{roleId})
|
||||
(username, password, nickname, role_id) VALUES
|
||||
(#{username}, #{password}, #{nickname}, #{roleId})
|
||||
</insert>
|
||||
|
||||
<update id="updateUser">
|
||||
@@ -74,4 +74,123 @@
|
||||
, delete_status = #{deleteStatus}
|
||||
WHERE id = #{userId}
|
||||
</update>
|
||||
<resultMap id="roleMap" type="com.heeexy.example.util.model.One2Many">
|
||||
<id column="roleId" property="roleId"/>
|
||||
<result column="roleName" property="roleName"/>
|
||||
<collection property="users" ofType="com.alibaba.fastjson.JSONObject">
|
||||
<id column="userId" property="userId"/>
|
||||
<result column="nickname" property="nickname"/>
|
||||
</collection>
|
||||
<collection property="menus" ofType="com.heeexy.example.util.model.One2Many">
|
||||
<id column="menuCode" property="menuCode"/>
|
||||
<result column="menuName" property="menuName"/>
|
||||
<collection property="permissions" ofType="com.alibaba.fastjson.JSONObject">
|
||||
<id column="permissionId" property="permissionId"/>
|
||||
<result column="permissionName" property="permissionName"/>
|
||||
</collection>
|
||||
</collection>
|
||||
</resultMap>
|
||||
<select id="listRole" resultMap="roleMap">
|
||||
SELECT
|
||||
r.id roleId,
|
||||
r.role_name roleName,
|
||||
u.id userId,
|
||||
u.nickname nickname,
|
||||
p.id permissionId,
|
||||
p.menu_code menuCode,
|
||||
p.menu_name menuName,
|
||||
p.permission_name permissionName
|
||||
FROM sys_role r
|
||||
LEFT JOIN sys_user u ON r.id = u.role_id AND u.delete_status = '1'
|
||||
LEFT JOIN sys_role_permission rp ON r.id = rp.role_id AND rp.delete_status = '1'
|
||||
LEFT JOIN sys_permission p ON rp.permission_id = p.id
|
||||
WHERE r.delete_status = '1'
|
||||
ORDER BY r.id, p.id
|
||||
</select>
|
||||
<resultMap id="permissionMap" type="com.heeexy.example.util.model.One2Many">
|
||||
<id column="menuName" property="menuName"/>
|
||||
<collection property="permissions" ofType="com.alibaba.fastjson.JSONObject">
|
||||
<id column="id" property="id"/>
|
||||
<result column="permissionName" property="permissionName"/>
|
||||
<result column="requiredPerm" property="requiredPerm" javaType="Integer"/>
|
||||
</collection>
|
||||
</resultMap>
|
||||
<select id="listAllPermission" resultMap="permissionMap">
|
||||
SELECT
|
||||
p.id id,
|
||||
p.menu_name menuName,
|
||||
p.permission_name permissionName,
|
||||
p.required_permission requiredPerm
|
||||
FROM sys_permission p;
|
||||
</select>
|
||||
|
||||
<insert id="insertRole" useGeneratedKeys="true" keyProperty="roleId">
|
||||
INSERT INTO sys_role
|
||||
(role_name)
|
||||
VALUES (#{roleName})
|
||||
</insert>
|
||||
|
||||
<insert id="insertRolePermission">
|
||||
insert into sys_role_permission (role_id, permission_id)
|
||||
values
|
||||
<foreach collection="permissions" item="item" index="index" separator=",">
|
||||
(#{roleId}, #{item})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
||||
<resultMap id="aRole" type="com.heeexy.example.util.model.One2Many">
|
||||
<id column="roleId" property="roleId"/>
|
||||
<result column="roleName" property="roleName"/>
|
||||
<collection property="users" ofType="com.alibaba.fastjson.JSONObject">
|
||||
<id column="userId" property="userId"/>
|
||||
</collection>
|
||||
<collection property="permissionIds" ofType="Integer">
|
||||
<id column="permissionId" property="permissionId"/>
|
||||
</collection>
|
||||
</resultMap>
|
||||
<select id="getRoleAllInfo" resultMap="aRole">
|
||||
SELECT
|
||||
r.id roleId,
|
||||
r.role_name roleName,
|
||||
u.id userId,
|
||||
p.id permissionId
|
||||
FROM sys_role r
|
||||
LEFT JOIN sys_user u ON r.id = u.role_id AND u.delete_status = '1'
|
||||
LEFT JOIN sys_role_permission rp ON r.id = rp.role_id AND rp.delete_status = '1'
|
||||
LEFT JOIN sys_permission p ON rp.permission_id = p.id
|
||||
WHERE r.id = #{roleId}
|
||||
</select>
|
||||
|
||||
|
||||
<update id="removeRole">
|
||||
UPDATE sys_role
|
||||
SET
|
||||
delete_status = '2'
|
||||
WHERE id = #{roleId}
|
||||
</update>
|
||||
<update id="removeRoleAllPermission">
|
||||
UPDATE sys_role_permission
|
||||
SET
|
||||
delete_status = '2'
|
||||
WHERE role_id = #{roleId}
|
||||
</update>
|
||||
|
||||
<update id="removeOldPermission">
|
||||
UPDATE sys_role_permission
|
||||
SET
|
||||
delete_status = '2'
|
||||
WHERE role_id = #{roleId}
|
||||
AND permission_id in (
|
||||
<foreach collection="permissions" item="item" index="index" separator=",">
|
||||
#{item}
|
||||
</foreach>
|
||||
)
|
||||
</update>
|
||||
<update id="updateRoleName">
|
||||
UPDATE sys_role
|
||||
SET
|
||||
role_name = #{roleName}
|
||||
WHERE id = #{roleId}
|
||||
</update>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user