feat: 优化组织机构、新增数据权限

1. 优化组织机构
2. 新增数据权限
pull/9/head
Carina 3 years ago
parent d948915ed8
commit 61ec20b260

@ -3,7 +3,7 @@
<img width="500" src="https://gitee.com/hiparker/opsli-ui/raw/master/repository-images/logo.png"/>
<br/> <br/>
[![AUR](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg)](https://github.com/hiparker/opsli-boot/blob/master/LICENSE) [![spring-boot](https://img.shields.io/badge/spring--boot-2.3.3.RELEASE-green.svg)](http://spring.io/projects/spring-boot) [![mybatis-plus](https://img.shields.io/badge/mybatis--plus-3.4.0-blue.svg)](http://mp.baomidou.com) [![hutool](https://img.shields.io/badge/hutool-5.6.3-blue.svg)](https://www.hutool.cn) [![Stars](https://img.shields.io/github/stars/hiparker/opsli-boot?style=flat-square&label=Stars&logo=github)](https://github.com/hiparker/opsli-boot) [![Forks](https://img.shields.io/github/forks/hiparker/opsli-boot?style=flat-square&label=Forks&logo=github)](https://github.com/hiparker/opsli-boot)
[![AUR](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg)](https://github.com/hiparker/opsli-boot/blob/master/LICENSE) [![spring-boot](https://img.shields.io/badge/spring--boot-2.3.3.RELEASE-green.svg)](http://spring.io/projects/spring-boot) [![mybatis-plus](https://img.shields.io/badge/mybatis--plus-3.4.0-blue.svg)](http://mp.baomidou.com) [![hutool](https://img.shields.io/badge/hutool-5.7.14-blue.svg)](https://www.hutool.cn) [![Stars](https://img.shields.io/github/stars/hiparker/opsli-boot?style=flat-square&label=Stars&logo=github)](https://github.com/hiparker/opsli-boot) [![Forks](https://img.shields.io/github/forks/hiparker/opsli-boot?style=flat-square&label=Forks&logo=github)](https://github.com/hiparker/opsli-boot)
</div>
## 关于
@ -39,7 +39,7 @@
| shiro-redis版本 | ^3.3.1 | | jwt版本 | ^3.10.3 |
| ehcache版本 | ^3.9.0 | | easyexcel版本 | ^2.2.6 |
| kaptcha版本 | ^0.0.9 | | guava版本 | ^29.0-jre |
| enjoy版本 | ^4.9.03 | | hutool版本 | ^5.6.3 |
| enjoy版本 | ^4.9.03 | | hutool版本 | ^5.7.14 |
## 在线演示

@ -17,6 +17,7 @@ package org.opsli.api.web.system.user;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserInfo;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserOrgRefWebModel;
@ -235,38 +236,6 @@ public interface UserApi {
//@GetMapping("/getUserByUsername")
ResultVo<UserModel> getUserByUsername(String username);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/getRolesByUserId")
ResultVo<List<String>> getRolesByUserId(String userId);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/queryAllPerms")
ResultVo<List<String>> getAllPerms(String userId);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/queryAllPerms")
ResultVo<List<MenuModel>> getMenuListByUserId(String userId);
/**
*
*
* @param userId ID
* @return ResultVo
*/
ResultVo<UserOrgRefWebModel> getOrgInfoByUserId(String userId);
}

@ -16,6 +16,7 @@
package org.opsli.api.web.system.user;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.org.SysOrgModel;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.api.wrapper.system.user.UserOrgRefWebModel;
import org.springframework.web.bind.annotation.PostMapping;
@ -58,4 +59,21 @@ public interface UserOrgRefApi {
*/
ResultVo<List<UserOrgRefModel>> findListByUserId(String userId);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/getRolesByUserId")
ResultVo<UserOrgRefModel> getDefOrgByUserId(String userId);
/**
*
*
* @param userId ID
* @return ResultVo
*/
ResultVo<UserOrgRefWebModel> getOrgInfoByUserId(String userId);
}

@ -16,12 +16,16 @@
package org.opsli.api.web.system.user;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.role.RoleMenuRefModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserRoleRefModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
/**
* API
@ -41,6 +45,13 @@ public interface UserRoleRefApi {
/** 子标题 */
String SUB_TITLE = "用户角色";
/**
*
* @param userId ID
* @return ResultVo
*/
@GetMapping("/getRoles")
ResultVo<?> getRoles(String userId);
/**
*
@ -50,4 +61,37 @@ public interface UserRoleRefApi {
@PostMapping("/setRoles")
ResultVo<?> setRoles(@RequestBody UserRoleRefModel model);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/getRolesByUserId")
ResultVo<List<String>> getRolesByUserId(String userId);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/getRolesByUserId")
ResultVo<RoleModel> getDefRoleByUserId(String userId);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/queryAllPerms")
ResultVo<List<String>> getAllPerms(String userId);
/**
* userId
* @param userId Id
* @return ResultVo
*/
//@GetMapping("/queryAllPerms")
ResultVo<List<MenuModel>> getMenuListByUserId(String userId);
}

@ -60,9 +60,18 @@ public class RoleModel extends ApiWrapper {
@ValidatorLenMax(1)
private String izLock;
/** 授权数据范围 */
@ApiModelProperty(value = "授权数据范围")
@ExcelProperty(value = "授权数据范围", order = 4)
@ExcelInfo(dictType = "role_data_scope")
@Validator({ValidatorType.IS_NOT_NULL})
@ValidatorLenMax(5)
private String dataScope;
/** 备注 */
@ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注", order = 4)
@ExcelProperty(value = "备注", order = 5)
@ExcelInfo
@ValidatorLenMax(255)
private String remark;

@ -17,6 +17,7 @@ package org.opsli.api.wrapper.system.user;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opsli.common.annotation.validator.Validator;
@ -34,6 +35,7 @@ import java.io.Serializable;
@Data
@EqualsAndHashCode(callSuper = false)
@ExcelIgnoreUnannotated
@Builder
public class UserRoleRefModel implements Serializable {
/** 用户ID */
@ -46,4 +48,9 @@ public class UserRoleRefModel implements Serializable {
@ApiModelProperty(value = "权限数组")
private String[] roleIds;
/** 默认角色ID */
@ApiModelProperty(value = "默认角色")
@Validator({ValidatorType.IS_NOT_NULL})
private String defRoleId;
}

@ -53,5 +53,5 @@ public interface MyBatisConstants {
/** 多租户字段 */
String FIELD_TENANT = "tenantId";
/** 组织字段 */
String FIELD_ORG_GROUP = "org_group";
String FIELD_ORG_GROUP = "orgIds";
}

@ -365,7 +365,7 @@ public class CheckStrength {
*/
public static boolean equalsNull(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0 || str.equalsIgnoreCase("null")) {
if (str == null || (strLen = str.length()) == 0 || "null".equalsIgnoreCase(str)) {
return true;
}
for (int i = 0; i < strLen; i++) {

@ -17,6 +17,12 @@ package org.opsli.common.utils;
import org.springframework.util.StringUtils;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Function;
/**
*
*
@ -101,8 +107,56 @@ public final class FieldUtil {
return str;
}
/**
*
* @param fn
* @param <T>
* @return String
*/
public static <T> String getFileName(SFunction<T, ?> fn) {
// 从function取出序列化方法
Method writeReplaceMethod;
try {
writeReplaceMethod = fn.getClass().getDeclaredMethod("writeReplace");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
// 从序列化方法取出序列化的lambda信息
boolean isAccessible = writeReplaceMethod.isAccessible();
writeReplaceMethod.setAccessible(true);
SerializedLambda serializedLambda;
try {
serializedLambda = (SerializedLambda) writeReplaceMethod.invoke(fn);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
writeReplaceMethod.setAccessible(isAccessible);
// 从lambda信息取出method、field、class等
String fieldName = serializedLambda.getImplMethodName().substring("get".length());
fieldName = fieldName.replaceFirst(fieldName.charAt(0) + "", (fieldName.charAt(0) + "").toLowerCase());
// Field field;
// try {
// field = Class.forName(serializedLambda.getImplClass().replace("/", ".")).getDeclaredField(fieldName);
// } catch (ClassNotFoundException | NoSuchFieldException e) {
// throw new RuntimeException(e);
// }
return fieldName;
}
// ====================
private FieldUtil(){}
/**
* 使Function
*/
@FunctionalInterface
public interface SFunction<T, R> extends Function<T, R>, Serializable {
}
private FieldUtil(){}
}

@ -32,7 +32,7 @@ import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.chain.QueryOrgHandler;
import org.opsli.core.persistence.querybuilder.chain.QueryDataPermsHandler;
import org.opsli.core.persistence.querybuilder.chain.QueryTenantHandler;
import org.springframework.transaction.annotation.Transactional;
@ -213,7 +213,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEnt
public List<T> findList(QueryWrapper<T> queryWrapper) {
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, queryWrapper);
return super.list(queryWrapper);
@ -225,7 +225,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEnt
QueryWrapper<T> queryWrapper = queryBuilder.build();
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, queryWrapper);
return super.list(queryWrapper);

@ -23,6 +23,7 @@ import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.enums.MsgArgsType;
import org.opsli.core.cache.pushsub.enums.PushSubType;
import org.opsli.core.utils.OrgUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.plugins.cache.EhCachePlugin;
import org.springframework.beans.factory.annotation.Autowired;
@ -66,7 +67,7 @@ public class OrgHandler implements RedisPushSubHandler{
return;
}
String cacheKey = CacheUtil.handleKey(OrgUtil.PREFIX_CODE + userId);
String cacheKey = CacheUtil.handleKey(UserUtil.PREFIX_ID_ORGS + userId);
// 先删除
ehCachePlugin.delete(CacheConstants.EHCACHE_SPACE, cacheKey);

@ -28,8 +28,11 @@ import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.opsli.api.wrapper.system.org.SysOrgModel;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.core.utils.UserTokenUtil;
import org.opsli.core.utils.UserUtil;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
@ -180,6 +183,19 @@ public class MybatisAutoFillInterceptor implements Interceptor {
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_TENANT, UserTokenUtil.getTenantIdByToken());
}
break;
// 组织机构设置
case MyBatisConstants.FIELD_ORG_GROUP:
// 如果组织IDs 为空则进行默认赋值
Object orgValue = ReflectUtil.getFieldValue(arg, f.getName());
if(StringUtils.isBlank(Convert.toStr(orgValue))){
UserOrgRefModel userOrgRefModel =
UserUtil.getUserDefOrgByUserId(UserTokenUtil.getUserIdByToken());
if(null != userOrgRefModel){
String orgIds = userOrgRefModel.getOrgIds();
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_ORG_GROUP, orgIds);
}
}
break;
default:
break;
}

@ -50,7 +50,7 @@ public enum TokenMsg implements BaseMsg {
EXCEPTION_LOGIN_DECRYPT(12107,"登录账号密码解析失败"),
EXCEPTION_USER_ROLE_NOT_NULL(12108,"用户暂无角色,请设置后登录"),
EXCEPTION_USER_MENU_NOT_NULL(12109,"用户暂无角色菜单,请设置后登录"),
EXCEPTION_USER_PERMS_NOT_NULL(12110,"用户暂无权限,请设置后登录"),
/**
*
*/

@ -17,6 +17,7 @@ package org.opsli.core.persistence.querybuilder.chain;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.persistence.querybuilder.conf.WebQueryConf;
/**
*
@ -35,4 +36,15 @@ public interface QueryBuilderChain {
*/
<T extends BaseEntity> QueryWrapper<T> handler(Class<T> entityClazz, QueryWrapper<T> wrapper);
/**
*
* @param entityClazz entity class
* @param wrapper
* @param webQueryConf
* @param <T>
* @return <T>
*/
<T extends BaseEntity> QueryWrapper<T> handler(Class<T> entityClazz, WebQueryConf webQueryConf,
QueryWrapper<T> wrapper);
}

@ -0,0 +1,232 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.opsli.core.persistence.querybuilder.chain;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.utils.FieldUtil;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.persistence.querybuilder.conf.WebQueryConf;
import org.opsli.core.utils.UserUtil;
import java.util.List;
/**
*
*
* @author Parker
* @date 2020-09-13 19:36
*/
public class QueryDataPermsHandler implements QueryBuilderChain{
/**
*
*/
private QueryBuilderChain queryBuilderChain;
public QueryDataPermsHandler(){}
/**
*
* @param queryBuilderChain
*/
public QueryDataPermsHandler(QueryBuilderChain queryBuilderChain){
this.queryBuilderChain = queryBuilderChain;
}
@Override
public <T extends BaseEntity> QueryWrapper<T> handler(Class<T> entityClazz, QueryWrapper<T> wrapper) {
// 执行 子 责任链
if(queryBuilderChain != null){
wrapper = queryBuilderChain.handler(entityClazz, wrapper);
}
// 自身责任 -- 判断组织
boolean flag = ReflectUtil.hasField(entityClazz, MyBatisConstants.FIELD_ORG_GROUP);
if(flag) {
String fieldName = FieldUtil.humpToUnderline(MyBatisConstants.FIELD_ORG_GROUP);
// 处理查询条件
handleDataPermsCondition(fieldName, wrapper);
}
return wrapper;
}
@Override
public <T extends BaseEntity> QueryWrapper<T> handler(Class<T> entityClazz, WebQueryConf webQueryConf, QueryWrapper<T> wrapper) {
// 执行 子 责任链
if(queryBuilderChain != null){
wrapper = queryBuilderChain.handler(entityClazz, webQueryConf, wrapper);
}
// 自身责任 -- 判断组织
boolean flag = ReflectUtil.hasField(entityClazz, MyBatisConstants.FIELD_ORG_GROUP);
if(flag) {
String fieldName = webQueryConf.get(MyBatisConstants.FIELD_ORG_GROUP);
if(StringUtils.isEmpty(fieldName)){
fieldName = FieldUtil.humpToUnderline(MyBatisConstants.FIELD_ORG_GROUP);
}
// 处理查询条件
handleDataPermsCondition(fieldName, wrapper);
}
return wrapper;
}
/**
*
*
* @param field
* @param queryWrapper
*/
private static <T extends BaseEntity> QueryWrapper<T> handleDataPermsCondition(String field, QueryWrapper<T> queryWrapper) {
// 1. 当前用户
UserModel currUser = UserUtil.getUser();
// 2. 当前用户 组织机构集合
List<UserOrgRefModel> userOrgRefModelList = UserUtil.getOrgListByUserId(currUser.getId());
List<String> orgIdGroupList = Lists.newArrayListWithCapacity(userOrgRefModelList.size());
for (UserOrgRefModel userOrgRefModel : userOrgRefModelList) {
orgIdGroupList.add(userOrgRefModel.getOrgIds());
}
// 组织机构集合 去重
orgIdGroupList = ListDistinctUtil.distinct(orgIdGroupList);
// 3. 获得查询类型
// 如果是超级管理员 则查询类型为全部
ConditionType conditionType = ConditionType.SELF;
if(StringUtils.equals(UserUtil.SUPER_ADMIN, currUser.getUsername())){
conditionType = ConditionType.ALL;
}else{
// 如果不是超级管理员 则获得当前用户的默认角色下的 授权数据权限类型
RoleModel defRole = UserUtil.getUserDefRoleByUserId(currUser.getId());
if(null != defRole){
conditionType = ConditionType.getConditionType(defRole.getDataScope());
}
}
// 4. 如果查询字段为空 则默认
if(StringUtils.isBlank(field)){
field = FieldUtil.humpToUnderline(MyBatisConstants.FIELD_ORG_GROUP);
}
// 常量
final ConditionType finalConditionType = conditionType;
final String finalField = field;
final List<String> finalOrgIdGroupList = orgIdGroupList;
// 查询 全部
if(ConditionType.ALL.equals(finalConditionType)){
// ..
}else {
queryWrapper.and(wra -> {
// 查询 本部门
if(ConditionType.DEPT.equals(finalConditionType)){
wra.in(finalField, finalOrgIdGroupList);
}
// 部门及以下
else if(ConditionType.DEPT_AND_BELOW.equals(finalConditionType)){
wra.and(wraConfine -> {
// 增加右模糊 查询条件
for (int i = 0; i < finalOrgIdGroupList.size(); i++) {
// 右模糊匹配
wraConfine.likeRight(
finalField, finalOrgIdGroupList.get(i));
if(i < finalOrgIdGroupList.size() - 1){
wraConfine.or();
}
}
});
}else {
// 查自身
wra.in(FieldUtil.humpToUnderline(MyBatisConstants.FIELD_CREATE_BY), currUser.getId());
}
});
}
return queryWrapper;
}
// =================================
/**
*
*/
public enum ConditionType{
/** 自身 */
SELF("0", "自身"),
/** 部门 */
DEPT("1", "本部门"),
/** 部门及以下 */
DEPT_AND_BELOW("2", "本部门及以下"),
/** 全部 */
ALL("3", "全部");
/** 值 */
private final String value;
/** 描述 */
private final String describe;
ConditionType(String value, String describe){
this.value = value;
this.describe = describe;
}
public String getValue() {
return value;
}
public String getDescribe() {
return describe;
}
/**
*
* @param value
* @return AlgSource
*/
public static ConditionType getConditionType(String value) {
ConditionType[] var1 = values();
for (ConditionType source : var1) {
if(source.value.equals(value)){
return source;
}
}
// 如果条件类型为空 则默认 查看自身数据
return ConditionType.SELF;
}
}
}

@ -1,71 +0,0 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.opsli.core.persistence.querybuilder.chain;
import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.utils.FieldUtil;
import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.utils.UserUtil;
/**
*
*
* @author Parker
* @date 2020-09-13 19:36
*/
public class QueryOrgHandler implements QueryBuilderChain{
/**
*
*/
private QueryBuilderChain queryBuilderChain;
public QueryOrgHandler(){}
/**
*
* @param queryBuilderChain
*/
public QueryOrgHandler(QueryBuilderChain queryBuilderChain){
this.queryBuilderChain = queryBuilderChain;
}
@Override
public <T extends BaseEntity> QueryWrapper<T> handler(Class<T> entityClazz, QueryWrapper<T> wrapper) {
// 执行 子 责任链
if(queryBuilderChain != null){
wrapper = queryBuilderChain.handler(entityClazz, wrapper);
}
// 自身责任 -- 判断组织
boolean flag = ReflectUtil.hasField(entityClazz, MyBatisConstants.FIELD_ORG_GROUP);
if(flag) {
// 1. 获得 用户 数据权限
// 2. 获得 角色 数据权限
}
return wrapper;
}
}

@ -22,6 +22,7 @@ import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.utils.FieldUtil;
import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.persistence.querybuilder.conf.WebQueryConf;
import org.opsli.core.utils.UserUtil;
/**
@ -70,4 +71,31 @@ public class QueryTenantHandler implements QueryBuilderChain{
return wrapper;
}
@Override
public <T extends BaseEntity> QueryWrapper<T> handler(Class<T> entityClazz, WebQueryConf webQueryConf, QueryWrapper<T> wrapper) {
// 执行 子 责任链
if(queryBuilderChain != null){
wrapper = queryBuilderChain.handler(entityClazz, webQueryConf, wrapper);
}
// 自身责任 -- 判断多租户
boolean tenantFlag = ReflectUtil.hasField(entityClazz, MyBatisConstants.FIELD_TENANT);
if(tenantFlag) {
String tenantId = UserUtil.getTenantId();
UserModel user = UserUtil.getUser();
// 超级管理员可以操作 无租户限制, 其余用户全部有租户限制
if(!UserUtil.SUPER_ADMIN.equals(user.getUsername()) &&
StringUtils.isNotEmpty(tenantId)
){
String fieldName = webQueryConf.get(MyBatisConstants.FIELD_TENANT);
if(StringUtils.isEmpty(fieldName)){
fieldName = FieldUtil.humpToUnderline(MyBatisConstants.FIELD_TENANT);
}
wrapper.eq(fieldName, tenantId);
}
}
return wrapper;
}
}

@ -15,6 +15,10 @@
*/
package org.opsli.core.persistence.querybuilder.conf;
import org.apache.poi.ss.formula.functions.T;
import org.opsli.common.utils.FieldUtil;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@ -34,29 +38,30 @@ public class WebQueryConf {
/**
* Key
* @param key key
*
* @param fieldFn
* @param value
*/
public void pub(String key, String value){
queryMap.putIfAbsent(key, value);
public void pub(FieldUtil.SFunction<T, ?> fieldFn, String value){
String fileName = FieldUtil.getFileName(fieldFn);
queryMap.putIfAbsent(fileName, value);
}
/**
* Key
* @param key key
*
* @param field
*/
public String get(String key){
return queryMap.get(key);
public String get(String field){
return queryMap.get(field);
}
/**
* Key
* @param key key
* field
* @param field
* @return boolean
*/
public boolean hashKey(String key){
return queryMap.containsKey(key);
public boolean hashKey(String field){
return queryMap.containsKey(field);
}
}

@ -16,34 +16,17 @@
package org.opsli.core.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserApi;
import org.opsli.api.web.system.user.UserOrgRefApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.org.SysOrgModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.api.wrapper.system.user.UserOrgRefWebModel;
import org.opsli.common.enums.DictType;
import org.opsli.common.utils.FieldUtil;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
/**
*
*
@ -51,13 +34,7 @@ import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
* @date 2020-09-19 20:03
*/
@Slf4j
@Order(UTIL_ORDER)
@Component
@Lazy(false)
public class OrgUtil {
/** 前缀 */
public static final String PREFIX_CODE = "org:userId:";
public final class OrgUtil {
/** 用户表 是否分配组织 状态标识 */
public static final String USER_ORG_FIELD = "iz_exist_org";
@ -66,79 +43,9 @@ public class OrgUtil {
/** 未分组 */
public static final String ORG_NULL = "org_null";
/** 用户组织 Api */
private static UserOrgRefApi userOrgRefApi;
/** 增加初始状态开关 防止异常使用 */
private static boolean IS_INIT;
/**
* userId
* @param userId ID
* @return List
*/
public static List<UserOrgRefModel> getOrgListByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_CODE + userId;
List<UserOrgRefModel> orgList;
// 先从缓存里拿
Object obj = CacheUtil.getTimed(cacheKey);
orgList = Convert.toList(UserOrgRefModel.class, obj);
if(CollUtil.isNotEmpty(orgList)){
return orgList;
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey);
if(hasNilFlag){
return ListUtil.empty();
}
try {
// 分布式加锁
if(!DistributedLockUtil.lock(cacheKey)){
// 无法申领分布式锁
log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage());
return ListUtil.empty();
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
obj = CacheUtil.getTimed(cacheKey);
orgList = Convert.toList(UserOrgRefModel.class, obj);
if(CollUtil.isNotEmpty(orgList)){
return orgList;
}
// 查询数据库
ResultVo<List<UserOrgRefModel>> resultVo = userOrgRefApi.findListByUserId(userId);
if(resultVo.isSuccess()){
orgList = resultVo.getData();
// 存入缓存
CacheUtil.put(cacheKey, orgList);
}
}catch (Exception e){
log.error(e.getMessage(), e);
}finally {
// 释放锁
DistributedLockUtil.unlock(cacheKey);
}
if(CollUtil.isEmpty(orgList)){
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(cacheKey);
return ListUtil.empty();
}
return orgList;
}
/**
*
@ -179,8 +86,7 @@ public class OrgUtil {
if(!ORG_NULL.equals(orgIdGroup)){
wra.and(wraConfine -> {
// 增加自身 组织限制
UserModel currUser = UserUtil.getUser();
List<UserOrgRefModel> orgListByUserId = OrgUtil.getOrgListByUserId(currUser.getId());
List<UserOrgRefModel> orgListByUserId = UserUtil.getOrgByCurrUser();
if(CollUtil.isEmpty(orgListByUserId)){
// 如果为空 则默认 不查询
wraConfine.eq("1", "2");
@ -240,60 +146,8 @@ public class OrgUtil {
});
}
// ============== 刷新缓存 ==============
/**
* -
* @param userId ID
* @return boolean
*/
public static boolean refreshOrg(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(userId)){
return true;
}
// 计数器
int count = 0;
UserOrgRefWebModel orgRefModel = CacheUtil.getTimed(UserOrgRefWebModel.class, PREFIX_CODE + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_CODE + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_CODE + userId);
if(tmp){
count--;
}
}
if(orgRefModel != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_CODE + userId);
if(tmp){
count--;
}
}
return count == 0;
}
// =====================================
// ===========
/**
*
*/
@Autowired
public void init(UserOrgRefApi userOrgRefApi) {
OrgUtil.userOrgRefApi = userOrgRefApi;
IS_INIT = true;
}
private OrgUtil() {}
}

@ -254,12 +254,15 @@ public class UserTokenUtil {
// 如果缓存中 无该用户任何Token信息 则删除用户缓存
Long size = redisPlugin.sSize(
CacheUtil.getPrefixName() + TICKET_PREFIX + user.getUsername());
if(size == null || size == 0L){
if(size == null || size == 0L) {
// 删除相关信息
UserUtil.refreshUser(user);
UserUtil.refreshUserRoles(user.getId());
UserUtil.refreshUserAllPerms(user.getId());
UserUtil.refreshUserMenus(user.getId());
UserUtil.refreshUserOrgs(user.getId());
UserUtil.refreshUserDefRole(userId);
UserUtil.refreshUserDefOrg(userId);
}
}

@ -23,11 +23,15 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserApi;
import org.opsli.api.web.system.user.UserOrgRefApi;
import org.opsli.api.web.system.user.UserRoleRefApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.api.wrapper.system.user.UserOrgRefWebModel;
import org.opsli.common.exception.TokenException;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
@ -36,6 +40,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@ -55,6 +60,9 @@ public class UserUtil {
/** 前缀 */
public static final String PREFIX_ID = "userId:";
public static final String PREFIX_ID_ROLES = "userId:roles:";
public static final String PREFIX_ID_DEF_ROLE = "userId:def_role:";
public static final String PREFIX_ID_ORGS = "userId:orgs";
public static final String PREFIX_ID_DEF_ORG = "userId:def_org:";
public static final String PREFIX_ID_PERMISSIONS = "userId:permissions:";
public static final String PREFIX_ID_MENUS = "userId:menus:";
public static final String PREFIX_USERNAME = "username:";
@ -65,6 +73,12 @@ public class UserUtil {
/** 用户Service */
private static UserApi userApi;
/** 用户角色 Api */
private static UserRoleRefApi userRoleRefApi;
/** 用户组织 Api */
private static UserOrgRefApi userOrgRefApi;
/** 超级管理员 */
public static String SUPER_ADMIN;
@ -274,7 +288,7 @@ public class UserUtil {
}
// 查询数据库
ResultVo<List<String>> resultVo = userApi.getRolesByUserId(userId);
ResultVo<List<String>> resultVo = userRoleRefApi.getRolesByUserId(userId);
if(resultVo.isSuccess()){
roles = resultVo.getData();
// 存入缓存
@ -342,7 +356,7 @@ public class UserUtil {
}
// 查询数据库
ResultVo<List<String>> resultVo = userApi.getAllPerms(userId);
ResultVo<List<String>> resultVo = userRoleRefApi.getAllPerms(userId);
if(resultVo.isSuccess()){
permissions = resultVo.getData();
// 存入缓存
@ -364,6 +378,87 @@ public class UserUtil {
return permissions;
}
/**
* userId
* @param userId ID
* @return List
*/
public static List<UserOrgRefModel> getOrgListByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID_ORGS + userId;
List<UserOrgRefModel> orgList;
// 先从缓存里拿
Object obj = CacheUtil.getTimed(cacheKey);
orgList = Convert.toList(UserOrgRefModel.class, obj);
if(CollUtil.isNotEmpty(orgList)){
return orgList;
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey);
if(hasNilFlag){
return ListUtil.empty();
}
try {
// 分布式加锁
if(!DistributedLockUtil.lock(cacheKey)){
// 无法申领分布式锁
log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage());
return ListUtil.empty();
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
obj = CacheUtil.getTimed(cacheKey);
orgList = Convert.toList(UserOrgRefModel.class, obj);
if(CollUtil.isNotEmpty(orgList)){
return orgList;
}
// 查询数据库
ResultVo<List<UserOrgRefModel>> resultVo = userOrgRefApi.findListByUserId(userId);
if(resultVo.isSuccess()){
orgList = resultVo.getData();
// 存入缓存
CacheUtil.put(cacheKey, orgList);
}
}catch (Exception e){
log.error(e.getMessage(), e);
}finally {
// 释放锁
DistributedLockUtil.unlock(cacheKey);
}
if(CollUtil.isEmpty(orgList)){
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(cacheKey);
return ListUtil.empty();
}
return orgList;
}
/**
*
* @return List
*/
public static List<UserOrgRefModel> getOrgByCurrUser(){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
UserModel user = UserUtil.getUser();
return getOrgListByUserId(user.getId());
}
/**
* userId
* @param userId ID
@ -410,7 +505,7 @@ public class UserUtil {
}
// 查询数据库
ResultVo<List<MenuModel>> resultVo = userApi.getMenuListByUserId(userId);
ResultVo<List<MenuModel>> resultVo = userRoleRefApi.getMenuListByUserId(userId);
if(resultVo.isSuccess()){
menus = resultVo.getData();
// 存入缓存
@ -432,6 +527,136 @@ public class UserUtil {
return menus;
}
/**
* userId
* @param userId ID
* @return List
*/
public static RoleModel getUserDefRoleByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID_DEF_ROLE + userId;
// 先从缓存里拿
RoleModel roleModel = CacheUtil.getTimed(RoleModel.class, cacheKey);
if (roleModel != null){
return roleModel;
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey);
if(hasNilFlag){
return null;
}
try {
// 分布式加锁
if(!DistributedLockUtil.lock(cacheKey)){
// 无法申领分布式锁
log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage());
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
roleModel = CacheUtil.getTimed(RoleModel.class, cacheKey);
if (roleModel != null){
return roleModel;
}
// 查询数据库
ResultVo<RoleModel> resultVo = userRoleRefApi.getDefRoleByUserId(userId);
if(resultVo.isSuccess()){
roleModel = resultVo.getData();
// 存入缓存
CacheUtil.put(cacheKey, roleModel);
}
}catch (Exception e){
log.error(e.getMessage(),e);
}finally {
// 释放锁
DistributedLockUtil.unlock(cacheKey);
}
if(roleModel == null){
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(cacheKey);
return null;
}
return roleModel;
}
/**
* userId
* @param userId ID
* @return List
*/
public static UserOrgRefModel getUserDefOrgByUserId(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
// 缓存Key
String cacheKey = PREFIX_ID_DEF_ORG + userId;
// 先从缓存里拿
UserOrgRefModel orgModel = CacheUtil.getTimed(UserOrgRefModel.class, cacheKey);
if (orgModel != null){
return orgModel;
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(cacheKey);
if(hasNilFlag){
return null;
}
try {
// 分布式加锁
if(!DistributedLockUtil.lock(cacheKey)){
// 无法申领分布式锁
log.error(CoreMsg.REDIS_EXCEPTION_LOCK.getMessage());
return null;
}
// 如果获得锁 则 再次检查缓存里有没有, 如果有则直接退出, 没有的话才发起数据库请求
orgModel = CacheUtil.getTimed(UserOrgRefModel.class, cacheKey);
if (orgModel != null){
return orgModel;
}
// 查询数据库
ResultVo<UserOrgRefModel> resultVo = userOrgRefApi.getDefOrgByUserId(userId);
if(resultVo.isSuccess()){
orgModel = resultVo.getData();
// 存入缓存
CacheUtil.put(cacheKey, orgModel);
}
}catch (Exception e){
log.error(e.getMessage(),e);
}finally {
// 释放锁
DistributedLockUtil.unlock(cacheKey);
}
if(orgModel == null){
// 设置空变量 用于防止穿透判断
CacheUtil.putNilFlag(cacheKey);
return null;
}
return orgModel;
}
// ============== 刷新缓存 ==============
/**
@ -536,6 +761,50 @@ public class UserUtil {
return count == 0;
}
/**
* -
* @param userId ID
* @return boolean
*/
public static boolean refreshUserDefRole(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(userId)){
return true;
}
// 计数器
int count = 0;
RoleModel roleModel = CacheUtil.getTimed(RoleModel.class, PREFIX_ID_DEF_ROLE + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_DEF_ROLE + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID_DEF_ROLE + userId);
if(tmp){
count--;
}
}
if(roleModel != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_ID_DEF_ROLE + userId);
if(tmp){
count--;
}
}
return count == 0;
}
/**
* -
* @param userId ID
@ -576,6 +845,90 @@ public class UserUtil {
return count == 0;
}
/**
* -
* @param userId ID
* @return boolean
*/
public static boolean refreshUserOrgs(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(userId)){
return true;
}
// 计数器
int count = 0;
UserOrgRefWebModel orgRefModel = CacheUtil.getTimed(UserOrgRefWebModel.class, PREFIX_ID_ORGS + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_ORGS + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID_ORGS + userId);
if(tmp){
count--;
}
}
if(orgRefModel != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_ID_ORGS + userId);
if(tmp){
count--;
}
}
return count == 0;
}
/**
* -
* @param userId ID
* @return boolean
*/
public static boolean refreshUserDefOrg(String userId){
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(userId)){
return true;
}
// 计数器
int count = 0;
UserOrgRefModel orgModel = CacheUtil.getTimed(UserOrgRefModel.class, PREFIX_ID_DEF_ORG + userId);
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_DEF_ORG + userId);
// 只要不为空 则执行刷新
if (hasNilFlag){
count++;
// 清除空拦截
boolean tmp = CacheUtil.delNilFlag(PREFIX_ID_DEF_ORG + userId);
if(tmp){
count--;
}
}
if(orgModel != null){
count++;
// 先删除
boolean tmp = CacheUtil.del(PREFIX_ID_DEF_ORG + userId);
if(tmp){
count--;
}
}
return count == 0;
}
/**
* -
* @param userId ID
@ -692,7 +1045,10 @@ public class UserUtil {
*
*/
@Autowired
public void init(GlobalProperties globalProperties, UserApi userApi){
public void init(GlobalProperties globalProperties,
UserApi userApi,
UserRoleRefApi userRoleRefApi,
UserOrgRefApi userOrgRefApi){
if(globalProperties != null && globalProperties.getAuth() != null
&& globalProperties.getAuth().getToken() != null
){
@ -702,6 +1058,10 @@ public class UserUtil {
UserUtil.userApi = userApi;
UserUtil.userRoleRefApi = userRoleRefApi;
UserUtil.userOrgRefApi = userOrgRefApi;
IS_INIT = true;
}

@ -139,6 +139,13 @@ public class LoginRestController {
// 用户暂无角色菜单,请设置后登录
throw new TokenException(TokenMsg.EXCEPTION_USER_MENU_NOT_NULL);
}
// 检测用户是否有角色权限
List<String> userAllPermsList = UserUtil.getUserAllPermsByUserId(user.getId());
if(CollUtil.isEmpty(userAllPermsList)){
// 用户暂无角色菜单,请设置后登录
throw new TokenException(TokenMsg.EXCEPTION_USER_PERMS_NOT_NULL);
}
}
// 失败次数超过 验证次数阈值 开启验证码验证

@ -374,7 +374,7 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
List<String> userIdList = iUserRoleRefService.getUserIdListByMenuId(menuModel.getId());
if(CollUtil.isNotEmpty(userIdList)){
for (String userId : userIdList) {
cacheCount += 3;
cacheCount += 6;
// 清空当期用户缓存角色、权限、菜单
cacheRet = UserUtil.refreshUserRoles(userId);
if(cacheRet){
@ -388,6 +388,18 @@ public class MenuServiceImpl extends CrudServiceImpl<MenuMapper, SysMenu, MenuMo
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserOrgs(userId);
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserDefRole(userId);
if(cacheRet){
cacheCount--;
}
cacheRet = UserUtil.refreshUserDefOrg(userId);
if(cacheRet){
cacheCount--;
}
}
}

@ -53,6 +53,7 @@ import org.opsli.core.utils.TreeBuildUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.menu.service.IMenuService;
import org.opsli.modulars.system.user.service.IUserRoleRefService;
import org.opsli.modulars.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@ -77,7 +78,7 @@ public class MenuRestController extends BaseRestController<SysMenu, MenuModel, I
implements MenuApi {
@Autowired
private IUserService iUserService;
private IUserRoleRefService iUserRoleRefService;
/** 排序字段 */
private static final String SORT_FIELD = "order";
@ -104,7 +105,7 @@ public class MenuRestController extends BaseRestController<SysMenu, MenuModel, I
UserModel user = UserUtil.getUser();
// 获得当前用户菜单
List<MenuModel> menuModelList = iUserService.getMenuAllListByUserId(user.getId());
List<MenuModel> menuModelList = iUserRoleRefService.getMenuAllListByUserId(user.getId());
// 这里有坑 如果 为 菜单数据 且 组件(Component)地址为空 不会跳转到主页 也不报错
// 修复菜单问题导致无法跳转主页

@ -92,7 +92,7 @@ public class SysOrgRestController extends BaseRestController<SysOrg, SysOrgModel
QueryBuilder<SysOrg> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysOrg> wrapper = queryBuilder.build();
List<UserOrgRefModel> orgListByUserId = OrgUtil.getOrgByCurrUser();
List<UserOrgRefModel> orgListByUserId = UserUtil.getOrgByCurrUser();
if(!CollUtil.isEmpty(orgListByUserId)){
List<String> parentIdList = Lists.newArrayListWithCapacity(orgListByUserId.size());

@ -39,9 +39,12 @@ public class SysRole extends BaseEntity {
/** 角色名称 */
private String roleName;
/** 是否内置数据 0否 1是*/
/** 是否内置数据 0否 1是 */
private String izLock;
/** 授权数据范围 */
private String dataScope;
/** 备注 */
@TableField(updateStrategy = FieldStrategy.IGNORED)
private String remark;

@ -113,7 +113,7 @@ public class RoleMenuRefServiceImpl extends ServiceImpl<RoleMenuRefMapper,SysRol
if(CollUtil.isNotEmpty(userIdList)){
int cacheCount = 0;
for (String userId : userIdList) {
cacheCount += 3;
cacheCount += 6;
boolean tmp;
// 清空当期用户缓存角色、权限、菜单
tmp = UserUtil.refreshUserRoles(userId);
@ -128,6 +128,18 @@ public class RoleMenuRefServiceImpl extends ServiceImpl<RoleMenuRefMapper,SysRol
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserOrgs(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserDefRole(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserDefOrg(userId);
if(tmp){
cacheCount--;
}
}
// 判断删除状态
if(cacheCount != 0){

@ -20,13 +20,14 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.FieldUtil;
import org.opsli.core.base.service.impl.CrudServiceImpl;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.chain.QueryOrgHandler;
import org.opsli.core.persistence.querybuilder.chain.QueryDataPermsHandler;
import org.opsli.core.persistence.querybuilder.chain.QueryTenantHandler;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
@ -149,7 +150,7 @@ public class RoleServiceImpl extends CrudServiceImpl<RoleMapper, SysRole, RoleMo
if(!UserUtil.isHasUpdateTenantPerms(UserUtil.getUser())){
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, queryWrapper);
}
@ -165,7 +166,7 @@ public class RoleServiceImpl extends CrudServiceImpl<RoleMapper, SysRole, RoleMo
if(!UserUtil.isHasUpdateTenantPerms(UserUtil.getUser())){
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, queryWrapper);
}
@ -192,11 +193,23 @@ public class RoleServiceImpl extends CrudServiceImpl<RoleMapper, SysRole, RoleMo
wrapper.notIn(MyBatisConstants.FIELD_ID, model.getId());
}
// 租户检测
// 数据处理责任链
wrapper = new QueryTenantHandler(
new QueryOrgHandler()
).handler(entityClazz, wrapper);
UserModel currUser = UserUtil.getUser();
// 如果是超级管理员 需要特殊处理
if(StringUtils.isNotBlank(model.getTenantId())){
wrapper.eq(FieldUtil.humpToUnderline(MyBatisConstants.FIELD_TENANT), model.getTenantId());
}else {
// 如果是超级管理员 需要特殊处理
if(UserUtil.SUPER_ADMIN.equals(currUser.getUsername())){
// 角色分页 增加租户权限
wrapper.isNull(FieldUtil.humpToUnderline(MyBatisConstants.FIELD_TENANT));
}else {
// 租户检测
// 数据处理责任链
wrapper = new QueryTenantHandler(
new QueryDataPermsHandler()
).handler(entityClazz, wrapper);
}
}
return super.count(wrapper) == 0;
}
@ -222,11 +235,25 @@ public class RoleServiceImpl extends CrudServiceImpl<RoleMapper, SysRole, RoleMo
wrapper.notIn(MyBatisConstants.FIELD_ID, model.getId());
}
// 租户检测
// 数据处理责任链
wrapper = new QueryTenantHandler(
new QueryOrgHandler()
).handler(entityClazz, wrapper);
UserModel currUser = UserUtil.getUser();
// 如果是超级管理员 需要特殊处理
if(StringUtils.isNotBlank(model.getTenantId())){
wrapper.eq(FieldUtil.humpToUnderline(MyBatisConstants.FIELD_TENANT), model.getTenantId());
}else {
// 如果是超级管理员 需要特殊处理
if(UserUtil.SUPER_ADMIN.equals(currUser.getUsername())){
// 角色分页 增加租户权限
wrapper.isNull(FieldUtil.humpToUnderline(MyBatisConstants.FIELD_TENANT));
}else {
// 租户检测
// 数据处理责任链
wrapper = new QueryTenantHandler(
new QueryDataPermsHandler()
).handler(entityClazz, wrapper);
}
}
return super.count(wrapper) == 0;
}

@ -70,7 +70,7 @@ public class RoleMenuRefRestController implements RoleMenuRefApi {
* @param model roleId Id
* @return ResultVo
*/
@RequiresPermissions("system_role_setPerms")
@RequiresPermissions("system_role_setMenuPerms")
@Override
public ResultVo<?> getPerms(RoleMenuRefModel model) {
if(model == null){
@ -99,11 +99,11 @@ public class RoleMenuRefRestController implements RoleMenuRefApi {
}
/**
*
*
* @param model
* @return ResultVo
*/
@RequiresPermissions("system_role_setPerms")
@RequiresPermissions("system_role_setMenuPerms")
@EnableLog
@Override
public ResultVo<?> setPerms(RoleMenuRefModel model) {

@ -32,6 +32,7 @@ import org.opsli.common.annotation.EnableLog;
import org.opsli.common.annotation.RequiresPermissionsCus;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.FieldUtil;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
@ -93,8 +94,17 @@ public class RoleRestController extends BaseRestController<SysRole, RoleModel, I
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<SysRole> queryBuilder = new WebQueryBuilder<>(entityClazz, request.getParameterMap());
QueryWrapper<SysRole> wrapper = queryBuilder.build();
UserModel user = UserUtil.getUser();
// 如果是超级管理员 则查询为空的角色
if(StringUtils.equals(UserUtil.SUPER_ADMIN, user.getUsername())){
// 角色分页 增加租户权限
wrapper.isNull(FieldUtil.humpToUnderline(MyBatisConstants.FIELD_TENANT));
}
Page<SysRole, RoleModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build());
page.setQueryWrapper(wrapper);
page = IService.findPage(page);
return ResultVo.success(page.getPageData());

@ -49,5 +49,7 @@ public class SysUserRoleRef implements Serializable {
/** 角色ID */
private String roleId;
/** 是否默认 */
private String izDef;
}

@ -37,41 +37,6 @@ import java.util.List;
public interface UserMapper extends BaseMapper<SysUser> {
/**
* ID
* @param userId ID
* @return List
*/
List<String> getRoleCodeList(String userId);
/**
* ID Id
* @param userId ID
* @return List
*/
List<String> getRoleIdList(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<String> queryAllPerms(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<SysMenu> findMenuListByUserId(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<SysMenu> findMenuAllListByUserId(String userId);
/**
*
* @param userPassword

@ -17,6 +17,7 @@ package org.opsli.modulars.system.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.user.entity.SysUserRoleRef;
import java.util.List;
@ -30,6 +31,41 @@ import java.util.List;
@Mapper
public interface UserRoleRefMapper extends BaseMapper<SysUserRoleRef> {
/**
* ID
* @param userId ID
* @return List
*/
List<String> getRoleCodeList(String userId);
/**
* ID Id
* @param userId ID
* @return List
*/
List<String> getRoleIdList(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<String> queryAllPerms(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<SysMenu> findMenuListByUserId(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<SysMenu> findMenuAllListByUserId(String userId);
/**
* ID Id
* @param roleId ID

@ -45,99 +45,6 @@
${ew.customSqlSegment}
</select>
<select id="queryAllPerms" resultType="String">
select
a.permissions
from
sys_menu a,
sys_role_menu_ref b,
sys_user_role_ref c
where
a.id = b.menu_id
and a.hidden = '0'
and a.deleted = '0'
and b.role_id = c.role_id
<!-- 2 表示按钮 -->
and a.type = '2'
and c.user_id = #{userId}
</select>
<select id="getRoleIdList" resultType="String">
select
a.role_id
from
sys_user_role_ref a
where
a.user_id = #{userId}
</select>
<select id="getRoleCodeList" resultType="String">
select
b.role_code
from
sys_user_role_ref a,
sys_role b
where
a.role_id = b.id
and a.user_id = #{userId}
</select>
<!-- 根据用户id查询菜单列表-->
<select id="findMenuListByUserId" resultType="SysMenu">
select
c.id,
c.parent_Id,
c.permissions,
c.menu_name,
c.redirect,
c.type,
c.icon,
c.url,
c.component,
c.always_show,
c.sort_no
from
sys_user_role_ref a,
sys_role_menu_ref b,
sys_menu c
where
a.role_Id = b.role_Id
and b.menu_id = c.id
and c.parent_id != -1
and a.user_id = #{userId}
<!-- 1 表示内部链接 3 表示外部链接 -->
and c.type in ( '1', '3' )
and c.deleted = '0'
and c.hidden = '0'
</select>
<!-- 根据用户id查询菜单列表-->
<select id="findMenuAllListByUserId" resultType="SysMenu">
select
c.id,
c.parent_Id,
c.permissions,
c.menu_name,
c.redirect,
c.type,
c.icon,
c.url,
c.component,
c.always_show,
c.sort_no
from
sys_user_role_ref a,
sys_role_menu_ref b,
sys_menu c
where
a.role_Id = b.role_Id
and b.menu_id = c.id
and c.parent_id != -1
and a.user_id = #{userId}
and c.deleted = '0'
and c.hidden = '0'
</select>
<update id="updatePassword" parameterType="org.opsli.api.wrapper.system.user.UserPassword">
update sys_user
set

@ -2,6 +2,99 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.opsli.modulars.system.user.mapper.UserRoleRefMapper">
<select id="queryAllPerms" resultType="String">
select
a.permissions
from
sys_menu a,
sys_role_menu_ref b,
sys_user_role_ref c
where
a.id = b.menu_id
and a.hidden = '0'
and a.deleted = '0'
and b.role_id = c.role_id
<!-- 2 表示按钮 -->
and a.type = '2'
and c.user_id = #{userId}
</select>
<select id="getRoleIdList" resultType="String">
select
a.role_id
from
sys_user_role_ref a
where
a.user_id = #{userId}
</select>
<select id="getRoleCodeList" resultType="String">
select
b.role_code
from
sys_user_role_ref a,
sys_role b
where
a.role_id = b.id
and a.user_id = #{userId}
</select>
<!-- 根据用户id查询菜单列表-->
<select id="findMenuListByUserId" resultType="SysMenu">
select
c.id,
c.parent_Id,
c.permissions,
c.menu_name,
c.redirect,
c.type,
c.icon,
c.url,
c.component,
c.always_show,
c.sort_no
from
sys_user_role_ref a,
sys_role_menu_ref b,
sys_menu c
where
a.role_Id = b.role_Id
and b.menu_id = c.id
and c.parent_id != -1
and a.user_id = #{userId}
<!-- 1 表示内部链接 3 表示外部链接 -->
and c.type in ( '1', '3' )
and c.deleted = '0'
and c.hidden = '0'
</select>
<!-- 根据用户id查询菜单列表-->
<select id="findMenuAllListByUserId" resultType="SysMenu">
select
c.id,
c.parent_Id,
c.permissions,
c.menu_name,
c.redirect,
c.type,
c.icon,
c.url,
c.component,
c.always_show,
c.sort_no
from
sys_user_role_ref a,
sys_role_menu_ref b,
sys_menu c
where
a.role_Id = b.role_Id
and b.menu_id = c.id
and c.parent_id != -1
and a.user_id = #{userId}
and c.deleted = '0'
and c.hidden = '0'
</select>
<select id="getUserIdListByRoleId" resultType="String">
select
a.user_id

@ -43,4 +43,18 @@ public interface IUserOrgRefService {
*/
List<UserOrgRefModel> findListByUserId(String userId);
/**
* ID ID
* @param userId ID
* @return List
*/
String getDefOrgId(String userId);
/**
* ID
* @param userId ID
* @return List
*/
UserOrgRefModel getDefOrgByUserId(String userId);
}

@ -15,6 +15,11 @@
*/
package org.opsli.modulars.system.user.service;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserRoleRefModel;
import org.opsli.modulars.system.menu.entity.SysMenu;
import java.util.List;
/**
@ -25,6 +30,41 @@ import java.util.List;
*/
public interface IUserRoleRefService {
/**
* ID
* @param userId ID
* @return List
*/
List<String> getRoleCodeList(String userId);
/**
* ID Id
* @param userId ID
* @return List
*/
List<String> getRoleIdList(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<String> getAllPerms(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<MenuModel> getMenuListByUserId(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<MenuModel> getMenuAllListByUserId(String userId);
/**
* ID Id
* @param roleId ID
@ -40,11 +80,24 @@ public interface IUserRoleRefService {
List<String> getUserIdListByMenuId(String roleId);
/**
*
* ID ID
* @param userId ID
* @return List
*/
String getDefRoleId(String userId);
/**
* ID
* @param userId ID
* @param roleIds ID
* @return List
*/
RoleModel getDefRoleByUserId(String userId);
/**
*
* @param model
* @return boolean
*/
boolean setRoles(String userId,String[] roleIds);
boolean setRoles(UserRoleRefModel model);
}

@ -15,17 +15,14 @@
*/
package org.opsli.modulars.system.user.service;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserWebModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserPassword;
import org.opsli.api.wrapper.system.user.UserWebModel;
import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import org.opsli.core.persistence.Page;
import org.opsli.modulars.system.user.entity.SysUser;
import org.opsli.modulars.system.user.entity.SysUserWeb;
import java.util.List;
/**
* Service
@ -42,41 +39,6 @@ public interface IUserService extends CrudServiceInterface<SysUser, UserModel> {
*/
UserModel queryByUserName(String username);
/**
* ID
* @param userId ID
* @return List
*/
List<String> getRoleCodeList(String userId);
/**
* ID Id
* @param userId ID
* @return List
*/
List<String> getRoleIdList(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<String> getAllPerms(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<MenuModel> getMenuListByUserId(String userId);
/**
* ID
* @param userId ID
* @return List
*/
List<MenuModel> getMenuAllListByUserId(String userId);
/**
*

@ -23,7 +23,6 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.api.wrapper.system.org.SysOrgModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
@ -35,12 +34,8 @@ import org.opsli.common.utils.FieldUtil;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.utils.OrgUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.org.entity.SysOrg;
import org.opsli.modulars.system.org.service.ISysOrgService;
import org.opsli.modulars.system.user.entity.SysUser;
@ -194,6 +189,28 @@ public class UserOrgRefServiceImpl extends ServiceImpl<UserOrgRefMapper, SysUser
iUserService.update(updateUserWrapper);
}
@Override
public String getDefOrgId(String userId) {
String orgId = null;
QueryWrapper<SysUserOrgRef> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
wrapper.eq("iz_def", DictType.NO_YES_YES.getValue());
SysUserOrgRef sysUserOrgRef = this.getOne(wrapper);
if(sysUserOrgRef != null){
orgId = sysUserOrgRef.getOrgId();
}
return orgId;
}
@Override
public UserOrgRefModel getDefOrgByUserId(String userId) {
QueryWrapper<SysUserOrgRef> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
wrapper.eq("iz_def", DictType.NO_YES_YES.getValue());
return WrapperUtil.transformInstance(
this.getOne(wrapper), UserOrgRefModel.class);
}
/**
* Org
* @param userId ID
@ -225,8 +242,12 @@ public class UserOrgRefServiceImpl extends ServiceImpl<UserOrgRefMapper, SysUser
if(CollUtil.isNotEmpty(userIds)){
int cacheCount = 0;
for (String userId : userIds) {
cacheCount++;
boolean tmp = OrgUtil.refreshOrg(userId);
cacheCount+=2;
boolean tmp = UserUtil.refreshUserOrgs(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserDefOrg(userId);
if(tmp){
cacheCount--;
}

@ -21,18 +21,32 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserRoleRefModel;
import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.menu.entity.SysMenu;
import org.opsli.modulars.system.menu.service.IMenuService;
import org.opsli.modulars.system.role.service.IRoleService;
import org.opsli.modulars.system.user.entity.SysUserRoleRef;
import org.opsli.modulars.system.user.mapper.UserRoleRefMapper;
import org.opsli.modulars.system.user.service.IUserRoleRefService;
import org.opsli.modulars.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Comparator;
import java.util.List;
/**
@ -47,6 +61,118 @@ public class UserRoleRefServiceImpl extends ServiceImpl<UserRoleRefMapper, SysUs
@Autowired(required = false)
private UserRoleRefMapper mapper;
@Autowired
private IRoleService iRoleService;
@Autowired
private IUserService iUserService;
@Autowired
private IMenuService iMenuService;
@Override
public List<String> getRoleCodeList(String userId) {
List<String> roles = mapper.getRoleCodeList(userId);
// 去重
return ListDistinctUtil.distinct(roles);
}
@Override
public List<String> getRoleIdList(String userId) {
List<String> roles = mapper.getRoleIdList(userId);
// 去重
return ListDistinctUtil.distinct(roles);
}
@Override
public List<String> getAllPerms(String userId) {
UserModel userModel = iUserService.get(userId);
if(userModel == null){
return ListUtil.empty();
}
List<String> perms;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
perms = Lists.newArrayList();
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.eq("type", '2');
queryWrapper.eq("hidden", '0');
List<SysMenu> menuList = iMenuService.findList(queryWrapper);
for (SysMenu sysMenu : menuList) {
perms.add(sysMenu.getPermissions());
}
}else{
perms = mapper.queryAllPerms(userId);
}
// 去重
return ListDistinctUtil.distinct(perms);
}
@Override
public List<MenuModel> getMenuListByUserId(String userId) {
UserModel userModel = iUserService.get(userId);
if(userModel == null){
return ListUtil.empty();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.in("type", '1', '3');
queryWrapper.eq("hidden", '0');
menuList = iMenuService.findList(queryWrapper);
}else{
menuList = mapper.findMenuListByUserId(userId);
}
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
List<SysMenu> distinctList = ListDistinctUtil.distinct(
menuList, Comparator.comparing(ApiWrapper::getId));
return WrapperUtil.transformInstance(distinctList, MenuModel.class);
}
@Override
public List<MenuModel> getMenuAllListByUserId(String userId) {
UserModel userModel = iUserService.get(userId);
if(userModel == null){
return ListUtil.empty();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.eq("hidden", '0');
menuList = iMenuService.findList(queryWrapper);
}else{
menuList = mapper.findMenuAllListByUserId(userId);
}
if(CollUtil.isEmpty(menuList)){
return ListUtil.empty();
}
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
List<SysMenu> distinctList = ListDistinctUtil.distinct(
menuList, Comparator.comparing(ApiWrapper::getId));
return WrapperUtil.transformInstance(distinctList, MenuModel.class);
}
@Override
public List<String> getUserIdListByRoleId(String roleId) {
List<String> users = mapper.getUserIdListByRoleId(roleId);
@ -69,31 +195,58 @@ public class UserRoleRefServiceImpl extends ServiceImpl<UserRoleRefMapper, SysUs
return ListDistinctUtil.distinct(users);
}
@Override
public String getDefRoleId(String userId) {
String roleId = null;
QueryWrapper<SysUserRoleRef> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
wrapper.eq("iz_def", DictType.NO_YES_YES.getValue());
SysUserRoleRef sysUserRoleRef = this.getOne(wrapper);
if(sysUserRoleRef != null){
roleId = sysUserRoleRef.getRoleId();
}
return roleId;
}
@Override
public RoleModel getDefRoleByUserId(String userId) {
String defRoleId = this.getDefRoleId(userId);
return iRoleService.get(defRoleId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean setRoles(String userId, String[] roleIds) {
if(StringUtils.isEmpty(userId)){
public boolean setRoles(UserRoleRefModel model) {
if(StringUtils.isEmpty(model.getUserId())){
throw new ServiceException(SystemMsg.EXCEPTION_USER_ID_NOT_NULL);
}
// 删除已有权限
String userIdField = "user_id";
QueryWrapper<SysUserRoleRef> wrapper = new QueryWrapper<>();
wrapper.eq(userIdField, userId);
wrapper.eq(userIdField, model.getUserId());
super.remove(wrapper);
if(roleIds != null && roleIds.length > 0){
List<SysUserRoleRef> list = Lists.newArrayListWithCapacity(roleIds.length);
for (String roleId : roleIds) {
if(model.getRoleIds() != null && model.getRoleIds().length > 0){
List<SysUserRoleRef> list = Lists.newArrayListWithCapacity(model.getRoleIds().length);
for (String roleId : model.getRoleIds()) {
SysUserRoleRef entity = new SysUserRoleRef();
entity.setUserId(userId);
entity.setUserId(model.getUserId());
entity.setRoleId(roleId);
// 是否默认角色
if(roleId.equals(model.getDefRoleId())){
entity.setIzDef(DictType.NO_YES_YES.getValue());
}else{
entity.setIzDef(DictType.NO_YES_NO.getValue());
}
list.add(entity);
}
boolean ret = super.saveBatch(list);
if(ret){
// 清除缓存
this.clearCache(userId);
this.clearCache(model.getUserId());
}
}
@ -108,7 +261,7 @@ public class UserRoleRefServiceImpl extends ServiceImpl<UserRoleRefMapper, SysUs
* @param userId ID
*/
private void clearCache(String userId) {
int cacheCount = 3;
int cacheCount = 6;
boolean tmp;
// 清空当期用户缓存角色、权限、菜单
tmp = UserUtil.refreshUserRoles(userId);
@ -123,6 +276,18 @@ public class UserRoleRefServiceImpl extends ServiceImpl<UserRoleRefMapper, SysUs
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserOrgs(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserDefRole(userId);
if(tmp){
cacheCount--;
}
tmp = UserUtil.refreshUserDefOrg(userId);
if(tmp){
cacheCount--;
}
// 判断删除状态
if(cacheCount != 0){

@ -27,8 +27,10 @@ import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserPassword;
import org.opsli.api.wrapper.system.user.UserRoleRefModel;
import org.opsli.api.wrapper.system.user.UserWebModel;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.DictType;
@ -42,7 +44,7 @@ import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.chain.QueryOrgHandler;
import org.opsli.core.persistence.querybuilder.chain.QueryDataPermsHandler;
import org.opsli.core.persistence.querybuilder.chain.QueryTenantHandler;
import org.opsli.core.utils.OptionsUtil;
import org.opsli.core.utils.UserUtil;
@ -164,9 +166,14 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
DictType.NO_YES_NO.getValue());
SysRole sysRole = iRoleService.getOne(roleQueryWrapper);
if(sysRole != null){
UserRoleRefModel userRoleRefModel = UserRoleRefModel.builder()
.userId(insertModel.getId())
.roleIds(Convert.toStrArray(sysRole.getId()))
.defRoleId(sysRole.getId())
.build();
// 设置用户默认角色
iUserRoleRefService.setRoles(insertModel.getId(),
Convert.toStrArray(sysRole.getId()));
iUserRoleRefService.setRoles(userRoleRefModel);
}
}
}
@ -430,109 +437,6 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
return super.transformT2M(user);
}
@Override
public List<String> getRoleCodeList(String userId) {
List<String> roles = mapper.getRoleCodeList(userId);
// 去重
return ListDistinctUtil.distinct(roles);
}
@Override
public List<String> getRoleIdList(String userId) {
List<String> roles = mapper.getRoleIdList(userId);
// 去重
return ListDistinctUtil.distinct(roles);
}
@Override
public List<String> getAllPerms(String userId) {
UserModel userModel = this.get(userId);
if(userModel == null){
return ListUtil.empty();
}
List<String> perms;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
perms = Lists.newArrayList();
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.eq("type", '2');
queryWrapper.eq("hidden", '0');
List<SysMenu> menuList = iMenuService.findList(queryWrapper);
for (SysMenu sysMenu : menuList) {
perms.add(sysMenu.getPermissions());
}
}else{
perms = mapper.queryAllPerms(userId);
}
// 去重
return ListDistinctUtil.distinct(perms);
}
@Override
public List<MenuModel> getMenuListByUserId(String userId) {
UserModel userModel = this.get(userId);
if(userModel == null){
return ListUtil.empty();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.in("type", '1', '3');
queryWrapper.eq("hidden", '0');
menuList = iMenuService.findList(queryWrapper);
}else{
menuList = mapper.findMenuListByUserId(userId);
}
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
List<SysMenu> distinctList = ListDistinctUtil.distinct(
menuList, Comparator.comparing(ApiWrapper::getId));
return WrapperUtil.transformInstance(distinctList, MenuModel.class);
}
@Override
public List<MenuModel> getMenuAllListByUserId(String userId) {
UserModel userModel = this.get(userId);
if(userModel == null){
return ListUtil.empty();
}
List<SysMenu> menuList;
// 判断是否是超级管理员 如果是超级管理员 则默认享有全部权限
if(StringUtils.equals(UserUtil.SUPER_ADMIN, userModel.getUsername())){
QueryBuilder<SysMenu> queryBuilder = new GenQueryBuilder<>();
QueryWrapper<SysMenu> queryWrapper = queryBuilder.build();
queryWrapper.notIn("parent_id", -1);
queryWrapper.eq("hidden", '0');
menuList = iMenuService.findList(queryWrapper);
}else{
menuList = mapper.findMenuAllListByUserId(userId);
}
if(CollUtil.isEmpty(menuList)){
return ListUtil.empty();
}
// 去重处理 这里不放在SQL 是为了保证数据库兼容性
List<SysMenu> distinctList = ListDistinctUtil.distinct(
menuList, Comparator.comparing(ApiWrapper::getId));
return WrapperUtil.transformInstance(distinctList, MenuModel.class);
}
@Override
public Page<SysUser, UserModel> findPage(Page<SysUser, UserModel> page) {
UserModel currUser = UserUtil.getUser();
@ -551,7 +455,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
if(!UserUtil.isHasUpdateTenantPerms(UserUtil.getUser())){
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, queryWrapper);
}
@ -566,7 +470,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
if(!UserUtil.isHasUpdateTenantPerms(UserUtil.getUser())){
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, queryWrapper);
}
return super.list(queryWrapper);
@ -700,7 +604,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
if(!UserUtil.isHasUpdateTenantPerms(UserUtil.getUser())){
// 数据处理责任链
queryWrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(SysUserWeb.class, queryWrapper);
}
@ -765,7 +669,7 @@ public class UserServiceImpl extends CrudServiceImpl<UserMapper, SysUser, UserMo
// 租户检测
// 数据处理责任链
wrapper = new QueryTenantHandler(
new QueryOrgHandler()
new QueryDataPermsHandler()
).handler(entityClazz, wrapper);
return super.count(wrapper) == 0;

@ -15,22 +15,33 @@
*/
package org.opsli.modulars.system.user.web;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserOrgRefApi;
import org.opsli.api.wrapper.system.org.SysOrgModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserOrgRefModel;
import org.opsli.api.wrapper.system.user.UserOrgRefWebModel;
import org.opsli.api.wrapper.system.user.UserWebModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.user.entity.SysUserOrgRef;
import org.opsli.modulars.system.user.entity.SysUserWeb;
import org.opsli.modulars.system.user.service.IUserOrgRefService;
import org.opsli.modulars.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@ -53,6 +64,9 @@ public class UserOrgRefRestController implements UserOrgRefApi {
@Autowired
private IUserOrgRefService iUserOrgRefService;
@Autowired
private IUserService iUserService;
@Override
public ResultVo<List<UserOrgRefModel>> findListByUserId(String userId) {
List<UserOrgRefModel> listByUserId = iUserOrgRefService.findListByUserId(userId);
@ -78,6 +92,87 @@ public class UserOrgRefRestController implements UserOrgRefApi {
return ResultVo.success();
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<UserOrgRefModel> getDefOrgByUserId(String userId) {
UserOrgRefModel userOrgRefModel = iUserOrgRefService.getDefOrgByUserId(userId);
return ResultVo.success(userOrgRefModel);
}
/**
*
* @param userId ID
* @return ResultVo
*/
@ApiOperation(value = "用户组织机构", notes = "用户组织机构")
@Override
public ResultVo<UserOrgRefWebModel> getOrgInfoByUserId(String userId) {
UserOrgRefWebModel org = null;
// 不写SQL了 直接分页 第一页 取第一条
QueryBuilder<SysUserWeb> queryBuilder = new GenQueryBuilder<>();
Page<SysUserWeb, UserWebModel> page = new Page<>(1, 1);
QueryWrapper<SysUserWeb> queryWrapper = queryBuilder.build();
queryWrapper.eq(
"a.id",
userId
);
page.setQueryWrapper(queryWrapper);
page = iUserService.findPageByCus(page);
List<UserWebModel> list = page.getList();
if(CollUtil.isNotEmpty(list)){
UserWebModel userWebModel = list.get(0);
if(userWebModel != null){
// org = userAndOrgModel.getOrg();
// if(org != null){
//
// org.setUserId(userId);
// List<String> orgIds = Lists.newArrayListWithCapacity(3);
// orgIds.add(org.getCompanyId());
// orgIds.add(org.getDepartmentId());
// orgIds.add(org.getPostId());
// QueryWrapper<SysOrg> orgQueryWrapper = new QueryWrapper<>();
// orgQueryWrapper.in(
// FieldUtil.humpToUnderline(MyBatisConstants.FIELD_ID),
// orgIds);
// List<SysOrg> orgList = iSysOrgService.findList(orgQueryWrapper);
// if(CollUtil.isNotEmpty(orgList)){
// Map<String, SysOrg> tmp = Maps.newHashMap();
// for (SysOrg sysOrg : orgList) {
// tmp.put(sysOrg.getId(), sysOrg);
// }
//
// // 设置 名称
// SysOrg company = tmp.get(org.getCompanyId());
// if(company != null){
// org.setCompanyName(company.getOrgName());
// }
//
// SysOrg department = tmp.get(org.getDepartmentId());
// if(department != null){
// org.setDepartmentName(department.getOrgName());
// }
//
// SysOrg post = tmp.get(org.getPostId());
// if(post != null){
// org.setPostName(post.getOrgName());
// }
// }
// }
}
}
return ResultVo.success(org);
}
/**
*

@ -21,7 +21,6 @@ import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@ -31,16 +30,13 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.options.OptionsModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.*;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.annotation.EnableLog;
import org.opsli.common.annotation.RequiresPermissionsCus;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.common.enums.DictType;
import org.opsli.common.exception.ServiceException;
import org.opsli.common.exception.TokenException;
import org.opsli.common.utils.FieldUtil;
import org.opsli.common.utils.ListDistinctUtil;
import org.opsli.common.utils.WrapperUtil;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.msg.TokenMsg;
@ -48,14 +44,13 @@ import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.WebQueryBuilder;
import org.opsli.core.persistence.querybuilder.conf.WebQueryConf;
import org.opsli.core.utils.OptionsUtil;
import org.opsli.core.utils.OrgUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.org.service.ISysOrgService;
import org.opsli.modulars.system.user.entity.SysUser;
import org.opsli.modulars.system.user.entity.SysUserWeb;
import org.opsli.modulars.system.user.service.IUserRoleRefService;
import org.opsli.modulars.system.user.service.IUserService;
import org.opsli.plugins.oss.OssStorageFactory;
import org.opsli.plugins.oss.service.BaseOssStorageService;
@ -84,6 +79,8 @@ import java.util.List;
public class UserRestController extends BaseRestController<SysUser, UserModel, IUserService>
implements UserApi {
@Autowired
private IUserRoleRefService iUserRoleRefService;
/**
*
@ -143,7 +140,7 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
@ApiOperation(value = "用户组织机构", notes = "用户组织机构")
@Override
public ResultVo<?> getOrgByUserId(String userId) {
List<UserOrgRefModel> orgListByUserId = OrgUtil.getOrgListByUserId(userId);
List<UserOrgRefModel> orgListByUserId = UserUtil.getOrgListByUserId(userId);
return ResultVo.success(orgListByUserId);
}
@ -155,7 +152,7 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
@ApiOperation(value = "根据 userId 获得用户角色Id集合", notes = "根据 userId 获得用户角色Id集合")
@Override
public ResultVo<List<String>> getRoleIdsByUserId(String userId) {
List<String> roleIdList = IService.getRoleIdList(userId);
List<String> roleIdList = iUserRoleRefService.getRoleIdList(userId);
return ResultVo.success(roleIdList);
}
@ -489,104 +486,5 @@ public class UserRestController extends BaseRestController<SysUser, UserModel, I
return ResultVo.success(userModel);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<List<String>> getRolesByUserId(String userId) {
List<String> roleCodeList = IService.getRoleCodeList(userId);
return ResultVo.success(roleCodeList);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<List<String>> getAllPerms(String userId) {
List<String> allPerms = IService.getAllPerms(userId);
return ResultVo.success(allPerms);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<List<MenuModel>> getMenuListByUserId(String userId) {
List<MenuModel> menuModelList = IService.getMenuListByUserId(userId);
return ResultVo.success(menuModelList);
}
/**
*
* @param userId ID
* @return ResultVo
*/
@ApiOperation(value = "用户组织机构", notes = "用户组织机构")
@Override
public ResultVo<UserOrgRefWebModel> getOrgInfoByUserId(String userId) {
UserOrgRefWebModel org = null;
// 不写SQL了 直接分页 第一页 取第一条
QueryBuilder<SysUserWeb> queryBuilder = new GenQueryBuilder<>();
Page<SysUserWeb, UserWebModel> page = new Page<>(1, 1);
QueryWrapper<SysUserWeb> queryWrapper = queryBuilder.build();
queryWrapper.eq(
"a.id",
userId
);
page.setQueryWrapper(queryWrapper);
page = IService.findPageByCus(page);
List<UserWebModel> list = page.getList();
if(CollUtil.isNotEmpty(list)){
UserWebModel userWebModel = list.get(0);
if(userWebModel != null){
// org = userAndOrgModel.getOrg();
// if(org != null){
//
// org.setUserId(userId);
// List<String> orgIds = Lists.newArrayListWithCapacity(3);
// orgIds.add(org.getCompanyId());
// orgIds.add(org.getDepartmentId());
// orgIds.add(org.getPostId());
// QueryWrapper<SysOrg> orgQueryWrapper = new QueryWrapper<>();
// orgQueryWrapper.in(
// FieldUtil.humpToUnderline(MyBatisConstants.FIELD_ID),
// orgIds);
// List<SysOrg> orgList = iSysOrgService.findList(orgQueryWrapper);
// if(CollUtil.isNotEmpty(orgList)){
// Map<String, SysOrg> tmp = Maps.newHashMap();
// for (SysOrg sysOrg : orgList) {
// tmp.put(sysOrg.getId(), sysOrg);
// }
//
// // 设置 名称
// SysOrg company = tmp.get(org.getCompanyId());
// if(company != null){
// org.setCompanyName(company.getOrgName());
// }
//
// SysOrg department = tmp.get(org.getDepartmentId());
// if(department != null){
// org.setDepartmentName(department.getOrgName());
// }
//
// SysOrg post = tmp.get(org.getPostId());
// if(post != null){
// org.setPostName(post.getOrgName());
// }
// }
// }
}
}
return ResultVo.success(org);
}
}

@ -15,23 +15,32 @@
*/
package org.opsli.modulars.system.user.web;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserRoleRefApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.role.RoleModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.api.wrapper.system.user.UserRoleRefModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.msg.CoreMsg;
import org.opsli.core.utils.OrgUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.SystemMsg;
import org.opsli.modulars.system.user.service.IUserRoleRefService;
import org.opsli.modulars.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* - Controller
@ -52,6 +61,27 @@ public class UserRoleRefRestController implements UserRoleRefApi {
private IUserRoleRefService iUserRoleRefService;
/**
* userId Id
* @param userId Id
* @return ResultVo
*/
@ApiOperation(value = "根据 userId 获得用户角色Id集合", notes = "根据 userId 获得用户角色Id集合")
@Override
public ResultVo<UserRoleRefModel> getRoles(String userId) {
List<String> roleIdList = iUserRoleRefService.getRoleIdList(userId);
String defRoleId = iUserRoleRefService.getDefRoleId(userId);
UserRoleRefModel userRoleRefModel = UserRoleRefModel.builder()
.userId(userId)
.roleIds(Convert.toStrArray(roleIdList))
.defRoleId(defRoleId)
.build();
return ResultVo.success(userRoleRefModel);
}
/**
*
* @param model
@ -63,8 +93,7 @@ public class UserRoleRefRestController implements UserRoleRefApi {
// 演示模式 不允许操作
this.demoError();
boolean ret = iUserRoleRefService.setRoles(model.getUserId(),
model.getRoleIds());
boolean ret = iUserRoleRefService.setRoles(model);
if(ret){
return ResultVo.success();
}
@ -74,6 +103,58 @@ public class UserRoleRefRestController implements UserRoleRefApi {
);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<List<String>> getRolesByUserId(String userId) {
List<String> roleCodeList = iUserRoleRefService.getRoleCodeList(userId);
return ResultVo.success(roleCodeList);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<RoleModel> getDefRoleByUserId(String userId) {
RoleModel defRoleByUserId = iUserRoleRefService.getDefRoleByUserId(userId);
return ResultVo.success(defRoleByUserId);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<List<String>> getAllPerms(String userId) {
List<String> allPerms = iUserRoleRefService.getAllPerms(userId);
return ResultVo.success(allPerms);
}
/**
* userId
* @param userId Id
* @return ResultVo
*/
@Override
public ResultVo<List<MenuModel>> getMenuListByUserId(String userId) {
List<MenuModel> menuModelList = iUserRoleRefService.getMenuListByUserId(userId);
return ResultVo.success(menuModelList);
}
/**
*

@ -52,10 +52,12 @@ public class TestCar extends BaseEntity {
// ========================================
/** 多租户字段 */
private String tenantId;
/** 组织机构 */
private String orgIds;
/** 逻辑删除字段 */
@TableLogic
private String deleted;

Loading…
Cancel
Save