1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
public interface AuthJudge { public static final List<Integer> operateCodes=new ArrayList(){{ this.add(SysRoleConstant.SysRoleEnum.COMPANY_SUPER_MANAGER.getCode()); this.add(SysRoleConstant.SysRoleEnum.SCHEMA_OPERATOR_USER.getCode()); this.add(SysRoleConstant.SysRoleEnum.SCHEMA_OPERATOR_MANAGER.getCode());
}};
public static final String[] operateCodesArray={SysRoleConstant.COMPANY_SUPER_MANAGER_CODE+"", SysRoleConstant.SCHEMA_OPERATOR_USER_CODE+"",SysRoleConstant.SCHEMA_OPERATOR_MANAGER_CODE+""}; public static final String[] implCodesArray={SysRoleConstant.COMPANY_SUPER_MANAGER_CODE+"", SysRoleConstant.SCHEMA_IMP_MANAGER_CODE+"",SysRoleConstant.SCHEMA_IMP_USER_CODE+""};
public static final List<Integer> implCodes=new ArrayList(){{ this.add(SysRoleConstant.SysRoleEnum.COMPANY_SUPER_MANAGER.getCode()); this.add(SysRoleConstant.SysRoleEnum.SCHEMA_IMP_MANAGER.getCode()); this.add(SysRoleConstant.SysRoleEnum.SCHEMA_IMP_USER.getCode()); }};
default Boolean checkAuthentication(HttpServletRequest request, HttpServletResponse response, Object handler, UserProjectScheams userProjectScheams){ if(userProjectScheams==null || CollectionUtils.isEmpty(userProjectScheams.getProjectUsers())){ getLogger().error("projectUsers is null:{}",userProjectScheams); return false; }
String projectId=getProjectIdByRequest(request); String schemaId=getSchemaIdByRequest(request); if(StringUtils.isNotBlank(projectId) && StringUtils.isNotBlank(schemaId)){ return checkByUserProjectSchemas(projectId,schemaId,userProjectScheams); }else if(StringUtils.isNotBlank(projectId)){ return checkByUserProjectSchemas(projectId,userProjectScheams); }else{ return checkByUserProjectSchemas(userProjectScheams); } };
@NotNull List<Integer> getAuthSysRoleCodes();
Logger getLogger();
}
@Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) public @interface DataAuthAnnotation {
String[] idKeys();
Class<? extends DataAuthAbstractHandle>[] handleCls();
int[] roleCodes();
String[] otherParams() default {};
String method() default "";
String type() default ""; public static final String PUBLISH_CHECK="1";
public static final String THIS_KEY_REGEX="^_this_\\d*$"; public static final String THIS_KEY="_this_";
}
public abstract class DataAuthAbstractHandle implements AuthJudge { @Autowired private ProjectUserCache projectUserCache;
@Autowired private ProjectManager projectManager;
@Data public static class DataAuthDomain { private String projectId=""; private String schemaId="";
@Override public boolean equals(Object newV) { if (newV == null) { return false; } if (newV.getClass() == this.getClass()) { DataAuthDomain newV2 = (DataAuthDomain) newV; return StringUtils.equals(newV2.getProjectId(), projectId) && StringUtils.equals(newV2.getSchemaId(), schemaId); } return false; } @Override public int hashCode(){ int result = 17; result = result*31+projectId==null?0:projectId.hashCode(); result = result*31+schemaId==null?0:schemaId.hashCode(); return result; } }
protected abstract Set<DataAuthDomain> getAuthDomain(Set<String> ids, String[] params,String fieldName);
public Boolean checkDataAuth(@NotNull DataAuthAnnotation dataAuthAnnotation, @NotNull String userId, @NotNull Map<String,Set<String>> ids) { if (MapUtils.isEmpty(ids)) { return true; }
int[] roleCodes = dataAuthAnnotation.roleCodes(); String[] params = dataAuthAnnotation.otherParams(); List<Integer> roleList = Arrays.stream(roleCodes).boxed().collect(Collectors.toList()); for(Map.Entry<String, Set<String>> entry:ids.entrySet()) { String fieldName=entry.getKey(); Set<String> vids=entry.getValue(); if(CollectionUtils.isEmpty(vids)){ continue; } Set<DataAuthDomain> dataAuthDomains = getAuthDomain(vids, params,fieldName); if(CollectionUtils.isEmpty(dataAuthDomains)){ continue; } for (DataAuthDomain dataAuthDomain : dataAuthDomains) { String projectId = dataAuthDomain.getProjectId(); String schemaId = dataAuthDomain.getSchemaId(); UserProjectScheams userProjectScheams = projectUserCache.getHashValue(userId); if (!checkByUserProjectSchemas(roleList, projectId, schemaId, userProjectScheams)) { return false; } if (DataAuthAnnotation.PUBLISH_CHECK.equals(dataAuthAnnotation.type())) { if (projectManager.hasSchemaInPublish(projectId)) { getLogger().warn("ProjectId has schema ing publish, projectId:{}", projectId); throw new CustomBusinessException(ErrorCodeUtils.PermissionErrorCode.SCHEMA_IN_PUBLISH); } } } }
return true; }
@Nullable public static <T> T findInRequestAttributes(String fieldName){ Object o= AuthUtils.findInRequestAttributes(fieldName); if(o==null){ return null; } return (T)o; }
public int getOrder(){ return 2<<16; } }
|