Java tutorial
/** * Copyright 2018 ? http://www.renren.io * <p> * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package io.renren.common.aspect; import io.renren.common.annotation.DataFilter; import io.renren.common.exception.RRException; import io.renren.common.utils.Constant; import io.renren.modules.sys.entity.SysUserEntity; import io.renren.modules.sys.service.SysDeptService; import io.renren.modules.sys.service.SysRoleDeptService; import io.renren.modules.sys.service.SysUserRoleService; import io.renren.modules.sys.shiro.ShiroUtils; import org.apache.commons.lang.StringUtils; 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.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; /** * ??? * * @author Mark sunlightcs@gmail.com * @since 3.0.0 2017-09-17 */ @Aspect @Component public class DataFilterAspect { @Autowired private SysDeptService sysDeptService; @Autowired private SysUserRoleService sysUserRoleService; @Autowired private SysRoleDeptService sysRoleDeptService; @Pointcut("@annotation(io.renren.common.annotation.DataFilter)") public void dataFilterCut() { } @Before("dataFilterCut()") public void dataFilter(JoinPoint point) throws Throwable { Object params = point.getArgs()[0]; if (params != null && params instanceof Map) { SysUserEntity user = ShiroUtils.getUserEntity(); //??? if (user.getUserId() != Constant.SUPER_ADMIN) { Map map = (Map) params; map.put(Constant.SQL_FILTER, getSQLFilter(user, point)); } return; } throw new RRException("?????Map??NULL"); } /** * ??SQL */ private String getSQLFilter(SysUserEntity user, JoinPoint point) { MethodSignature signature = (MethodSignature) point.getSignature(); DataFilter dataFilter = signature.getMethod().getAnnotation(DataFilter.class); //??? String tableAlias = dataFilter.tableAlias(); if (StringUtils.isNotBlank(tableAlias)) { tableAlias += "."; } //ID Set<Long> deptIdList = new HashSet<>(); //ID List<Long> roleIdList = sysUserRoleService.queryRoleIdList(user.getUserId()); if (roleIdList.size() > 0) { List<Long> userDeptIdList = sysRoleDeptService .queryDeptIdList(roleIdList.toArray(new Long[roleIdList.size()])); deptIdList.addAll(userDeptIdList); } //?ID if (dataFilter.subDept()) { List<Long> subDeptIdList = sysDeptService.getSubDeptIdList(user.getDeptId()); deptIdList.addAll(subDeptIdList); } StringBuilder sqlFilter = new StringBuilder(); sqlFilter.append(" ("); if (deptIdList.size() > 0) { sqlFilter.append(tableAlias).append(dataFilter.deptId()).append(" in(") .append(StringUtils.join(deptIdList, ",")).append(")"); } //???? if (dataFilter.user()) { if (deptIdList.size() > 0) { sqlFilter.append(" or "); } sqlFilter.append(tableAlias).append(dataFilter.userId()).append("=").append(user.getUserId()); } sqlFilter.append(")"); return sqlFilter.toString(); } }