CheckCurrenUserAspect.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.fdkankan.scene.aop;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.fdkankan.common.constant.ErrorCode;
  6. import com.fdkankan.common.constant.SceneStatus;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import com.fdkankan.scene.annotation.CheckCurrentUser;
  9. import com.fdkankan.scene.entity.*;
  10. import com.fdkankan.scene.service.*;
  11. import com.fdkankan.web.user.SSOLoginHelper;
  12. import com.fdkankan.web.user.SSOUser;
  13. import java.io.IOException;
  14. import java.lang.reflect.Method;
  15. import java.util.List;
  16. import java.util.Objects;
  17. import java.util.Set;
  18. import java.util.stream.Collectors;
  19. import javax.servlet.http.HttpServletRequest;
  20. import com.fdkankan.web.util.WebUtil;
  21. import lombok.extern.log4j.Log4j2;
  22. import org.aspectj.lang.JoinPoint;
  23. import org.aspectj.lang.annotation.Aspect;
  24. import org.aspectj.lang.annotation.Before;
  25. import org.aspectj.lang.annotation.Pointcut;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.core.annotation.Order;
  28. import org.springframework.stereotype.Component;
  29. import org.springframework.web.context.request.RequestContextHolder;
  30. import org.springframework.web.context.request.ServletRequestAttributes;
  31. /**
  32. * 记录注册用户操作记录、异常记录
  33. */
  34. @Log4j2
  35. @Aspect
  36. @Component
  37. @Order(2)
  38. public class CheckCurrenUserAspect {
  39. @Autowired
  40. private ISceneProService sceneProService;
  41. @Autowired
  42. private ICameraService cameraService;
  43. @Autowired
  44. private SSOLoginHelper ssoLoginHelper;
  45. @Autowired
  46. private ISceneCooperationService sceneCooperationService;
  47. @Autowired
  48. private IUserRoleService userRoleService;
  49. @Autowired
  50. private IUserService userService;
  51. // Service层切点
  52. @Pointcut("@annotation(com.fdkankan.scene.annotation.CheckCurrentUser)")
  53. public void checkUserAspect() {
  54. }
  55. /**
  56. * 前置通知 用于拦截Controller层记录用户的操作
  57. *
  58. * @param joinPoint
  59. * 切点
  60. * @throws IOException
  61. */
  62. @Before("checkUserAspect()")
  63. public void doBefore(JoinPoint joinPoint) throws Exception {
  64. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  65. // 读取session中的用户
  66. SSOUser user = ssoLoginHelper.getSsoUser(request.getHeader("token"));
  67. if(Objects.isNull(user)){
  68. throw new BusinessException(ErrorCode.TOKEN_NOT_FOUND);
  69. }
  70. String num = WebUtil.getParameter("num", joinPoint, request);
  71. if(StrUtil.isEmpty(num)){
  72. throw new BusinessException(ErrorCode.PARAM_REQUIRED);
  73. }
  74. ScenePro scenePro= sceneProService.findBySceneNum(num);
  75. if(Objects.isNull(scenePro)){
  76. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  77. }
  78. //如果是计算中或者计算出错,返回计算中
  79. if(SceneStatus.wait.code().equals(scenePro.getStatus())
  80. || SceneStatus.FAILD.code().equals(scenePro.getStatus())){
  81. throw new BusinessException(ErrorCode.FAILURE_CODE_5033);
  82. }
  83. //校验场景用户是否与当前登录用户相同,相同则跳出
  84. if(Objects.isNull(scenePro.getUserId())){
  85. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  86. }
  87. if(scenePro.getUserId().equals(user.getId())){
  88. return;
  89. }
  90. //如果上面场景用户与当前用户不匹配,需要校验当前用户是否拥有某些角色,从而可以访问此场景
  91. List<UserRole> list = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()));
  92. Set<Long> roleIdSet = null;
  93. if(CollUtil.isNotEmpty(list)){
  94. roleIdSet = list.stream().map(ur -> ur.getRoleId()).collect(Collectors.toSet());
  95. }
  96. if(CollUtil.isEmpty(roleIdSet)){
  97. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  98. }
  99. //平台管理员拥有最高权限
  100. if(roleIdSet.contains(5L)){
  101. return;
  102. }
  103. //判断是否有公司管理者权限,有则放开
  104. if(roleIdSet.contains(6L)){
  105. //当前登录用户user
  106. User currentUser = userService.getById(user.getId());
  107. User sceneUser = userService.getById(scenePro.getUserId());
  108. if(Objects.isNull(currentUser) || Objects.isNull(currentUser.getCompanyId())
  109. || Objects.isNull(sceneUser) || Objects.isNull(sceneUser.getCompanyId())
  110. || !currentUser.getCompanyId().equals(sceneUser.getCompanyId())){
  111. throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  112. }
  113. }
  114. // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
  115. // .getRequest();
  116. // // 读取session中的用户
  117. // SSOUser user = ssoLoginHelper.loginCheckV3(request.getHeader("token"));
  118. // String sceneNum = request.getParameter("sceneNum");
  119. //
  120. // ScenePro entity = sceneProService.findBySceneNum(sceneNum);
  121. // if(user == null){
  122. // log.info(getCheckUserMthodDescription(joinPoint));
  123. // log.info("不是当前用户的方法:"
  124. // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  125. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  126. // }
  127. //
  128. // if("18750226207".equals(user.getUserName())){
  129. // log.info("18750226207该账号默认超级管理员,可以操作所有场景");
  130. // return;
  131. // }
  132. //
  133. // if(user.getId() == null){
  134. // Camera cameraEntity = cameraService.findByChildName(user.getUserName());
  135. // if((cameraEntity != null && entity != null) && (cameraEntity.getId().longValue() != entity.getCameraId().longValue())){
  136. // log.info(getCheckUserMthodDescription(joinPoint));
  137. // log.info("不是当前用户的方法:"
  138. // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  139. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  140. // }
  141. // }
  142. // else if((user != null && entity != null) && entity.getUserId() != null && (user.getId().longValue() != entity.getUserId().longValue())){
  143. // SceneCooperation sceneCooperation = sceneCooperationService.getByNum(sceneNum);
  144. // if(Objects.nonNull(sceneCooperation)){
  145. // if(sceneCooperation.getUserId().longValue() != user.getId().longValue()){
  146. // log.info(getCheckUserMthodDescription(joinPoint));
  147. // log.info("不是当前用户的方法:"
  148. // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  149. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  150. // }
  151. // }else {
  152. // log.info(getCheckUserMthodDescription(joinPoint));
  153. // log.info("不是当前用户的方法:"
  154. // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
  155. // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
  156. // }
  157. // }
  158. }
  159. /**
  160. * 获取注解中对方法的描述信息
  161. *
  162. * @param joinPoint
  163. * 切点
  164. * @return 方法描述
  165. * @throws Exception
  166. */
  167. public static String getCheckUserMthodDescription(JoinPoint joinPoint) throws Exception {
  168. String targetName = joinPoint.getTarget().getClass().getName();
  169. String methodName = joinPoint.getSignature().getName();
  170. Object[] arguments = joinPoint.getArgs();
  171. Class targetClass = Class.forName(targetName);
  172. Method[] methods = targetClass.getMethods();
  173. String description = "";
  174. for (Method method : methods) {
  175. if (method.getName().equals(methodName)) {
  176. Class[] clazzs = method.getParameterTypes();
  177. if (clazzs.length == arguments.length) {
  178. description = method.getAnnotation(CheckCurrentUser.class).description();
  179. break;
  180. }
  181. }
  182. }
  183. return description;
  184. }
  185. }