SpDataScopeAspect.java 4.1 KB
package com.bs.common.aop;

import com.bs.common.annotation.SpDataScope;
import com.bs.common.core.domain.BaseEntity;
import com.bs.common.core.domain.model.LoginUser;
import com.bs.common.utils.SecurityUtils;
import com.bs.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * 服务商数据过滤处理
 *
 * @author system
 */
@Aspect
@Component
public class SpDataScopeAspect {

    /**
     * 数据权限过滤关键字
     */
    public static final String DATA_SCOPE = "dataScope";

    /**
     * 驻店人员角色名称
     */
    public static final String ROLE_STORE_STAFF = "驻店人员";

    /**
     * 总部运营角色名称
     */
    public static final String ROLE_HQ_OPERATION = "总部运营";

    @Before("@annotation(spDataScope)")
    public void doBefore(JoinPoint point, SpDataScope spDataScope) throws Throwable {
        clearDataScope(point);
        handleDataScope(point, spDataScope);
    }

    protected void handleDataScope(final JoinPoint joinPoint, SpDataScope spDataScope) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)) {
            String spCode = loginUser.getSpCode();
            String storeCode = loginUser.getStoreCode();
            String spRoleName = loginUser.getSpRoleName();
            Long userId = loginUser.getUserId();

            // 如果登录信息中的服务商编码和门店编码为空,则无需处理
            if (StringUtils.isEmpty(spCode) && StringUtils.isEmpty(storeCode)) {
                return;
            }

            // 如果角色为空,也无需处理
            if (StringUtils.isEmpty(spRoleName)) {
                return;
            }

            StringBuilder sqlString = new StringBuilder();
            String alias = spDataScope.alias();
            if (StringUtils.isNotEmpty(alias)) {
                alias = alias + ".";
            } else {
                alias = "";
            }

            if (ROLE_STORE_STAFF.equals(spRoleName)) {
                // 驻店人员:只能看自己门店,本人所属服务商的自己数据
                if (StringUtils.isNotEmpty(storeCode)) {
                    sqlString.append(StringUtils.format(" OR {}store_code = '{}' AND {}sp_code = '{}' AND {}create_by_id = '{}' ", alias, storeCode, alias, spCode, alias, userId));
                }
            } else if (ROLE_HQ_OPERATION.equals(spRoleName)) {
                // 总部运营:查看服务商关联的门店数据
                if (StringUtils.isNotEmpty(spCode)) {
                    if (null == storeCode || storeCode.isEmpty()) {
                        sqlString.append(StringUtils.format(" OR {}sp_code = '{}' ", alias, spCode));
                    } else {
                        sqlString.append(StringUtils.format(" OR {}store_code = '{}' AND {}sp_code = '{}' ", alias, storeCode, alias, spCode));
                    }
                }
            }

            if (StringUtils.isNotBlank(sqlString.toString())) {
                Object[] args = joinPoint.getArgs();
                if (args != null) {
                    for (Object arg : args) {
                        if (StringUtils.isNotNull(arg) && arg instanceof BaseEntity) {
                            BaseEntity baseEntity = (BaseEntity) arg;
                            baseEntity.getParams().put(DATA_SCOPE, " (" + sqlString.substring(4) + ")");
                        }
                    }
                }
            }
        }
    }

    /**
     * 拼接权限sql前先清空params.dataScope参数防止注入
     */
    private void clearDataScope(final JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        if (args != null) {
            for (Object arg : args) {
                if (StringUtils.isNotNull(arg) && arg instanceof BaseEntity) {
                    BaseEntity baseEntity = (BaseEntity) arg;
                    baseEntity.getParams().put(DATA_SCOPE, "");
                }
            }
        }
    }
}