提交 8036604c 编写于 作者: zhouweidong's avatar zhouweidong

权限按照Authority返回

上级 12d3a520
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
TARGET=PSSYSTEM TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
{ {
"systemid":"${sys.getName()}",
"unires":[ "unires":[
<#if sys.getAllPSSysUniReses()??> <#if sys.getAllPSSysUniReses()??>
<#list sys.getAllPSSysUniReses() as unires> <#list sys.getAllPSSysUniReses() as unires>
......
...@@ -12,8 +12,8 @@ import com.alibaba.fastjson.JSONObject; ...@@ -12,8 +12,8 @@ import com.alibaba.fastjson.JSONObject;
public class IBZUAAFallback implements IBZUAAFeignClient { public class IBZUAAFallback implements IBZUAAFeignClient {
@Override @Override
public boolean pushSystemPermissionData(String systemid,JSONObject systemPermissionData) { public Boolean syncSysAuthority(JSONObject system) {
return false; return null;
} }
@Override @Override
......
...@@ -13,13 +13,12 @@ import com.alibaba.fastjson.JSONObject; ...@@ -13,13 +13,12 @@ import com.alibaba.fastjson.JSONObject;
public interface IBZUAAFeignClient public interface IBZUAAFeignClient
{ {
/** /**
* 推送系统权限数据到uaa * 同步系统资源到uaa
* @param systemid * @param system 系统资源信息
* @param systemPermissionData
* @return * @return
*/ */
@PostMapping("/syspssystems/{systemid}/permissiondata") @PostMapping("/syspssystems/save")
boolean pushSystemPermissionData(@PathVariable("systemid") String systemid,@RequestBody JSONObject systemPermissionData); Boolean syncSysAuthority(@RequestBody JSONObject system);
/** /**
* 用户登录 * 用户登录
......
...@@ -40,10 +40,14 @@ public class PermissionSyncJob implements ApplicationRunner { ...@@ -40,10 +40,14 @@ public class PermissionSyncJob implements ApplicationRunner {
Thread.sleep(10000); Thread.sleep(10000);
InputStream permission= this.getClass().getResourceAsStream("/permission/systemResource.json"); //获取当前系统所有实体资源能力 InputStream permission= this.getClass().getResourceAsStream("/permission/systemResource.json"); //获取当前系统所有实体资源能力
String permissionResult = IOUtils.toString(permission,"UTF-8"); String permissionResult = IOUtils.toString(permission,"UTF-8");
if(client.pushSystemPermissionData(systemId,JSONObject.parseObject(permissionResult))){ JSONObject system= new JSONObject();
system.put("pssystemid",systemId);
system.put("pssystemname",systemId);
system.put("sysstructure",JSONObject.parseObject(permissionResult));
if(client.syncSysAuthority(system)){
log.info("向[UAA]同步系统资源成功"); log.info("向[UAA]同步系统资源成功");
}else{ }else{
log.info(String.format("向[UAA]同步系统资源失败")); log.error("向[UAA]同步系统资源失败");
} }
} }
catch (Exception ex) { catch (Exception ex) {
......
...@@ -3,18 +3,21 @@ TARGET=PSSYSTEM ...@@ -3,18 +3,21 @@ TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
package ${pub.getPKGCodeName()}.util.rest; package ${pub.getPKGCodeName()}.util.rest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import ${pub.getPKGCodeName()}.util.security.AuthenticationUser;
import ${pub.getPKGCodeName()}.util.service.AuthenticationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils; import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value; import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired; import java.util.HashSet;
import ${pub.getPKGCodeName()}.util.security.AuthenticationUser; import java.util.Iterator;
import ${pub.getPKGCodeName()}.util.service.AuthenticationUserService; import java.util.Set;
@RestController @RestController
@RequestMapping(value = "") @RequestMapping(value = "")
...@@ -30,14 +33,20 @@ public class AppController { ...@@ -30,14 +33,20 @@ public class AppController {
public ResponseEntity<JSONObject> getAppData() { public ResponseEntity<JSONObject> getAppData() {
JSONObject appData = new JSONObject() ; JSONObject appData = new JSONObject() ;
JSONArray uniRes=new JSONArray(); Set<String> appMenu = new HashSet();
JSONArray appMenu=new JSONArray(); Set<String> uniRes = new HashSet();
if(enablePermissionValid){ if(enablePermissionValid){
JSONObject userPermission=AuthenticationUser.getAuthenticationUser().getPermissionList(); Collection<GrantedAuthority> authorities=AuthenticationUser.getAuthenticationUser().getAuthorities();
if(!ObjectUtils.isEmpty(userPermission)){ Iterator it = authorities.iterator();
uniRes = userPermission.getJSONArray("unires"); while(it.hasNext()) {
appMenu = userPermission.getJSONArray("appmenu"); GrantedAuthority authority = (GrantedAuthority)it.next();
} String strAuthority=authority.getAuthority();
if(strAuthority.startsWith("UNIRES"))
uniRes.add(strAuthority);
else if(strAuthority.startsWith("APPMENU"))
appMenu.add(strAuthority);
}
} }
appData.put("unires",uniRes); appData.put("unires",uniRes);
appData.put("appmenu",appMenu); appData.put("appmenu",appMenu);
......
...@@ -3,8 +3,6 @@ TARGET=PSSYSTEM ...@@ -3,8 +3,6 @@ TARGET=PSSYSTEM
</#ibiztemplate> </#ibiztemplate>
package ${pub.getPKGCodeName()}.util.security; package ${pub.getPKGCodeName()}.util.security;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mongodb.QueryBuilder; import com.mongodb.QueryBuilder;
...@@ -21,6 +19,7 @@ import org.springframework.data.mongodb.core.query.BasicQuery; ...@@ -21,6 +19,7 @@ import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.access.PermissionEvaluator; import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -38,10 +37,6 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -38,10 +37,6 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
@Value("${r'${ibiz.enablePermissionValid:false}'}") @Value("${r'${ibiz.enablePermissionValid:false}'}")
boolean enablePermissionValid; //是否开启权限校验 boolean enablePermissionValid; //是否开启权限校验
/**
* 实体行为操作标识
*/
private String DEActionType="DEACTION";
/** /**
*实体主键标识 *实体主键标识
*/ */
...@@ -70,9 +65,6 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -70,9 +65,6 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
List<String> ids=null; List<String> ids=null;
EntityBase entity; EntityBase entity;
List<EntityBase> entityList = null; List<EntityBase> entityList = null;
JSONObject userPermission= AuthenticationUser.getAuthenticationUser().getPermissionList();
if(userPermission==null)
return false;
MappingBase mappingBase= (MappingBase) paramList.get(1); MappingBase mappingBase= (MappingBase) paramList.get(1);
//参数准备 //参数准备
if(action.equalsIgnoreCase("remove")){ if(action.equalsIgnoreCase("remove")){
...@@ -89,26 +81,19 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -89,26 +81,19 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
if (entity==null) if (entity==null)
return false; return false;
JSONObject permissionList=userPermission.getJSONObject("entities"); Set<String> entityDataRange = getAuthorities(authentication,entity.getClass().getSimpleName(),action);
String entityName = entity.getClass().getSimpleName(); if(entityDataRange.size()==0)
return false;
//拥有全部数据访问权限时,则跳过权限检查 //拥有全部数据访问权限时,则跳过权限检查
if(isAllData(entityName,action,permissionList)){ if(isAllData(action,entityDataRange)){
return true; return true;
} }
//检查是否有操作权限[create.update.delete.read]
if(!validDEActionHasPermission(entityName,action,permissionList)){
return false;
}
JSONArray dataRangeList=getDataRange(entityName,action,permissionList);
if(dataRangeList.size()==0)
return false;
if(action.equalsIgnoreCase("create")){ if(action.equalsIgnoreCase("create")){
return createBatchActionPermissionValid(entityList,dataRangeList); return createBatchActionPermissionValid(entityList,entityDataRange);
} }
else if(action.equalsIgnoreCase("save")){ else if(action.equalsIgnoreCase("save")){
return saveBatchActionPermissionValid(deStorageMode, entityList, dataRangeList); return saveBatchActionPermissionValid(deStorageMode, entityList, entityDataRange);
} }
else{ else{
if(!action.equalsIgnoreCase("remove")){ if(!action.equalsIgnoreCase("remove")){
...@@ -116,7 +101,7 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -116,7 +101,7 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
} }
if(ids.size()==0) if(ids.size()==0)
return false; return false;
return otherBatchActionPermissionValidRouter(deStorageMode, entity ,ids, dataRangeList); return otherBatchActionPermissionValidRouter(deStorageMode, entity ,ids, entityDataRange);
} }
} }
...@@ -145,23 +130,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -145,23 +130,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
if (StringUtils.isEmpty(entity)) if (StringUtils.isEmpty(entity))
return false; return false;
JSONObject userPermission= AuthenticationUser.getAuthenticationUser().getPermissionList(); Set<String> entityDataRange = getAuthorities(authentication,entity.getClass().getSimpleName(),action);
if(userPermission==null)
if(entityDataRange.size()==0)
return false; return false;
JSONObject permissionList=userPermission.getJSONObject("entities");
String entityName = entity.getClass().getSimpleName();
//拥有全部数据访问权限时,则跳过权限检查 //拥有全部数据访问权限时,则跳过权限检查
if(isAllData(entityName,action,permissionList)){ if(isAllData(action,entityDataRange)){
return true; return true;
} }
//检查是否有操作权限[create.update.delete.read]
if(!validDEActionHasPermission(entityName,action,permissionList)){
return false;
}
JSONArray dataRangeList=getDataRange(entityName,action,permissionList);
if(dataRangeList.size()==0)
return false;
if(action.equalsIgnoreCase("save")){ if(action.equalsIgnoreCase("save")){
Map<String,String> permissionField=getPermissionField(entity); Map<String,String> permissionField=getPermissionField(entity);
...@@ -173,21 +150,41 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -173,21 +150,41 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
action="update"; action="update";
} }
if(action.equalsIgnoreCase("create")){ if(action.equalsIgnoreCase("create")){
return createActionPermissionValid(entity,dataRangeList); return createActionPermissionValid(entity,entityDataRange);
} }
else{ else{
return otherActionPermissionValidRouter(deStorageMode, entity, id, dataRangeList); return otherActionPermissionValidRouter(deStorageMode, entity, id, entityDataRange);
} }
} }
/**
* 获取用户权限资源
* @param authentication
* @param entityName
* @param action
* @return
*/
private Set<String> getAuthorities(Authentication authentication,String entityName,String action){
Collection authorities=authentication.getAuthorities();
Set<String> entityDataRange = new HashSet();
Iterator var2 = authorities.iterator();
while(var2.hasNext()) {
GrantedAuthority authority = (GrantedAuthority)var2.next();
if(authority.getAuthority().contains(String.format("%s-%s-",entityName,action)))
entityDataRange.add(authority.getAuthority());
}
return entityDataRange;
}
/** /**
* save校验 * save校验
* @param deStorageMode * @param deStorageMode
* @param entityList * @param entityList
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean saveBatchActionPermissionValid(String deStorageMode, List<EntityBase> entityList, JSONArray dataRangeList) { private boolean saveBatchActionPermissionValid(String deStorageMode, List<EntityBase> entityList, Set<String> entityDataRange) {
if(entityList==null || entityList.size()==0) if(entityList==null || entityList.size()==0)
return false; return false;
...@@ -205,12 +202,12 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -205,12 +202,12 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
updateList.add(String.valueOf(id)); updateList.add(String.valueOf(id));
} }
if(updateList.size()>0){ if(updateList.size()>0){
boolean isUpdate = otherBatchActionPermissionValidRouter(deStorageMode, tempEntity ,updateList, dataRangeList); boolean isUpdate = otherBatchActionPermissionValidRouter(deStorageMode, tempEntity ,updateList, entityDataRange);
if(!isUpdate) if(!isUpdate)
return false; return false;
} }
if(createList.size()>0){ if(createList.size()>0){
boolean isCreate=createBatchActionPermissionValid(entityList,dataRangeList); boolean isCreate=createBatchActionPermissionValid(entityList,entityDataRange);
if(!isCreate) if(!isCreate)
return false; return false;
} }
...@@ -220,12 +217,12 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -220,12 +217,12 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
/** /**
* 批处理新建权限校验 * 批处理新建权限校验
* @param entityList * @param entityList
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean createBatchActionPermissionValid(List<EntityBase> entityList,JSONArray dataRangeList){ private boolean createBatchActionPermissionValid(List<EntityBase> entityList,Set<String> entityDataRange){
for(EntityBase entity : entityList){ for(EntityBase entity : entityList){
boolean isCreate = createActionPermissionValid(entity ,dataRangeList); boolean isCreate = createActionPermissionValid(entity ,entityDataRange);
if(!isCreate){ if(!isCreate){
return false; return false;
} }
...@@ -238,16 +235,16 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -238,16 +235,16 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
* @param deStorageMode * @param deStorageMode
* @param entity * @param entity
* @param ids * @param ids
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean otherBatchActionPermissionValidRouter(String deStorageMode , EntityBase entity , List<String> ids , JSONArray dataRangeList){ private boolean otherBatchActionPermissionValidRouter(String deStorageMode , EntityBase entity , List<String> ids , Set<String> entityDataRange){
if(deStorageMode.equalsIgnoreCase("sql")){ if(deStorageMode.equalsIgnoreCase("sql")){
return sqlBatchPermissionValid(entity ,ids, dataRangeList); return sqlBatchPermissionValid(entity ,ids, entityDataRange);
} }
else if(deStorageMode.equalsIgnoreCase("nosql")){ else if(deStorageMode.equalsIgnoreCase("nosql")){
return noSqlBatchPermissionValid(entity, ids , dataRangeList); return noSqlBatchPermissionValid(entity, ids , entityDataRange);
} }
else if(deStorageMode.equalsIgnoreCase("serviceapi")){ else if(deStorageMode.equalsIgnoreCase("serviceapi")){
return true; return true;
...@@ -261,16 +258,16 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -261,16 +258,16 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
* SQL批处理权限校验 * SQL批处理权限校验
* @param entity * @param entity
* @param ids * @param ids
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean sqlBatchPermissionValid(EntityBase entity , List<String> ids, JSONArray dataRangeList){ private boolean sqlBatchPermissionValid(EntityBase entity , List<String> ids, Set<String> entityDataRange){
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
String keyFieldName=permissionField.get(keyFieldTag); String keyFieldName=permissionField.get(keyFieldTag);
ServiceImpl service= SpringContextHolder.getBean(String.format("%s%s",entity.getClass().getSimpleName(),"ServiceImpl"));//获取实体service对象 ServiceImpl service= SpringContextHolder.getBean(String.format("%s%s",entity.getClass().getSimpleName(),"ServiceImpl"));//获取实体service对象
//通过权限表达式来获取sql //通过权限表达式来获取sql
String permissionSQL= String.format(" (%s) AND ( %s in (%s) ) ",getPermissionSQL(entity,dataRangeList),keyFieldName,getEntityKeyCond(ids)); //拼接权限条件-编辑 String permissionSQL= String.format(" (%s) AND ( %s in (%s) ) ",getPermissionSQL(entity,entityDataRange),keyFieldName,getEntityKeyCond(ids)); //拼接权限条件-编辑
//执行sql进行权限检查 //执行sql进行权限检查
QueryWrapper permissionWrapper=getPermissionWrapper(permissionSQL);//构造权限条件 QueryWrapper permissionWrapper=getPermissionWrapper(permissionSQL);//构造权限条件
List list=service.list(permissionWrapper); List list=service.list(permissionWrapper);
...@@ -285,15 +282,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -285,15 +282,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
* NoSQL批处理权限校验 * NoSQL批处理权限校验
* @param entity * @param entity
* @param ids * @param ids
* @param dataRange * @param entityDataRange
* @return * @return
*/ */
private boolean noSqlBatchPermissionValid(EntityBase entity, List<String> ids, JSONArray dataRange) { private boolean noSqlBatchPermissionValid(EntityBase entity, List<String> ids, Set<String> entityDataRange) {
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
String keyFieldName=permissionField.get(keyFieldTag); String keyFieldName=permissionField.get(keyFieldTag);
//根据权限表达式填充权限条件 //根据权限表达式填充权限条件
QueryBuilder permissionCond=getNoSqlPermissionCond(entity,dataRange); QueryBuilder permissionCond=getNoSqlPermissionCond(entity,entityDataRange);
//权限条件拼接主键 //权限条件拼接主键
permissionCond.and(keyFieldName).in(ids); permissionCond.and(keyFieldName).in(ids);
//执行权限检查 //执行权限检查
...@@ -309,59 +306,26 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -309,59 +306,26 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
/** /**
* 是否为全部数据 * 是否为全部数据
* @param permissionList
* @param entityName
* @param action
* @return
*/
private boolean isAllData( String entityName, String action ,JSONObject permissionList) {
if(permissionList==null)
return false;
if(!permissionList.containsKey(entityName))
return false;
JSONObject entity=permissionList.getJSONObject(entityName);
if(!entity.containsKey(DEActionType))
return false;
JSONObject dataRange=entity.getJSONObject(DEActionType);//获取实体行为对应的数据范围
if(dataRange.containsKey(action) && dataRange.getJSONArray(action).contains("all"))
return true;
return false;
}
/**
* 实体行为权限校验
* @param userPermission
* @param entityName
* @param action * @param action
* userPermission:{"ENTITY":{"DEACTION":{"READ":["CURORG"]},"DATASET":{"Default":["CURORG"]}}} * @param entityDataRange
* @return * @return
*/ */
private boolean validDEActionHasPermission(String entityName , String action ,JSONObject userPermission){ private boolean isAllData(String action , Set<String> entityDataRange) {
for(String dataRange : entityDataRange ){
boolean hasPermission=false; if(dataRange.endsWith(String.format("%s-all",action))){
if(userPermission==null) return true;
return false; }
if(!userPermission.containsKey(entityName))
return false;
JSONObject entity=userPermission.getJSONObject(entityName);//获取实体
if(!entity.containsKey(DEActionType))
return false;
JSONObject dataRange=entity.getJSONObject(DEActionType);//获取实体行为对应的数据范围
if(dataRange.containsKey(action)){
hasPermission=true;
} }
return hasPermission; return false;
} }
/** /**
* 新建行为校验 * 新建行为校验
* @param entity * @param entity
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean createActionPermissionValid(EntityBase entity, JSONArray dataRangeList){ private boolean createActionPermissionValid(EntityBase entity, Set<String> entityDataRange){
boolean isCreate=true; boolean isCreate=true;
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
...@@ -382,24 +346,23 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -382,24 +346,23 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
Set<String> userOrg = new HashSet<>(); Set<String> userOrg = new HashSet<>();
Set<String> userOrgDept = new HashSet<>(); Set<String> userOrgDept = new HashSet<>();
for(int a=0;a<dataRangeList.size();a++){ for(String permissionCond:entityDataRange){
String permissionCond=dataRangeList.getString(a);//权限配置条件 if(permissionCond.endsWith("curorg")){ //本单位
if(permissionCond.equals("curorg")){ //本单位
userOrg.add(authenticationUser.getOrgid()); userOrg.add(authenticationUser.getOrgid());
} }
else if(permissionCond.equals("porg")){//上级单位 else if(permissionCond.endsWith("porg")){//上级单位
userOrg.addAll(orgParent); userOrg.addAll(orgParent);
} }
else if(permissionCond.equals("sorg")){//下级单位 else if(permissionCond.endsWith("sorg")){//下级单位
userOrg.addAll(orgChild); userOrg.addAll(orgChild);
} }
else if(permissionCond.equals("curorgdept")){//本部门 else if(permissionCond.endsWith("curorgdept")){//本部门
userOrgDept.add(authenticationUser.getMdeptid()); userOrgDept.add(authenticationUser.getMdeptid());
} }
else if(permissionCond.equals("porgdept")){//上级部门 else if(permissionCond.endsWith("porgdept")){//上级部门
userOrgDept.addAll(orgDeptParent); userOrgDept.addAll(orgDeptParent);
} }
else if(permissionCond.equals("sorgdept")){//下级部门 else if(permissionCond.endsWith("sorgdept")){//下级部门
userOrgDept.addAll(orgDeptChild); userOrgDept.addAll(orgDeptChild);
} }
} }
...@@ -422,16 +385,16 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -422,16 +385,16 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
* @param deStorageMode * @param deStorageMode
* @param entity * @param entity
* @param id * @param id
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean otherActionPermissionValidRouter(String deStorageMode, EntityBase entity , Object id , JSONArray dataRangeList){ private boolean otherActionPermissionValidRouter(String deStorageMode, EntityBase entity , Object id , Set<String> entityDataRange){
if(deStorageMode.equalsIgnoreCase("sql")){ if(deStorageMode.equalsIgnoreCase("sql")){
return sqlPermissionValid(entity , id, dataRangeList); return sqlPermissionValid(entity , id, entityDataRange);
} }
else if(deStorageMode.equalsIgnoreCase("nosql")){ else if(deStorageMode.equalsIgnoreCase("nosql")){
return noSqlPermissionValid(entity , id, dataRangeList); return noSqlPermissionValid(entity , id, entityDataRange);
} }
else if(deStorageMode.equalsIgnoreCase("serviceapi")){ else if(deStorageMode.equalsIgnoreCase("serviceapi")){
return true; return true;
...@@ -445,15 +408,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -445,15 +408,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
* sql存储模式实体行为鉴权 * sql存储模式实体行为鉴权
* @param entity * @param entity
* @param id * @param id
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean sqlPermissionValid(EntityBase entity , Object id, JSONArray dataRangeList){ private boolean sqlPermissionValid(EntityBase entity , Object id, Set<String> entityDataRange){
ServiceImpl service= SpringContextHolder.getBean(String.format("%s%s",entity.getClass().getSimpleName(),"ServiceImpl"));//获取实体service对象 ServiceImpl service= SpringContextHolder.getBean(String.format("%s%s",entity.getClass().getSimpleName(),"ServiceImpl"));//获取实体service对象
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
//通过权限表达式来获取sql //通过权限表达式来获取sql
String permissionSQL= String.format(" (%s) AND (%s='%s')",getPermissionSQL(entity,dataRangeList),permissionField.get(keyFieldTag),id); //拼接权限条件-编辑 String permissionSQL= String.format(" (%s) AND (%s='%s')",getPermissionSQL(entity,entityDataRange),permissionField.get(keyFieldTag),id); //拼接权限条件-编辑
//执行sql进行权限检查 //执行sql进行权限检查
QueryWrapper permissionWrapper=getPermissionWrapper(permissionSQL);//构造权限条件 QueryWrapper permissionWrapper=getPermissionWrapper(permissionSQL);//构造权限条件
List list=service.list(permissionWrapper); List list=service.list(permissionWrapper);
...@@ -469,15 +432,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -469,15 +432,15 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
* NoSQL实体行为鉴权 * NoSQL实体行为鉴权
* @param entity * @param entity
* @param id * @param id
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private boolean noSqlPermissionValid(EntityBase entity, Object id, JSONArray dataRangeList) { private boolean noSqlPermissionValid(EntityBase entity, Object id, Set<String> entityDataRange) {
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
String keyField=permissionField.get(keyFieldTag); String keyField=permissionField.get(keyFieldTag);
//根据权限表达式填充权限条件 //根据权限表达式填充权限条件
QueryBuilder permissionCond=getNoSqlPermissionCond(entity,dataRangeList); QueryBuilder permissionCond=getNoSqlPermissionCond(entity,entityDataRange);
//权限条件拼接主键 //权限条件拼接主键
permissionCond.and(keyField).is(id); permissionCond.and(keyField).is(id);
//执行权限检查 //执行权限检查
...@@ -495,10 +458,10 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -495,10 +458,10 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
/** /**
* NoSQL存储模式的表格查询填充权限条件 * NoSQL存储模式的表格查询填充权限条件
* @param entity * @param entity
* @param dataRangeList * @param entityDataRange
* @return * @return
*/ */
private QueryBuilder getNoSqlPermissionCond( EntityBase entity ,JSONArray dataRangeList ){ private QueryBuilder getNoSqlPermissionCond( EntityBase entity ,Set<String> entityDataRange){
QueryBuilder permissionSQL=new QueryBuilder(); QueryBuilder permissionSQL=new QueryBuilder();
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
...@@ -512,30 +475,29 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -512,30 +475,29 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
Set<String> orgDeptParent = userInfo.get("parentdept"); Set<String> orgDeptParent = userInfo.get("parentdept");
Set<String> orgDeptChild = userInfo.get("subdept"); Set<String> orgDeptChild = userInfo.get("subdept");
for(int i=0;i<dataRangeList.size();i++){ for(String permissionCond:entityDataRange){
String permissionCond=dataRangeList.getString(i);//权限配置条件 if(permissionCond.endsWith("curorg")){ //本单位
if(permissionCond.equals("curorg")){ //本单位
permissionSQL.or(new QueryBuilder().and(orgField).is(AuthenticationUser.getAuthenticationUser().getOrgid()).get()); permissionSQL.or(new QueryBuilder().and(orgField).is(AuthenticationUser.getAuthenticationUser().getOrgid()).get());
} }
else if(permissionCond.equals("porg")){//上级单位 else if(permissionCond.endsWith("porg")){//上级单位
permissionSQL.or(new QueryBuilder().and(orgField).in(formatStringArr(orgParent)).get()); permissionSQL.or(new QueryBuilder().and(orgField).in(formatStringArr(orgParent)).get());
} }
else if(permissionCond.equals("sorg")){//下级单位 else if(permissionCond.endsWith("sorg")){//下级单位
permissionSQL.or(new QueryBuilder().and(orgField).in(formatStringArr(orgChild)).get()); permissionSQL.or(new QueryBuilder().and(orgField).in(formatStringArr(orgChild)).get());
} }
else if(permissionCond.equals("createman")){//建立人 else if(permissionCond.endsWith("createman")){//建立人
permissionSQL.or(new QueryBuilder().and(createManField).is(AuthenticationUser.getAuthenticationUser().getUserid()).get()); permissionSQL.or(new QueryBuilder().and(createManField).is(AuthenticationUser.getAuthenticationUser().getUserid()).get());
} }
else if(permissionCond.equals("curorgdept")){//本部门 else if(permissionCond.endsWith("curorgdept")){//本部门
permissionSQL.or(new QueryBuilder().and(orgDeptField).is(AuthenticationUser.getAuthenticationUser().getMdeptid()).get()); permissionSQL.or(new QueryBuilder().and(orgDeptField).is(AuthenticationUser.getAuthenticationUser().getMdeptid()).get());
} }
else if(permissionCond.equals("porgdept")){//上级部门 else if(permissionCond.endsWith("porgdept")){//上级部门
permissionSQL.or(new QueryBuilder().and(orgDeptField).in(formatStringArr(orgDeptParent)).get()); permissionSQL.or(new QueryBuilder().and(orgDeptField).in(formatStringArr(orgDeptParent)).get());
} }
else if(permissionCond.equals("sorgdept")){//下级部门 else if(permissionCond.endsWith("sorgdept")){//下级部门
permissionSQL.or(new QueryBuilder().and(orgDeptField).in(formatStringArr(orgDeptChild)).get()); permissionSQL.or(new QueryBuilder().and(orgDeptField).in(formatStringArr(orgDeptChild)).get());
} }
else if(permissionCond.equals("all")){ else if(permissionCond.endsWith("all")){
permissionSQL.or(new QueryBuilder().get()); permissionSQL.or(new QueryBuilder().get());
} }
} }
...@@ -546,10 +508,10 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -546,10 +508,10 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
/** /**
* SQL获取权限条件 * SQL获取权限条件
* @param entity * @param entity
* @param oppriList * @param entityDataRange
* @return * @return
*/ */
private String getPermissionSQL(EntityBase entity, JSONArray oppriList){ private String getPermissionSQL(EntityBase entity, Set<String> entityDataRange){
Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性 Map<String,String> permissionField=getPermissionField(entity);//获取组织、部门预置属性
String nPermissionSQL = "1<>1"; String nPermissionSQL = "1<>1";
...@@ -564,31 +526,30 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -564,31 +526,30 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
Set<String> orgDeptParent = userInfo.get("parentdept"); Set<String> orgDeptParent = userInfo.get("parentdept");
Set<String> orgDeptChild = userInfo.get("subdept"); Set<String> orgDeptChild = userInfo.get("subdept");
for(int i=0;i<oppriList.size();i++){ for(String permissionCond: entityDataRange){
permissionSQL.append("OR"); permissionSQL.append("OR");
String permissionCond=oppriList.getString(i);//权限配置条件 if(permissionCond.endsWith("curorg")){ //本单位
if(permissionCond.equals("curorg")){ //本单位
permissionSQL.append(String.format("(%s='%s')",orgField,AuthenticationUser.getAuthenticationUser().getOrgid())); permissionSQL.append(String.format("(%s='%s')",orgField,AuthenticationUser.getAuthenticationUser().getOrgid()));
} }
else if(permissionCond.equals("porg")){//上级单位 else if(permissionCond.endsWith("porg")){//上级单位
permissionSQL.append(String.format(" %s in(%s) ", orgField, formatStringArr(orgParent))); permissionSQL.append(String.format(" %s in(%s) ", orgField, formatStringArr(orgParent)));
} }
else if(permissionCond.equals("sorg")){//下级单位 else if(permissionCond.endsWith("sorg")){//下级单位
permissionSQL.append(String.format(" %s in(%s) ", orgField, formatStringArr(orgChild))); permissionSQL.append(String.format(" %s in(%s) ", orgField, formatStringArr(orgChild)));
} }
else if(permissionCond.equals("createman")){//建立人 else if(permissionCond.endsWith("createman")){//建立人
permissionSQL.append(String.format("(%s='%s')",createManField,AuthenticationUser.getAuthenticationUser().getUserid())); permissionSQL.append(String.format("(%s='%s')",createManField,AuthenticationUser.getAuthenticationUser().getUserid()));
} }
else if(permissionCond.equals("curorgdept")){//本部门 else if(permissionCond.endsWith("curorgdept")){//本部门
permissionSQL.append(String.format("(%s='%s')",orgDeptField,AuthenticationUser.getAuthenticationUser().getMdeptid())); permissionSQL.append(String.format("(%s='%s')",orgDeptField,AuthenticationUser.getAuthenticationUser().getMdeptid()));
} }
else if(permissionCond.equals("porgdept")){//上级部门 else if(permissionCond.endsWith("porgdept")){//上级部门
permissionSQL.append(String.format(" %s in (%s) ", orgDeptField, formatStringArr(orgDeptParent))); permissionSQL.append(String.format(" %s in (%s) ", orgDeptField, formatStringArr(orgDeptParent)));
} }
else if(permissionCond.equals("sorgdept")){//下级部门 else if(permissionCond.endsWith("sorgdept")){//下级部门
permissionSQL.append(String.format(" %s in (%s) ", orgDeptField, formatStringArr(orgDeptChild))); permissionSQL.append(String.format(" %s in (%s) ", orgDeptField, formatStringArr(orgDeptChild)));
} }
else if(permissionCond.equals("all")){//全部数据 else if(permissionCond.endsWith("all")){//全部数据
permissionSQL.append("(1=1)"); permissionSQL.append("(1=1)");
} }
else{ else{
...@@ -704,21 +665,6 @@ public class AuthPermissionEvaluator implements PermissionEvaluator { ...@@ -704,21 +665,6 @@ public class AuthPermissionEvaluator implements PermissionEvaluator {
return "'" + String.join("','", arr) + "'"; return "'" + String.join("','", arr) + "'";
} }
/**
* 获取数据范围
* @param entityName
* @param action
* @param permissionList
* @return
*/
private JSONArray getDataRange(String entityName, String action , JSONObject permissionList){
//获取权限表达式[全部数据、本单位、本部门等]
JSONObject entityObj=permissionList.getJSONObject(entityName);//获取实体
JSONObject permissionType= entityObj.getJSONObject(DEActionType);
JSONArray dataRangeList=permissionType.getJSONArray(action);//行为:readinsert...
return dataRangeList;
}
/** /**
* 获取实体主键集合 * 获取实体主键集合
* @param entityBase * @param entityBase
......
...@@ -17,6 +17,7 @@ import ${pub.getPKGCodeName()}.util.domain.IBZUSER; ...@@ -17,6 +17,7 @@ import ${pub.getPKGCodeName()}.util.domain.IBZUSER;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.security.core.authority.AuthorityUtils;
/** /**
* 实体[IBZUSER] 服务对象接口实现 * 实体[IBZUSER] 服务对象接口实现
...@@ -75,6 +76,9 @@ public class IBZUSERServiceImpl extends ServiceImpl<IBZUSERMapper, IBZUSER> impl ...@@ -75,6 +76,9 @@ public class IBZUSERServiceImpl extends ServiceImpl<IBZUSERMapper, IBZUSER> impl
public AuthenticationUser createUserDetails(IBZUSER user) { public AuthenticationUser createUserDetails(IBZUSER user) {
AuthenticationUser userdatail = new AuthenticationUser(); AuthenticationUser userdatail = new AuthenticationUser();
CachedBeanCopier.copy(user,userdatail); CachedBeanCopier.copy(user,userdatail);
if(userdatail.getSuperuser()==1){
userdatail.setAuthorities(AuthorityUtils.createAuthorityList("ROLE_SUPERADMIN"));
}
return userdatail; return userdatail;
} }
} }
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册