|
|
@@ -1,20 +1,30 @@
|
|
|
package com.fs.core.aspectj;
|
|
|
|
|
|
+import com.fs.app.exception.FSException;
|
|
|
+import com.fs.app.utils.JwtUtils;
|
|
|
import com.fs.common.annotation.DataScope;
|
|
|
import com.fs.common.core.domain.BaseEntity;
|
|
|
import com.fs.common.core.domain.entity.SysRole;
|
|
|
import com.fs.common.core.domain.entity.SysUser;
|
|
|
import com.fs.common.core.domain.model.LoginUser;
|
|
|
import com.fs.common.utils.SecurityUtils;
|
|
|
+import com.fs.common.utils.ServletUtils;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.company.domain.CompanyRole;
|
|
|
+import com.fs.company.domain.CompanyUser;
|
|
|
+import com.fs.company.service.ICompanyUserService;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.Signature;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
/**
|
|
|
@@ -56,6 +66,11 @@ public class DataScopeAspect
|
|
|
*/
|
|
|
public static final String DATA_SCOPE = "dataScope";
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private JwtUtils jwtUtils;
|
|
|
+ @Autowired
|
|
|
+ private ICompanyUserService companyUserService;
|
|
|
+
|
|
|
// 配置织入点
|
|
|
@Pointcut("@annotation(com.fs.common.annotation.DataScope)")
|
|
|
public void dataScopePointCut()
|
|
|
@@ -77,17 +92,29 @@ public class DataScopeAspect
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ HttpServletRequest request = ServletUtils.getRequest();
|
|
|
+ String token = request.getHeader(jwtUtils.getHeader());
|
|
|
+ if(StringUtils.isBlank(token)){
|
|
|
+ token = request.getParameter(jwtUtils.getHeader());
|
|
|
+ }
|
|
|
+
|
|
|
+ //凭证为空
|
|
|
+ if(StringUtils.isBlank(token)){
|
|
|
+ throw new FSException(jwtUtils.getHeader() + "不能为空", HttpStatus.UNAUTHORIZED.value());
|
|
|
+ }
|
|
|
+
|
|
|
+ Claims claims = jwtUtils.getClaimByToken(token);
|
|
|
+ if(claims == null || jwtUtils.isTokenExpired(claims.getExpiration())){
|
|
|
+ throw new FSException(jwtUtils.getHeader() + "失效,请重新登录", HttpStatus.UNAUTHORIZED.value());
|
|
|
+ }
|
|
|
+
|
|
|
// 获取当前的用户
|
|
|
- LoginUser loginUser = SecurityUtils.getLoginUser();
|
|
|
- if (StringUtils.isNotNull(loginUser))
|
|
|
+ CompanyUser companyUser = companyUserService.selectCompanyUserById(Long.parseLong(claims.getSubject()));
|
|
|
+ if (StringUtils.isNotNull(companyUser) && !companyUser.isAdmin())
|
|
|
{
|
|
|
- SysUser currentUser = loginUser.getUser();
|
|
|
// 如果是超级管理员,则不过滤数据
|
|
|
- if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
|
|
|
- {
|
|
|
- dataScopeFilter(joinPoint, currentUser, controllerDataScope.deptAlias(),
|
|
|
- controllerDataScope.userAlias());
|
|
|
- }
|
|
|
+ dataScopeFilter(joinPoint, companyUser, controllerDataScope.deptAlias(), controllerDataScope.userAlias());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -98,11 +125,11 @@ public class DataScopeAspect
|
|
|
* @param user 用户
|
|
|
* @param userAlias 别名
|
|
|
*/
|
|
|
- public static void dataScopeFilter(JoinPoint joinPoint, SysUser user, String deptAlias, String userAlias)
|
|
|
+ public static void dataScopeFilter(JoinPoint joinPoint, CompanyUser user, String deptAlias, String userAlias)
|
|
|
{
|
|
|
StringBuilder sqlString = new StringBuilder();
|
|
|
|
|
|
- for (SysRole role : user.getRoles())
|
|
|
+ for (CompanyRole role : user.getRoles())
|
|
|
{
|
|
|
String dataScope = role.getDataScope();
|
|
|
if (DATA_SCOPE_ALL.equals(dataScope))
|
|
|
@@ -113,7 +140,7 @@ public class DataScopeAspect
|
|
|
else if (DATA_SCOPE_CUSTOM.equals(dataScope))
|
|
|
{
|
|
|
sqlString.append(StringUtils.format(
|
|
|
- " OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,
|
|
|
+ " OR {}.dept_id IN ( SELECT dept_id FROM company_role_dept WHERE role_id = {} ) ", deptAlias,
|
|
|
role.getRoleId()));
|
|
|
}
|
|
|
else if (DATA_SCOPE_DEPT.equals(dataScope))
|
|
|
@@ -123,7 +150,7 @@ public class DataScopeAspect
|
|
|
else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
|
|
|
{
|
|
|
sqlString.append(StringUtils.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 company_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
|
|
|
deptAlias, user.getDeptId(), user.getDeptId()));
|
|
|
}
|
|
|
else if (DATA_SCOPE_SELF.equals(dataScope))
|