1.新增角色菜单和按钮授权以及接口授权

2.修改资源详情接口
pull/3/head
rays 2021-01-09 19:18:57 +08:00
parent 2bc5e18fe8
commit 4345fb0e5b
28 changed files with 863 additions and 22 deletions

View File

@ -112,4 +112,9 @@ public class ResourceDefinition implements Serializable {
*/
private Date createTime;
/**
*
*/
private String createUser;
}

View File

@ -1,5 +1,8 @@
package cn.stylefeng.roses.kernel.system;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleMenuButtonResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleMenuResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResourceResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import java.util.List;
@ -62,6 +65,16 @@ public interface RoleServiceApi {
*/
List<String> getRoleResourceCodeList(List<Long> roleIdList);
/**
* code
*
* @param roleIdList id
* @return code
* @author majianguo
* @date 2020/11/5 11:17
*/
List<SysRoleResourceResponse> getRoleResourceList(List<Long> roleIdList);
/**
*
*
@ -72,4 +85,22 @@ public interface RoleServiceApi {
*/
Set<String> getRoleButtonCodes(List<Long> roleIdList);
/**
*
*
* @param roleIdList
* @author majianguo
* @date 2021/1/9 17:33
*/
List<SysRoleMenuResponse> getRoleMenuList(List<Long> roleIdList);
/**
*
*
* @param roleIdList
* @author majianguo
* @date 2021/1/9 17:33
*/
List<SysRoleMenuButtonResponse> getRoleMenuButtonList(List<Long> roleIdList);
}

View File

@ -1,6 +1,7 @@
package cn.stylefeng.roses.kernel.system;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.system.pojo.user.SysUserResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.UserLoginInfoDTO;
import java.util.Date;
@ -76,8 +77,18 @@ public interface UserServiceApi {
/**
* 线
*
* @return
*/
List<LoginUser> onlineUserList();
/**
* ID
*
* @param userId ID
* @author majianguo
* @date 2021/1/9 19:00
*/
SysUserResponse getUserInfoByUserId(Long userId);
}

View File

@ -0,0 +1,44 @@
package cn.stylefeng.roses.kernel.system.pojo.menu.layui;
import lombok.Data;
/**
*
*
* @author majianguo
* @date 2021/1/9 16:59
*/
@Data
public class LayuiMenuAndButtonTreeResponse {
/**
* ID
*/
private Long id;
/**
* ID
*/
private Long pid;
/**
*
*/
private String name;
/**
* (false,pidid)
*/
private Boolean menuFlag;
/**
* (true)
*/
private Boolean checked;
/**
* code
*/
private String buttonCode;
}

View File

@ -0,0 +1,24 @@
package cn.stylefeng.roses.kernel.system.pojo.role.request;
import lombok.Data;
/**
*
*
* @author majianguo
* @date 2021/1/9 17:33
*/
@Data
public class SysRoleMenuButtonRequest {
/**
* id
*/
private Long buttonId;
/**
*
*/
private String buttonCode;
}

View File

@ -48,7 +48,7 @@ public class SysRoleRequest extends BaseRequest {
/**
*
*/
@NotNull(message = "roleId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class, grantResource.class, grantData.class})
@NotNull(message = "roleId不能为空", groups = {edit.class, delete.class, detail.class, updateStatus.class, grantResource.class, grantData.class, grantMenuButton.class})
private Long roleId;
/**
@ -61,13 +61,7 @@ public class SysRoleRequest extends BaseRequest {
*
*/
@NotBlank(message = "角色编码不能为空", groups = {add.class, edit.class})
@TableUniqueValue(
message = "角色编码存在重复",
groups = {add.class, edit.class},
tableName = "sys_role",
columnName = "role_code",
idFieldName = "role_id",
excludeLogicDeleteItems = true)
@TableUniqueValue(message = "角色编码存在重复", groups = {add.class, edit.class}, tableName = "sys_role", columnName = "role_code", idFieldName = "role_id", excludeLogicDeleteItems = true)
private String roleCode;
/**
@ -105,16 +99,37 @@ public class SysRoleRequest extends BaseRequest {
@NotNull(message = "授权数据不能为空请检查grantOrgIdList参数", groups = {grantData.class})
private List<Long> grantOrgIdList;
/**
*
*/
@NotNull(message = "授权菜单Id不能为空请检查grantMenuIdList参数", groups = {grantMenuButton.class})
private List<Long> grantMenuIdList;
/**
*
*/
@NotNull(message = "授权菜单按钮Id不能为空请检查grantMenuButtonIdList参数", groups = {grantMenuButton.class})
private List<SysRoleMenuButtonRequest> grantMenuButtonIdList;
/**
*
*/
public @interface grantMenuButton {
}
/**
*
*/
public @interface grantResource {
}
/**
*
*/
public @interface grantData {
}
}

View File

@ -0,0 +1,34 @@
package cn.stylefeng.roses.kernel.system.pojo.role.response;
import lombok.Data;
/**
*
*
* @author majianguo
* @date 2021/1/9 17:33
*/
@Data
public class SysRoleMenuButtonResponse {
/**
*
*/
private Long roleButtonId;
/**
* id
*/
private Long roleId;
/**
* id
*/
private Long buttonId;
/**
*
*/
private String buttonCode;
}

View File

@ -0,0 +1,29 @@
package cn.stylefeng.roses.kernel.system.pojo.role.response;
import lombok.Data;
/**
*
*
* @author majianguo
* @date 2021/1/9 18:07
*/
@Data
public class SysRoleMenuResponse {
/**
*
*/
private Long roleMenuId;
/**
* id
*/
private Long roleId;
/**
* id
*/
private Long menuId;
}

View File

@ -0,0 +1,58 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
GunsAPACHE LICENSE 2.0使
1.LICENSE
2.Guns
3.
4. https://gitee.com/stylefeng/guns-separation
5. https://gitee.com/stylefeng/guns-separation
6.Guns https://www.stylefeng.cn
*/
package cn.stylefeng.roses.kernel.system.pojo.role.response;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
*
*
* @author fengshuonan
* @date 2020/11/5 4:30
*/
@Data
public class SysRoleResourceResponse extends BaseEntity {
/**
*
*/
private Long roleResourceId;
/**
* id
*/
private Long roleId;
/**
*
*/
private String resourceCode;
}

View File

@ -0,0 +1,81 @@
package cn.stylefeng.roses.kernel.system.pojo.user;
import lombok.Data;
import java.util.Date;
/**
*
*
* @author fengshuonan
* @date 2020/4/2 9:19
*/
@Data
public class SysUserResponse {
/**
*
*/
private Long userId;
/**
*
*/
private String account;
/**
*
*/
private String nickName;
/**
*
*/
private String realName;
/**
*
*/
private Long avatar;
/**
*
*/
private Date birthday;
/**
* M-F-
*/
private String sex;
/**
*
*/
private String email;
/**
*
*/
private String phone;
/**
*
*/
private String tel;
/**
*
*/
private Long orgId;
/**
*
*/
private Long positionId;
/**
*
*/
private Integer statusFlag;
}

View File

@ -10,6 +10,8 @@ import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuRequest;
import cn.stylefeng.roses.kernel.system.pojo.menu.antd.AntdSysMenuResponse;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiMenuAndButtonTreeResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
@ -133,6 +135,17 @@ public class SysMenuController {
return new SuccessResponseData(sysMenuService.treeForGrant(sysMenuRequest));
}
/**
* ()
*
* @author majianguo
* @date 2021/1/9 17:10
*/
@GetResource(name = "获取系统菜单树(包含按钮),用于给角色授权时选择", path = "/sysMenu/menuAndButtonTree")
public List<LayuiMenuAndButtonTreeResponse> menuAndButtonTree(SysRoleRequest sysRoleRequest) {
return sysMenuService.getMenuAndButtonTree(sysRoleRequest);
}
/**
* Antd Vue
*

View File

@ -0,0 +1,54 @@
package cn.stylefeng.roses.kernel.menu.modular.entity;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
import java.util.Date;
/**
*
*
* @author majianguo
* @date 2021/01/09 14:44
*/
@TableName("sys_menu_button")
@Data
@EqualsAndHashCode(callSuper = true)
public class SysMenuButton extends BaseEntity implements Serializable {
/**
*
*/
@TableId("button_id")
private Long buttonId;
/**
* id
*/
@TableField("menu_id")
private Long menuId;
/**
*
*/
@TableField("button_name")
private String buttonName;
/**
*
*/
@TableField("button_code")
private String buttonCode;
/**
* Y-N-
*/
@TableField("del_flag")
private String delFlag;
}

View File

@ -0,0 +1,13 @@
package cn.stylefeng.roses.kernel.menu.modular.mapper;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
*
*
* @author majianguo
* @date 2021/01/09 14:44
*/
public interface SysMenuButtonMapper extends BaseMapper<SysMenuButton> {
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.stylefeng.roses.kernel.menu.modular.mapper.SysMenuButtonMapper">
</mapper>

View File

@ -0,0 +1,14 @@
package cn.stylefeng.roses.kernel.menu.modular.service;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import com.baomidou.mybatisplus.extension.service.IService;
/**
*
*
* @author majianguo
* @date 2021/01/09 14:44
*/
public interface SysMenuButtonService extends IService<SysMenuButton> {
}

View File

@ -29,7 +29,9 @@ import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuRequest;
import cn.stylefeng.roses.kernel.system.pojo.menu.antd.AntdIndexMenuTreeNode;
import cn.stylefeng.roses.kernel.system.pojo.menu.antd.AntdSysMenuResponse;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiAppIndexMenus;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiMenuAndButtonTreeResponse;
import cn.stylefeng.roses.kernel.system.pojo.menu.other.MenuSelectTreeNode;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
import com.baomidou.mybatisplus.extension.service.IService;
@ -171,4 +173,11 @@ public interface SysMenuService extends IService<SysMenu> {
*/
List<AntdSysMenuResponse> getSystemAllMenusAntdv();
/**
*
*
* @author majianguo
* @date 2021/1/9 17:11
*/
List<LayuiMenuAndButtonTreeResponse> getMenuAndButtonTree(SysRoleRequest sysRoleRequest);
}

View File

@ -0,0 +1,18 @@
package cn.stylefeng.roses.kernel.menu.modular.service.impl;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import cn.stylefeng.roses.kernel.menu.modular.mapper.SysMenuButtonMapper;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuButtonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
*
*
* @author majianguo
* @date 2021/01/09 14:44
*/
@Service
public class SysMenuButtonServiceImpl extends ServiceImpl<SysMenuButtonMapper, SysMenuButton> implements SysMenuButtonService {
}

View File

@ -33,10 +33,12 @@ import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenu;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenuButton;
import cn.stylefeng.roses.kernel.menu.modular.factory.AntdMenusFactory;
import cn.stylefeng.roses.kernel.menu.modular.factory.LayuiMenusFactory;
import cn.stylefeng.roses.kernel.menu.modular.factory.common.CommonMenusFactory;
import cn.stylefeng.roses.kernel.menu.modular.mapper.SysMenuMapper;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuButtonService;
import cn.stylefeng.roses.kernel.menu.modular.service.SysMenuService;
import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
@ -54,7 +56,11 @@ import cn.stylefeng.roses.kernel.system.pojo.menu.SysMenuRequest;
import cn.stylefeng.roses.kernel.system.pojo.menu.antd.AntdIndexMenuTreeNode;
import cn.stylefeng.roses.kernel.system.pojo.menu.antd.AntdSysMenuResponse;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiAppIndexMenus;
import cn.stylefeng.roses.kernel.system.pojo.menu.layui.LayuiMenuAndButtonTreeResponse;
import cn.stylefeng.roses.kernel.system.pojo.menu.other.MenuSelectTreeNode;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleMenuButtonResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleMenuResponse;
import cn.stylefeng.roses.kernel.system.pojo.ztree.ZTreeNode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -63,10 +69,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -87,6 +90,9 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
@Resource
private AppServiceApi appServiceApi;
@Resource
private SysMenuButtonService sysMenuButtonService;
@Override
public void add(SysMenuRequest sysMenuRequest) {
@ -258,8 +264,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
// 给应用排序,激活的应用放在前边
String activeAppCode = appServiceApi.getActiveAppCode();
if (activeAppCode != null) {
List<LayuiAppIndexMenus> layuiAppIndexMenusArrayList =
layuiAppIndexMenus.stream().filter(i -> activeAppCode.equals(i.getAppCode())).collect(Collectors.toList());
List<LayuiAppIndexMenus> layuiAppIndexMenusArrayList = layuiAppIndexMenus.stream().filter(i -> activeAppCode.equals(i.getAppCode())).collect(Collectors.toList());
layuiAppIndexMenusArrayList.addAll(layuiAppIndexMenus.stream().filter(i -> !activeAppCode.equals(i.getAppCode())).collect(Collectors.toList()));
return layuiAppIndexMenusArrayList;
}
@ -321,6 +326,106 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
return this.baseMapper.getSystemAllMenus();
}
@Override
public List<LayuiMenuAndButtonTreeResponse> getMenuAndButtonTree(SysRoleRequest sysRoleRequest) {
List<LayuiMenuAndButtonTreeResponse> menuTreeNodeList = CollectionUtil.newArrayList();
LambdaQueryWrapper<SysMenu> munuWrapper = new LambdaQueryWrapper<>();
munuWrapper.eq(SysMenu::getStatusFlag, StatusEnum.ENABLE.getCode());
munuWrapper.eq(SysMenu::getDelFlag, YesOrNotEnum.N.getCode());
// 非超级管理员则获取自己拥有的菜单,分配给人员,防止越级授权
if (!LoginContext.me().getSuperAdminFlag()) {
List<Long> menuIdList = getCurrentUserMenuIds();
if (!menuIdList.isEmpty()) {
munuWrapper.in(SysMenu::getMenuId, menuIdList);
}
}
List<SysMenu> sysMenuList = this.list(munuWrapper);
List<Long> menuList = sysMenuList.parallelStream().map(SysMenu::getMenuId).collect(Collectors.toList());
// 查询所有的按钮
LambdaQueryWrapper<SysMenuButton> buttonWrapper = new LambdaQueryWrapper<>();
buttonWrapper.eq(SysMenuButton::getDelFlag, YesOrNotEnum.N.getCode());
buttonWrapper.in(SysMenuButton::getMenuId, menuList);
List<SysMenuButton> sysMenuButtons = sysMenuButtonService.list(buttonWrapper);
// 把按钮按照菜单id存起来方便后续操作
Map<Long, List<SysMenuButton>> buttons = new HashMap<>(menuList.size());
for (SysMenuButton menuButton : sysMenuButtons) {
List<SysMenuButton> buttonList = buttons.get(menuButton.getMenuId());
if (ObjectUtil.isEmpty(buttonList)) {
buttonList = new ArrayList<>();
buttons.put(menuButton.getMenuId(), buttonList);
}
buttonList.add(menuButton);
}
// 查询所有已有的权限
// 所有已有的菜单权限
List<SysRoleMenuResponse> roleMenuList = roleServiceApi.getRoleMenuList(Collections.singletonList(sysRoleRequest.getRoleId()));
// 转换成map方便后续处理
Map<Long, SysRoleMenuResponse> roleMenuMap = new HashMap<>();
for (SysRoleMenuResponse sysRoleMenuResponse : roleMenuList) {
roleMenuMap.put(sysRoleMenuResponse.getMenuId(), sysRoleMenuResponse);
}
// 所有的按钮权限
List<SysRoleMenuButtonResponse> roleMenuButtonList = roleServiceApi.getRoleMenuButtonList(Collections.singletonList(sysRoleRequest.getRoleId()));
// 转换成map方便后续处理
Map<Long, SysRoleMenuButtonResponse> roleMenuButtonMap = new HashMap<>();
for (SysRoleMenuButtonResponse buttonResponse : roleMenuButtonList) {
roleMenuButtonMap.put(buttonResponse.getButtonId(), buttonResponse);
}
// 组装树结果
for (SysMenu sysMenu : sysMenuList) {
LayuiMenuAndButtonTreeResponse menuTree = new LayuiMenuAndButtonTreeResponse();
menuTree.setId(sysMenu.getMenuId());
menuTree.setMenuFlag(true);
menuTree.setName(sysMenu.getMenuName());
menuTree.setPid(sysMenu.getMenuParentId());
// 判断是否已经有了
SysRoleMenuResponse roleMenuResponse = roleMenuMap.get(sysMenu.getMenuId());
if (ObjectUtil.isEmpty(roleMenuResponse)) {
menuTree.setChecked(false);
} else {
menuTree.setChecked(true);
}
// 处理该菜单的按钮
List<SysMenuButton> menuButtons = buttons.get(sysMenu.getMenuId());
// 不为空就去处理
if (ObjectUtil.isNotEmpty(menuButtons)) {
for (SysMenuButton menuButton : menuButtons) {
LayuiMenuAndButtonTreeResponse buttonTree = new LayuiMenuAndButtonTreeResponse();
buttonTree.setName(menuButton.getButtonName());
buttonTree.setId(menuButton.getButtonId());
buttonTree.setPid(menuButton.getMenuId());
buttonTree.setButtonCode(menuButton.getButtonCode());
buttonTree.setMenuFlag(false);
// 判断是否已经拥有
SysRoleMenuButtonResponse buttonResponse = roleMenuButtonMap.get(menuButton.getButtonId());
if (ObjectUtil.isNotEmpty(buttonResponse)) {
buttonTree.setChecked(true);
menuTree.setChecked(true);
} else {
buttonTree.setChecked(false);
}
// 记录按钮节点
menuTreeNodeList.add(buttonTree);
}
}
// 记录菜单节点
menuTreeNodeList.add(menuTree);
}
return menuTreeNodeList;
}
@Override
public boolean hasMenu(String appCode) {
SysMenuRequest sysMenuRequest = new SysMenuRequest();

View File

@ -11,6 +11,7 @@ import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.system.pojo.resource.request.ResourceRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
@ -66,6 +67,17 @@ public class ResourceController {
return new SuccessResponseData(resourceTree);
}
/**
*
*
* @author majianguo
* @date 2021/1/9 15:07
*/
@GetResource(name = "获取资源平级树列表,用于分配接口权限", path = "/resource/getLateralTree")
public List<ResourceTreeNode> getLateralTree(SysRoleRequest sysRoleRequest) {
return sysResourceService.getResourceLateralTree(sysRoleRequest.getRoleId());
}
/**
*
*

View File

@ -36,6 +36,11 @@ public class ResourceTreeNode {
*/
private Boolean resourceFlag;
/**
*
*/
private Boolean checked;
/**
*
*/

View File

@ -67,4 +67,11 @@ public interface SysResourceService extends IService<SysResource> {
*/
ResourceDefinition getResourceDetail(ResourceRequest resourceRequest);
/**
*
*
* @author majianguo
* @date 2021/1/9 15:08
*/
List<ResourceTreeNode> getResourceLateralTree(Long roleId);
}

View File

@ -1,6 +1,9 @@
package cn.stylefeng.roses.kernel.resource.modular.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
@ -14,12 +17,19 @@ import cn.stylefeng.roses.kernel.resource.modular.factory.ResourceFactory;
import cn.stylefeng.roses.kernel.resource.modular.mapper.SysResourceMapper;
import cn.stylefeng.roses.kernel.resource.modular.pojo.ResourceTreeNode;
import cn.stylefeng.roses.kernel.resource.modular.service.SysResourceService;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.system.ResourceServiceApi;
import cn.stylefeng.roses.kernel.system.RoleServiceApi;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
import cn.stylefeng.roses.kernel.system.constants.SystemConstants;
import cn.stylefeng.roses.kernel.system.pojo.resource.request.ResourceRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResourceResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.SysUserResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
@ -46,6 +56,12 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
@Resource
private ResourceCache resourceCache;
@Resource
private RoleServiceApi roleServiceApi;
@Resource
private UserServiceApi userServiceApi;
@Override
public PageResult<SysResource> getResourceList(ResourceRequest resourceRequest) {
LambdaQueryWrapper<SysResource> wrapper = createWrapper(resourceRequest);
@ -103,12 +119,104 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
sysResourceLambdaQueryWrapper.eq(SysResource::getResourceCode, resourceRequest.getResourceCode());
SysResource sysResource = this.getOne(sysResourceLambdaQueryWrapper);
if (sysResource != null) {
return ResourceFactory.createResourceDefinition(sysResource);
ResourceDefinition definition = ResourceFactory.createResourceDefinition(sysResource);
// 翻译创建人
if (sysResource.getCreateUser().equals(RuleConstants.TREE_ROOT_ID)) {
definition.setCreateUser("超级管理员");
} else {
SysUserResponse userInfo = userServiceApi.getUserInfoByUserId(sysResource.getCreateUser());
if (ObjectUtil.isNotEmpty(userInfo)) {
definition.setCreateUser(userInfo.getRealName());
}
}
return definition;
} else {
return null;
}
}
@Override
public List<ResourceTreeNode> getResourceLateralTree(Long roleId) {
// 结果
List<ResourceTreeNode> res = new ArrayList<>();
// 获取所有的资源
LambdaQueryWrapper<SysResource> sysResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysResourceLambdaQueryWrapper.select(SysResource::getAppCode, SysResource::getModularCode, SysResource::getModularName, SysResource::getResourceCode, SysResource::getUrl, SysResource::getResourceName);
// 只查询需要授权的接口
sysResourceLambdaQueryWrapper.eq(SysResource::getRequiredPermissionFlag, YesOrNotEnum.Y.getCode());
LoginUserApi loginUserApi = LoginContext.me();
if (!loginUserApi.getSuperAdminFlag()) {
// 获取权限列表
List<Long> roleIds = loginUserApi.getLoginUser().getSimpleRoleInfoList().parallelStream().map(SimpleRoleInfo::getRoleId).collect(Collectors.toList());
List<String> resourceCodeList = roleServiceApi.getRoleResourceCodeList(roleIds);
if (!resourceCodeList.isEmpty()) {
sysResourceLambdaQueryWrapper.in(SysResource::getResourceCode, resourceCodeList);
}
}
List<SysResource> allResource = this.list(sysResourceLambdaQueryWrapper);
// 查询当前角色已有的接口
List<SysRoleResourceResponse> resourceList = roleServiceApi.getRoleResourceList(Collections.singletonList(roleId));
// 该角色已拥有权限
Map<String, SysRoleResourceResponse> alreadyHave = new HashMap<>(resourceList.size());
for (SysRoleResourceResponse sysRoleResponse : resourceList) {
alreadyHave.put(sysRoleResponse.getResourceCode(), sysRoleResponse);
}
// 根据模块名称把资源分类
Map<String, List<SysResource>> modularMap = new HashMap<>();
for (SysResource sysResource : allResource) {
List<SysResource> sysResources = modularMap.get(sysResource.getModularName());
// 没有就新建一个
if (ObjectUtil.isEmpty(sysResources)) {
sysResources = new ArrayList<>();
modularMap.put(sysResource.getModularName(), sysResources);
}
// 把自己加入进去
sysResources.add(sysResource);
}
// 创建一级节点
for (Map.Entry<String, List<SysResource>> entry : modularMap.entrySet()) {
ResourceTreeNode item = new ResourceTreeNode();
item.setResourceFlag(false);
String id = IdWorker.get32UUID();
item.setCode(id);
item.setParentCode(RuleConstants.TREE_ROOT_ID.toString());
item.setNodeName(entry.getKey());
item.setChecked(false);
//创建二级节点
for (SysResource resource : entry.getValue()) {
ResourceTreeNode subItem = new ResourceTreeNode();
// 判断是否已经拥有
SysRoleResourceResponse resourceResponse = alreadyHave.get(resource.getResourceCode());
if (ObjectUtil.isEmpty(resourceResponse)) {
subItem.setChecked(false);
} else {
// 让父类也选择
item.setChecked(true);
subItem.setChecked(true);
}
subItem.setResourceFlag(true);
subItem.setNodeName(resource.getResourceName());
subItem.setCode(resource.getResourceCode());
subItem.setParentCode(id);
res.add(subItem);
}
res.add(item);
}
// 根据map组装资源树
return res;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void reportResources(@RequestBody ReportResourceParam reportResourceReq) {
@ -315,4 +423,49 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
return finalTree;
}
/**
*
*
* @author majianguo
* @date 2021/1/9 15:10
*/
private List<ResourceTreeNode> createResourceLateralTree(Map<String, Map<String, List<ResourceTreeNode>>> appModularResources, Map<String, String> modularCodeName) {
List<ResourceTreeNode> finalTree = new ArrayList<>();
// 按应用遍历应用模块资源集合
for (String appName : appModularResources.keySet()) {
// 创建当前应用节点
ResourceTreeNode appNode = new ResourceTreeNode();
appNode.setCode(appName);
appNode.setNodeName(appName);
appNode.setResourceFlag(false);
appNode.setParentCode(SystemConstants.DEFAULT_PARENT_ID.toString());
// 遍历当前应用下的模块资源
Map<String, List<ResourceTreeNode>> modularResources = appModularResources.get(appName);
// 创建模块节点
ArrayList<ResourceTreeNode> modularNodes = new ArrayList<>();
for (String modularCode : modularResources.keySet()) {
ResourceTreeNode modularNode = new ResourceTreeNode();
modularNode.setCode(modularCode);
modularNode.setNodeName(modularCodeName.get(modularCode));
modularNode.setResourceFlag(false);
modularNode.setParentCode(appName);
modularNode.setChildren(modularResources.get(modularCode));
modularNodes.add(modularNode);
}
// 当前应用下添加模块的资源
appNode.setChildren(modularNodes);
// 添加到最终结果
finalTree.add(appNode);
}
return finalTree;
}
}

View File

@ -83,6 +83,18 @@ public class SysRoleController {
return new SuccessResponseData();
}
/**
*
*
* @author majianguo
* @date 2021/1/9 18:04
*/
@PostResource(name = "授权资源", path = "/sysRole/grantMenuAndButton")
public ResponseData grantMenuAndButton(@RequestBody @Validated(SysRoleRequest.grantMenuButton.class) SysRoleRequest sysRoleRequest) {
sysRoleService.grantMenuAndButton(sysRoleRequest);
return new SuccessResponseData();
}
/**
*
*

View File

@ -25,8 +25,8 @@ Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意
package cn.stylefeng.roses.kernel.role.modular.service;
import cn.stylefeng.roses.kernel.role.modular.entity.SysRoleResource;
import com.baomidou.mybatisplus.extension.service.IService;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;

View File

@ -136,4 +136,11 @@ public interface SysRoleService extends IService<SysRole> {
*/
String getNameByRoleId(Long roleId);
/**
*
*
* @author majianguo
* @date 2021/1/9 18:13
*/
void grantMenuAndButton(SysRoleRequest sysRoleMenuButtonRequest);
}

View File

@ -24,6 +24,7 @@ Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意
*/
package cn.stylefeng.roses.kernel.role.modular.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.role.modular.entity.SysRoleResource;
import cn.stylefeng.roses.kernel.role.modular.mapper.SysRoleResourceMapper;
import cn.stylefeng.roses.kernel.role.modular.service.SysRoleResourceService;
@ -45,6 +46,8 @@ import java.util.List;
@Service
public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMapper, SysRoleResource> implements SysRoleResourceService {
@Override
@Transactional(rollbackFor = Exception.class)
public void grantResource(SysRoleRequest sysRoleRequest) {
@ -85,4 +88,5 @@ public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMappe
queryWrapper.eq(SysRoleResource::getRoleId, roleId);
this.remove(queryWrapper);
}
}

View File

@ -47,7 +47,11 @@ import cn.stylefeng.roses.kernel.system.UserServiceApi;
import cn.stylefeng.roses.kernel.system.constants.SymbolConstant;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.exception.enums.SysRoleExceptionEnum;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleMenuButtonRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleMenuButtonResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleMenuResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResourceResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import cn.stylefeng.roses.kernel.system.util.DataScopeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -329,9 +333,18 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Override
public List<String> getRoleResourceCodeList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(SysRoleResource::getResourceCode);
queryWrapper.in(SysRoleResource::getRoleId, roleIdList);
List<SysRoleResource> sysRoleResources = sysRoleResourceService.list(queryWrapper);
return sysRoleResources.stream().map(SysRoleResource::getResourceCode).collect(Collectors.toList());
return sysRoleResources.parallelStream().map(SysRoleResource::getResourceCode).collect(Collectors.toList());
}
@Override
public List<SysRoleResourceResponse> getRoleResourceList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleResource::getRoleId, roleIdList);
List<SysRoleResource> sysRoleResources = sysRoleResourceService.list(queryWrapper);
return sysRoleResources.parallelStream().map(item -> BeanUtil.copyProperties(item, SysRoleResourceResponse.class)).collect(Collectors.toList());
}
@Override
@ -343,6 +356,66 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
return list.stream().map(SysRoleMenuButton::getButtonCode).collect(Collectors.toSet());
}
@Override
public List<SysRoleMenuResponse> getRoleMenuList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysRoleMenuLambdaQueryWrapper.in(SysRoleMenu::getRoleId, roleIdList);
List<SysRoleMenu> roleMenus = roleMenuService.list(sysRoleMenuLambdaQueryWrapper);
List<SysRoleMenuResponse> sysRoleMenuResponses = roleMenus.parallelStream().map(item -> BeanUtil.copyProperties(item, SysRoleMenuResponse.class)).collect(Collectors.toList());
return sysRoleMenuResponses;
}
@Override
public List<SysRoleMenuButtonResponse> getRoleMenuButtonList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleMenuButton> sysRoleMenuButtonLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysRoleMenuButtonLambdaQueryWrapper.in(SysRoleMenuButton::getRoleId, roleIdList);
List<SysRoleMenuButton> sysRoleMenuButtons = sysRoleMenuButtonService.list(sysRoleMenuButtonLambdaQueryWrapper);
List<SysRoleMenuButtonResponse> sysRoleMenuButtonResponses = sysRoleMenuButtons.parallelStream().map(item -> BeanUtil.copyProperties(item, SysRoleMenuButtonResponse.class)).collect(Collectors.toList());
return sysRoleMenuButtonResponses;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void grantMenuAndButton(SysRoleRequest sysRoleMenuButtonRequest) {
// 清除该角色之前的菜单
LambdaQueryWrapper<SysRoleMenu> sysRoleMenuLqw = new LambdaQueryWrapper<>();
sysRoleMenuLqw.eq(SysRoleMenu::getRoleId, sysRoleMenuButtonRequest.getRoleId());
roleMenuService.remove(sysRoleMenuLqw);
// 清除该角色之前的按钮授权
LambdaQueryWrapper<SysRoleMenuButton> menuButtonLqw = new LambdaQueryWrapper<>();
menuButtonLqw.eq(SysRoleMenuButton::getRoleId, sysRoleMenuButtonRequest.getRoleId());
sysRoleMenuButtonService.remove(menuButtonLqw);
// 新增菜单
List<Long> menuIdList = sysRoleMenuButtonRequest.getGrantMenuIdList();
if (ObjectUtil.isNotEmpty(menuIdList)) {
List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
for (Long menuId : menuIdList) {
SysRoleMenu item = new SysRoleMenu();
item.setRoleId(sysRoleMenuButtonRequest.getRoleId());
item.setMenuId(menuId);
sysRoleMenus.add(item);
}
roleMenuService.saveBatch(sysRoleMenus);
}
// 新增按钮
List<SysRoleMenuButtonRequest> menuButtonList = sysRoleMenuButtonRequest.getGrantMenuButtonIdList();
if (ObjectUtil.isNotEmpty(menuButtonList)) {
List<SysRoleMenuButton> sysRoleMenuButtons = new ArrayList<>();
for (SysRoleMenuButtonRequest menuButton : menuButtonList) {
SysRoleMenuButton item = new SysRoleMenuButton();
item.setRoleId(sysRoleMenuButtonRequest.getRoleId());
item.setButtonId(menuButton.getButtonId());
item.setButtonCode(menuButton.getButtonCode());
sysRoleMenuButtons.add(item);
}
sysRoleMenuButtonService.saveBatch(sysRoleMenuButtons);
}
}
/**
*
*

View File

@ -170,9 +170,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
// 更新枚举,更新只能更新未删除状态的
LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SysUser::getUserId, id)
.and(i -> i.ne(SysUser::getDelFlag, YesOrNotEnum.Y.getCode()))
.set(SysUser::getStatusFlag, statusFlag);
updateWrapper.eq(SysUser::getUserId, id).and(i -> i.ne(SysUser::getDelFlag, YesOrNotEnum.Y.getCode())).set(SysUser::getStatusFlag, statusFlag);
boolean update = this.update(updateWrapper);
if (!update) {
@ -319,8 +317,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Override
public PageResult<SysUserResponse> page(SysUserRequest sysUserRequest) {
Page<SysUserResponse> userPage =
this.baseMapper.findUserPage(PageFactory.defaultPage(), sysUserRequest);
Page<SysUserResponse> userPage = this.baseMapper.findUserPage(PageFactory.defaultPage(), sysUserRequest);
return PageResultFactory.createPageResult(userPage);
}
@ -478,6 +475,15 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return sessionManagerApi.onlineUserList();
}
@Override
public cn.stylefeng.roses.kernel.system.pojo.user.SysUserResponse getUserInfoByUserId(Long userId) {
SysUser sysUser = this.getById(userId);
if (ObjectUtil.isNotEmpty(sysUser)) {
return BeanUtil.copyProperties(sysUser, cn.stylefeng.roses.kernel.system.pojo.user.SysUserResponse.class);
}
return null;
}
/**
*
*