| | |
| | | import cn.hutool.core.lang.Validator; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.core.domain.BaseEntity; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.domain.model.LoginUser; |
| | |
| | | { |
| | | StringBuilder sqlString = new StringBuilder(); |
| | | |
| | | // 将 "." 提取出,不写别名为单表查询,写别名为多表查询 |
| | | deptAlias = StrUtil.isNotBlank(deptAlias) ? deptAlias + "." : ""; |
| | | userAlias = StrUtil.isNotBlank(userAlias) ? userAlias + "." : ""; |
| | | |
| | | for (SysRole role : user.getRoles()) |
| | | { |
| | | String dataScope = role.getDataScope(); |
| | |
| | | else if (DATA_SCOPE_CUSTOM.equals(dataScope)) |
| | | { |
| | | sqlString.append(StrUtil.format( |
| | | " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, |
| | | " OR {}dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias, |
| | | role.getRoleId())); |
| | | } |
| | | else if (DATA_SCOPE_DEPT.equals(dataScope)) |
| | | { |
| | | sqlString.append(StrUtil.format(" OR {}.dept_id = {} ", deptAlias, user.getDeptId())); |
| | | sqlString.append(StrUtil.format(" OR {}dept_id = {} ", deptAlias, user.getDeptId())); |
| | | } |
| | | else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) |
| | | { |
| | | sqlString.append(StrUtil.format( |
| | | " OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )", |
| | | " OR {}dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )", |
| | | deptAlias, user.getDeptId(), user.getDeptId())); |
| | | } |
| | | else if (DATA_SCOPE_SELF.equals(dataScope)) |
| | | { |
| | | if (StrUtil.isNotBlank(userAlias)) |
| | | { |
| | | sqlString.append(StrUtil.format(" OR {}.user_id = {} ", userAlias, user.getUserId())); |
| | | sqlString.append(StrUtil.format(" OR {}user_id = {} ", userAlias, user.getUserId())); |
| | | } |
| | | else |
| | | { |
| | |
| | | Object params = joinPoint.getArgs()[0]; |
| | | if (Validator.isNotNull(params)) |
| | | { |
| | | try { |
| | | Method getParams = params.getClass().getDeclaredMethod("getParams", null); |
| | | Map<String, Object> invoke = (Map<String, Object>) getParams.invoke(params, null); |
| | | invoke.put(DATA_SCOPE, sql); |
| | | } catch (Exception e) { |
| | | // 方法未找到 不处理 |
| | | if(params instanceof BaseEntity) { |
| | | BaseEntity baseEntity = (BaseEntity) params; |
| | | baseEntity.getParams().put(DATA_SCOPE, sql); |
| | | } else { |
| | | try { |
| | | Method getParams = params.getClass().getDeclaredMethod("getParams", null); |
| | | Map<String, Object> invoke = (Map<String, Object>) getParams.invoke(params, null); |
| | | invoke.put(DATA_SCOPE, sql); |
| | | } catch (Exception e) { |
| | | // 方法未找到 不处理 |
| | | } |
| | | } |
| | | } |
| | | } |