|
@@ -0,0 +1,409 @@
|
|
|
+package org.jeecg.modules.interlockUser.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.shiro.SecurityUtils;
|
|
|
+import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.common.constant.CommonConstant;
|
|
|
+import org.jeecg.common.constant.SymbolConstant;
|
|
|
+import org.jeecg.common.system.vo.LoginUser;
|
|
|
+import org.jeecg.common.util.Md5Util;
|
|
|
+import org.jeecg.common.util.oConvertUtils;
|
|
|
+import org.jeecg.config.JeecgBaseConfig;
|
|
|
+import org.jeecg.modules.interlockUser.entity.InterlockUser;
|
|
|
+import org.jeecg.modules.interlockUser.mapper.InterlockUserMapper;
|
|
|
+import org.jeecg.modules.interlockUser.service.IInterlockUserRolePermissionService;
|
|
|
+import org.jeecg.modules.system.controller.SysPermissionController;
|
|
|
+import org.jeecg.modules.system.entity.SysPermission;
|
|
|
+import org.jeecg.modules.system.entity.SysRoleIndex;
|
|
|
+import org.jeecg.modules.system.service.ISysPermissionService;
|
|
|
+import org.jeecg.modules.system.service.ISysUserService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * ClassName: InterlockUserRolePermissionController
|
|
|
+ * Package: org.jeecg.modules.interlockUser.controller
|
|
|
+ * Description: 改写系统的用户角色获取菜单权限接口
|
|
|
+ *
|
|
|
+ * @Author sl
|
|
|
+ * @Create 2024/8/6 15:43
|
|
|
+ * @Version 1.0
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/interlockUser/interlockUser")
|
|
|
+@Slf4j
|
|
|
+public class InterlockUserRolePermissionController {
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private JeecgBaseConfig jeecgBaseConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private SysPermissionController sysPermissionController;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private ISysPermissionService sysPermissionService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private InterlockUserMapper interlockUserMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private IInterlockUserRolePermissionService interlockUserRolePermissionService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 子菜单
|
|
|
+ */
|
|
|
+ private static final String CHILDREN = "children";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询用户拥有的菜单权限和按钮权限
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/getInterlockUserPermissionByToken", method = RequestMethod.GET)
|
|
|
+ //@DynamicTable(value = DynamicTableConstant.SYS_ROLE_INDEX)
|
|
|
+ @ApiOperation("查询用户拥有的菜单权限和按钮权限")
|
|
|
+ public Result<?> getInterlockUserPermissionByToken(HttpServletRequest request) {
|
|
|
+ Result<JSONObject> result = new Result<JSONObject>();
|
|
|
+ String wiseUser = "";
|
|
|
+ try {
|
|
|
+ //获取当前用户名
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
+ if (cookies != null) {
|
|
|
+ for (Cookie cookie : cookies) {
|
|
|
+ if (cookie.getName().equals("WISEUser")) {
|
|
|
+ wiseUser = cookie.getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+// wiseUser = "测试用户2";
|
|
|
+ if (wiseUser.equals("")) return Result.error("请登录系统!");
|
|
|
+
|
|
|
+ //直接获取当前用户不适用前端token
|
|
|
+ InterlockUser interlockUser = interlockUserMapper.getInterlockUserByUserName(wiseUser);
|
|
|
+// LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
|
+ if (oConvertUtils.isEmpty(interlockUser)) {
|
|
|
+ return Result.error("请登录系统!");
|
|
|
+ }
|
|
|
+ List<SysPermission> metaList = interlockUserRolePermissionService.queryByUser(wiseUser);
|
|
|
+// System.out.println(metaList);
|
|
|
+ //update-begin--Author:zyf Date:20220425 for:自定义首页地址 LOWCOD-1578
|
|
|
+ String version = request.getHeader(CommonConstant.VERSION);
|
|
|
+ //update-begin---author:liusq ---date:2022-06-29 for:接口返回值修改,同步修改这里的判断逻辑-----------
|
|
|
+ SysRoleIndex roleIndex= interlockUserRolePermissionService.getDynamicIndexByUserRole(wiseUser,version);
|
|
|
+ System.out.println(roleIndex);
|
|
|
+ //update-end---author:liusq ---date:2022-06-29 for:接口返回值修改,同步修改这里的判断逻辑-----------
|
|
|
+ //update-end--Author:zyf Date:20220425 for:自定义首页地址 LOWCOD-1578
|
|
|
+
|
|
|
+ if(roleIndex!=null){
|
|
|
+ List<SysPermission> menus = metaList.stream().filter(sysPermission -> "首页".equals(sysPermission.getName())).collect(Collectors.toList());
|
|
|
+ //update-begin---author:liusq ---date:2022-06-29 for:设置自定义首页地址和组件----------
|
|
|
+ String component = roleIndex.getComponent();
|
|
|
+ String routeUrl = roleIndex.getUrl();
|
|
|
+ boolean route = roleIndex.isRoute();
|
|
|
+ if(oConvertUtils.isNotEmpty(routeUrl)){
|
|
|
+ menus.get(0).setComponent(component);
|
|
|
+ menus.get(0).setRoute(route);
|
|
|
+ menus.get(0).setUrl(routeUrl);
|
|
|
+ }else{
|
|
|
+ menus.get(0).setComponent(component);
|
|
|
+ }
|
|
|
+ //update-end---author:liusq ---date:2022-06-29 for:设置自定义首页地址和组件-----------
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ JSONArray menujsonArray = new JSONArray();
|
|
|
+ this.getPermissionJsonArray(menujsonArray, metaList, null);
|
|
|
+ //一级菜单下的子菜单全部是隐藏路由,则一级菜单不显示
|
|
|
+ this.handleFirstLevelMenuHidden(menujsonArray);
|
|
|
+
|
|
|
+ JSONArray authjsonArray = new JSONArray();
|
|
|
+ this.getAuthJsonArray(authjsonArray, metaList);
|
|
|
+ //查询所有的权限
|
|
|
+ LambdaQueryWrapper<SysPermission> query = new LambdaQueryWrapper<SysPermission>();
|
|
|
+ query.eq(SysPermission::getDelFlag, CommonConstant.DEL_FLAG_0);
|
|
|
+ query.eq(SysPermission::getMenuType, CommonConstant.MENU_TYPE_2);
|
|
|
+ //query.eq(SysPermission::getStatus, "1");
|
|
|
+ List<SysPermission> allAuthList = sysPermissionService.list(query);
|
|
|
+ JSONArray allauthjsonArray = new JSONArray();
|
|
|
+ this.getAllAuthJsonArray(allauthjsonArray, allAuthList);
|
|
|
+ //路由菜单
|
|
|
+ json.put("menu", menujsonArray);
|
|
|
+ //按钮权限(用户拥有的权限集合)
|
|
|
+ json.put("auth", authjsonArray);
|
|
|
+ //全部权限配置集合(按钮权限,访问权限)
|
|
|
+ json.put("allAuth", allauthjsonArray);
|
|
|
+ json.put("sysSafeMode", jeecgBaseConfig.getSafeMode());
|
|
|
+ result.setResult(json);
|
|
|
+ } catch (Exception e) {
|
|
|
+ result.error500("查询失败:" + e.getMessage());
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取菜单JSON数组
|
|
|
+ * @param jsonArray
|
|
|
+ * @param metaList
|
|
|
+ * @param parentJson
|
|
|
+ */
|
|
|
+ private void getPermissionJsonArray(JSONArray jsonArray, List<SysPermission> metaList, JSONObject parentJson) {
|
|
|
+ for (SysPermission permission : metaList) {
|
|
|
+ if (permission.getMenuType() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String tempPid = permission.getParentId();
|
|
|
+ JSONObject json = getPermissionJsonObject(permission);
|
|
|
+ if(json==null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (parentJson == null && oConvertUtils.isEmpty(tempPid)) {
|
|
|
+ jsonArray.add(json);
|
|
|
+ if (!permission.isLeaf()) {
|
|
|
+ getPermissionJsonArray(jsonArray, metaList, json);
|
|
|
+ }
|
|
|
+ } else if (parentJson != null && oConvertUtils.isNotEmpty(tempPid) && tempPid.equals(parentJson.getString("id"))) {
|
|
|
+ // 类型( 0:一级菜单 1:子菜单 2:按钮 )
|
|
|
+ if (permission.getMenuType().equals(CommonConstant.MENU_TYPE_2)) {
|
|
|
+ JSONObject metaJson = parentJson.getJSONObject("meta");
|
|
|
+ if (metaJson.containsKey("permissionList")) {
|
|
|
+ metaJson.getJSONArray("permissionList").add(json);
|
|
|
+ } else {
|
|
|
+ JSONArray permissionList = new JSONArray();
|
|
|
+ permissionList.add(json);
|
|
|
+ metaJson.put("permissionList", permissionList);
|
|
|
+ }
|
|
|
+ // 类型( 0:一级菜单 1:子菜单 2:按钮 )
|
|
|
+ } else if (permission.getMenuType().equals(CommonConstant.MENU_TYPE_1) || permission.getMenuType().equals(CommonConstant.MENU_TYPE_0)) {
|
|
|
+ if (parentJson.containsKey("children")) {
|
|
|
+ parentJson.getJSONArray("children").add(json);
|
|
|
+ } else {
|
|
|
+ JSONArray children = new JSONArray();
|
|
|
+ children.add(json);
|
|
|
+ parentJson.put("children", children);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!permission.isLeaf()) {
|
|
|
+ getPermissionJsonArray(jsonArray, metaList, json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据菜单配置生成路由json
|
|
|
+ * @param permission
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private JSONObject getPermissionJsonObject(SysPermission permission) {
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ // 类型(0:一级菜单 1:子菜单 2:按钮)
|
|
|
+ if (permission.getMenuType().equals(CommonConstant.MENU_TYPE_2)) {
|
|
|
+ //json.put("action", permission.getPerms());
|
|
|
+ //json.put("type", permission.getPermsType());
|
|
|
+ //json.put("describe", permission.getName());
|
|
|
+ return null;
|
|
|
+ } else if (permission.getMenuType().equals(CommonConstant.MENU_TYPE_0) || permission.getMenuType().equals(CommonConstant.MENU_TYPE_1)) {
|
|
|
+ json.put("id", permission.getId());
|
|
|
+ if (permission.isRoute()) {
|
|
|
+ //表示生成路由
|
|
|
+ json.put("route", "1");
|
|
|
+ } else {
|
|
|
+ //表示不生成路由
|
|
|
+ json.put("route", "0");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isWwwHttpUrl(permission.getUrl())) {
|
|
|
+ json.put("path", Md5Util.md5Encode(permission.getUrl(), "utf-8"));
|
|
|
+ } else {
|
|
|
+ json.put("path", permission.getUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重要规则:路由name (通过URL生成路由name,路由name供前端开发,页面跳转使用)
|
|
|
+ if (oConvertUtils.isNotEmpty(permission.getComponentName())) {
|
|
|
+ json.put("name", permission.getComponentName());
|
|
|
+ } else {
|
|
|
+ json.put("name", urlToRouteName(permission.getUrl()));
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject meta = new JSONObject();
|
|
|
+ // 是否隐藏路由,默认都是显示的
|
|
|
+ if (permission.isHidden()) {
|
|
|
+ json.put("hidden", true);
|
|
|
+ //vue3版本兼容代码
|
|
|
+ meta.put("hideMenu",true);
|
|
|
+ }
|
|
|
+ // 聚合路由
|
|
|
+ if (permission.isAlwaysShow()) {
|
|
|
+ json.put("alwaysShow", true);
|
|
|
+ }
|
|
|
+ json.put("component", permission.getComponent());
|
|
|
+ // 由用户设置是否缓存页面 用布尔值
|
|
|
+ if (permission.isKeepAlive()) {
|
|
|
+ meta.put("keepAlive", true);
|
|
|
+ } else {
|
|
|
+ meta.put("keepAlive", false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /*update_begin author:wuxianquan date:20190908 for:往菜单信息里添加外链菜单打开方式 */
|
|
|
+ //外链菜单打开方式
|
|
|
+ if (permission.isInternalOrExternal()) {
|
|
|
+ meta.put("internalOrExternal", true);
|
|
|
+ } else {
|
|
|
+ meta.put("internalOrExternal", false);
|
|
|
+ }
|
|
|
+ /* update_end author:wuxianquan date:20190908 for: 往菜单信息里添加外链菜单打开方式*/
|
|
|
+
|
|
|
+ meta.put("title", permission.getName());
|
|
|
+
|
|
|
+ //update-begin--Author:scott Date:20201015 for:路由缓存问题,关闭了tab页时再打开就不刷新 #842
|
|
|
+ String component = permission.getComponent();
|
|
|
+ if(oConvertUtils.isNotEmpty(permission.getComponentName()) || oConvertUtils.isNotEmpty(component)){
|
|
|
+ meta.put("componentName", oConvertUtils.getString(permission.getComponentName(),component.substring(component.lastIndexOf("/")+1)));
|
|
|
+ }
|
|
|
+ //update-end--Author:scott Date:20201015 for:路由缓存问题,关闭了tab页时再打开就不刷新 #842
|
|
|
+
|
|
|
+ if (oConvertUtils.isEmpty(permission.getParentId())) {
|
|
|
+ // 一级菜单跳转地址
|
|
|
+ json.put("redirect", permission.getRedirect());
|
|
|
+ if (oConvertUtils.isNotEmpty(permission.getIcon())) {
|
|
|
+ meta.put("icon", permission.getIcon());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (oConvertUtils.isNotEmpty(permission.getIcon())) {
|
|
|
+ meta.put("icon", permission.getIcon());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (isWwwHttpUrl(permission.getUrl())) {
|
|
|
+ meta.put("url", permission.getUrl());
|
|
|
+ }
|
|
|
+ // update-begin--Author:sunjianlei Date:20210918 for:新增适配vue3项目的隐藏tab功能
|
|
|
+ if (permission.isHideTab()) {
|
|
|
+ meta.put("hideTab", true);
|
|
|
+ }
|
|
|
+ // update-end--Author:sunjianlei Date:20210918 for:新增适配vue3项目的隐藏tab功能
|
|
|
+ json.put("meta", meta);
|
|
|
+ }
|
|
|
+
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否外网URL 例如: http://localhost:8080/jeecg-boot/swagger-ui.html#/ 支持特殊格式: {{
|
|
|
+ * window._CONFIG['domianURL'] }}/druid/ {{ JS代码片段 }},前台解析会自动执行JS代码片段
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private boolean isWwwHttpUrl(String url) {
|
|
|
+ boolean flag = url != null && (url.startsWith(CommonConstant.HTTP_PROTOCOL) || url.startsWith(CommonConstant.HTTPS_PROTOCOL) || url.startsWith(SymbolConstant.DOUBLE_LEFT_CURLY_BRACKET));
|
|
|
+ if (flag) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过URL生成路由name(去掉URL前缀斜杠,替换内容中的斜杠‘/’为-) 举例: URL = /isystem/role RouteName =
|
|
|
+ * isystem-role
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String urlToRouteName(String url) {
|
|
|
+ if (oConvertUtils.isNotEmpty(url)) {
|
|
|
+ if (url.startsWith(SymbolConstant.SINGLE_SLASH)) {
|
|
|
+ url = url.substring(1);
|
|
|
+ }
|
|
|
+ url = url.replace("/", "-");
|
|
|
+
|
|
|
+ // 特殊标记
|
|
|
+ url = url.replace(":", "@");
|
|
|
+ return url;
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 一级菜单的子菜单全部是隐藏路由,则一级菜单不显示
|
|
|
+ * @param jsonArray
|
|
|
+ */
|
|
|
+ private void handleFirstLevelMenuHidden(JSONArray jsonArray) {
|
|
|
+ jsonArray = jsonArray.stream().map(obj -> {
|
|
|
+ JSONObject returnObj = new JSONObject();
|
|
|
+ JSONObject jsonObj = (JSONObject)obj;
|
|
|
+ if(jsonObj.containsKey(CHILDREN)){
|
|
|
+ JSONArray childrens = jsonObj.getJSONArray(CHILDREN);
|
|
|
+ childrens = childrens.stream().filter(arrObj -> !"true".equals(((JSONObject) arrObj).getString("hidden"))).collect(Collectors.toCollection(JSONArray::new));
|
|
|
+ if(childrens==null || childrens.size()==0){
|
|
|
+ jsonObj.put("hidden",true);
|
|
|
+
|
|
|
+ //vue3版本兼容代码
|
|
|
+ JSONObject meta = new JSONObject();
|
|
|
+ meta.put("hideMenu",true);
|
|
|
+ jsonObj.put("meta", meta);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return returnObj;
|
|
|
+ }).collect(Collectors.toCollection(JSONArray::new));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取权限JSON数组
|
|
|
+ * @param jsonArray
|
|
|
+ * @param metaList
|
|
|
+ */
|
|
|
+ private void getAuthJsonArray(JSONArray jsonArray,List<SysPermission> metaList) {
|
|
|
+ for (SysPermission permission : metaList) {
|
|
|
+ if(permission.getMenuType()==null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ JSONObject json = null;
|
|
|
+ if(permission.getMenuType().equals(CommonConstant.MENU_TYPE_2) &&CommonConstant.STATUS_1.equals(permission.getStatus())) {
|
|
|
+ json = new JSONObject();
|
|
|
+ json.put("action", permission.getPerms());
|
|
|
+ json.put("type", permission.getPermsType());
|
|
|
+ json.put("describe", permission.getName());
|
|
|
+ jsonArray.add(json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取权限JSON数组
|
|
|
+ * @param jsonArray
|
|
|
+ * @param allList
|
|
|
+ */
|
|
|
+ private void getAllAuthJsonArray(JSONArray jsonArray,List<SysPermission> allList) {
|
|
|
+ JSONObject json = null;
|
|
|
+ for (SysPermission permission : allList) {
|
|
|
+ json = new JSONObject();
|
|
|
+ json.put("action", permission.getPerms());
|
|
|
+ json.put("status", permission.getStatus());
|
|
|
+ //1显示2禁用
|
|
|
+ json.put("type", permission.getPermsType());
|
|
|
+ json.put("describe", permission.getName());
|
|
|
+ jsonArray.add(json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|