123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package com.fdkankan.scene.aop;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.fdkankan.common.constant.ErrorCode;
- import com.fdkankan.common.constant.SceneStatus;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.scene.annotation.CheckCurrentUser;
- import com.fdkankan.scene.entity.*;
- import com.fdkankan.scene.service.*;
- import com.fdkankan.web.user.SSOLoginHelper;
- import com.fdkankan.web.user.SSOUser;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.List;
- import java.util.Objects;
- import java.util.Set;
- import java.util.stream.Collectors;
- import javax.servlet.http.HttpServletRequest;
- import com.fdkankan.web.util.WebUtil;
- import lombok.extern.log4j.Log4j2;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.annotation.Order;
- import org.springframework.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- /**
- * 记录注册用户操作记录、异常记录
- */
- @Log4j2
- @Aspect
- @Component
- @Order(2)
- public class CheckCurrenUserAspect {
- @Autowired
- private ISceneProService sceneProService;
- @Autowired
- private ICameraService cameraService;
- @Autowired
- private SSOLoginHelper ssoLoginHelper;
- @Autowired
- private ISceneCooperationService sceneCooperationService;
- @Autowired
- private IUserRoleService userRoleService;
- @Autowired
- private IUserService userService;
- // Service层切点
- @Pointcut("@annotation(com.fdkankan.scene.annotation.CheckCurrentUser)")
- public void checkUserAspect() {
- }
- /**
- * 前置通知 用于拦截Controller层记录用户的操作
- *
- * @param joinPoint
- * 切点
- * @throws IOException
- */
- @Before("checkUserAspect()")
- public void doBefore(JoinPoint joinPoint) throws Exception {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- // 读取session中的用户
- SSOUser user = ssoLoginHelper.getSsoUser(request.getHeader("token"));
- if(Objects.isNull(user)){
- throw new BusinessException(ErrorCode.TOKEN_NOT_FOUND);
- }
- String num = WebUtil.getParameter("num", joinPoint, request);
- if(StrUtil.isEmpty(num)){
- throw new BusinessException(ErrorCode.PARAM_REQUIRED);
- }
- ScenePro scenePro= sceneProService.findBySceneNum(num);
- if(Objects.isNull(scenePro)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
- }
- //如果是计算中或者计算出错,返回计算中
- if(SceneStatus.wait.code().equals(scenePro.getStatus())
- || SceneStatus.FAILD.code().equals(scenePro.getStatus())){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5033);
- }
- //校验场景用户是否与当前登录用户相同,相同则跳出
- if(Objects.isNull(scenePro.getUserId())){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- if(scenePro.getUserId().equals(user.getId())){
- return;
- }
- //如果上面场景用户与当前用户不匹配,需要校验当前用户是否拥有某些角色,从而可以访问此场景
- List<UserRole> list = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()));
- Set<Long> roleIdSet = null;
- if(CollUtil.isNotEmpty(list)){
- roleIdSet = list.stream().map(ur -> ur.getRoleId()).collect(Collectors.toSet());
- }
- if(CollUtil.isEmpty(roleIdSet)){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- //平台管理员拥有最高权限
- if(roleIdSet.contains(5L)){
- return;
- }
- //判断是否有公司管理者权限,有则放开
- if(roleIdSet.contains(6L)){
- //当前登录用户user
- User currentUser = userService.getById(user.getId());
- User sceneUser = userService.getById(scenePro.getUserId());
- if(Objects.isNull(currentUser) || Objects.isNull(currentUser.getCompanyId())
- || Objects.isNull(sceneUser) || Objects.isNull(sceneUser.getCompanyId())
- || !currentUser.getCompanyId().equals(sceneUser.getCompanyId())){
- throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- }
- }
- // HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
- // .getRequest();
- // // 读取session中的用户
- // SSOUser user = ssoLoginHelper.loginCheckV3(request.getHeader("token"));
- // String sceneNum = request.getParameter("sceneNum");
- //
- // ScenePro entity = sceneProService.findBySceneNum(sceneNum);
- // if(user == null){
- // log.info(getCheckUserMthodDescription(joinPoint));
- // log.info("不是当前用户的方法:"
- // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- // }
- //
- // if("18750226207".equals(user.getUserName())){
- // log.info("18750226207该账号默认超级管理员,可以操作所有场景");
- // return;
- // }
- //
- // if(user.getId() == null){
- // Camera cameraEntity = cameraService.findByChildName(user.getUserName());
- // if((cameraEntity != null && entity != null) && (cameraEntity.getId().longValue() != entity.getCameraId().longValue())){
- // log.info(getCheckUserMthodDescription(joinPoint));
- // log.info("不是当前用户的方法:"
- // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- // }
- // }
- // else if((user != null && entity != null) && entity.getUserId() != null && (user.getId().longValue() != entity.getUserId().longValue())){
- // SceneCooperation sceneCooperation = sceneCooperationService.getByNum(sceneNum);
- // if(Objects.nonNull(sceneCooperation)){
- // if(sceneCooperation.getUserId().longValue() != user.getId().longValue()){
- // log.info(getCheckUserMthodDescription(joinPoint));
- // log.info("不是当前用户的方法:"
- // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- // }
- // }else {
- // log.info(getCheckUserMthodDescription(joinPoint));
- // log.info("不是当前用户的方法:"
- // + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
- // throw new BusinessException(ErrorCode.FAILURE_CODE_5014);
- // }
- // }
- }
- /**
- * 获取注解中对方法的描述信息
- *
- * @param joinPoint
- * 切点
- * @return 方法描述
- * @throws Exception
- */
- public static String getCheckUserMthodDescription(JoinPoint joinPoint) throws Exception {
- String targetName = joinPoint.getTarget().getClass().getName();
- String methodName = joinPoint.getSignature().getName();
- Object[] arguments = joinPoint.getArgs();
- Class targetClass = Class.forName(targetName);
- Method[] methods = targetClass.getMethods();
- String description = "";
- for (Method method : methods) {
- if (method.getName().equals(methodName)) {
- Class[] clazzs = method.getParameterTypes();
- if (clazzs.length == arguments.length) {
- description = method.getAnnotation(CheckCurrentUser.class).description();
- break;
- }
- }
- }
- return description;
- }
- }
|