Commit a2b7ac36 by leo330

权限开发

parent e39e6d16
......@@ -18,7 +18,7 @@
</parent>
<properties>
<auth.version>3.0.12-release</auth.version>
<auth.version>4.0.9-beta</auth.version>
</properties>
<dependencies>
......
package com.keymobile.metadata.metadataRelation.auth;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public abstract class AbstractOptionMaker implements OptionMaker{
public List<String> featureNameList;
public List<Integer> featureBitList;
public Integer featureTotalBit;
public AbstractOptionMaker() {
featureNameList = getFeatureList().stream().map(feature -> feature.getDisplayName()).collect(Collectors.toList());
featureBitList = getFeatureList().stream().map(feature -> feature.getBit()).collect(Collectors.toList());
featureTotalBit = getFeatureList().stream().mapToInt(Feature::getBit).sum();
}
public List<Feature> getFeatureList() {
return Arrays.asList(
new Feature("import","导入", Auth.Btn.IMPORT_BIT)
);
}
public List<String> getFeatureNameList() {
return getFeatureList().stream().map(feature -> feature.getName()).collect(Collectors.toList());
}
public List<Integer> getFeatureBitList() {
return getFeatureList().stream().map(feature -> feature.getBit()).collect(Collectors.toList());
}
public Integer getFeatureTotalBit() {
return getFeatureList().stream().mapToInt(Feature::getBit).sum();
}
@Override
public abstract List<OptionPropertyDataImpl> getOptionProperties();
@Override
public abstract List<OptionImpl> getOptions(Long dataRoleId,Long domainId);
@Override
public abstract List<OptionImpl> saveOptions(Long dataRoleId, List<OptionImpl> options,Long domainId);
public abstract List<Feature> getMyFeatureList();
}
package com.keymobile.metadata.metadataRelation.auth;
public final class Auth {
public interface Btn {
int IMPORT_BIT = 1;
}
}
package com.keymobile.metadata.metadataRelation.auth;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@Document(collection="mdr_authority")
public class Authority {
@Id
private String id;
private String name;
@Indexed
private Long domainId;
@Indexed
private Long dataRoleId;
@Indexed
private String range;
@Indexed
private String optionId;
@Indexed
private String optionPropertyType;
private String optionDataType;
private Integer grantedFeature;
private String value;
private List<String> parentFullQualifiedName;
public Authority() {
}
public Authority(String id, Long dataRoleId, String range, String optionId, Integer grantedFeature, String value) {
this.id = id;
this.dataRoleId = dataRoleId;
this.range = range;
this.optionId = optionId;
this.grantedFeature = grantedFeature;
this.value = value;
}
public Authority(String id, String name, Long domainId, Long dataRoleId, String range, String optionId, String optionPropertyType, Integer grantedFeature, String value, List<String> parentFullQualifiedName) {
this.id = id;
this.name = name;
this.domainId = domainId;
this.dataRoleId = dataRoleId;
this.range = range;
this.optionId = optionId;
this.optionPropertyType = optionPropertyType;
this.grantedFeature = grantedFeature;
this.value = value;
this.parentFullQualifiedName = parentFullQualifiedName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public Long getDataRoleId() {
return dataRoleId;
}
public void setDataRoleId(Long dataRoleId) {
this.dataRoleId = dataRoleId;
}
public String getRange() {
return range;
}
public void setRange(String range) {
this.range = range;
}
public String getOptionId() {
return optionId;
}
public void setOptionId(String optionId) {
this.optionId = optionId;
}
public Integer getGrantedFeature() {
return grantedFeature;
}
public void setGrantedFeature(Integer grantedFeature) {
this.grantedFeature = grantedFeature;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getOptionPropertyType() {
return optionPropertyType;
}
public void setOptionPropertyType(String optionPropertyType) {
this.optionPropertyType = optionPropertyType;
}
public List<String> getParentFullQualifiedName() {
return parentFullQualifiedName;
}
public void setParentFullQualifiedName(List<String> parentFullQualifiedName) {
this.parentFullQualifiedName = parentFullQualifiedName;
}
public String getOptionDataType() {
return optionDataType;
}
public void setOptionDataType(String optionDataType) {
this.optionDataType = optionDataType;
}
}
package com.keymobile.metadata.metadataRelation.auth;
import com.keymobile.auth.common.security.GrantedAuthHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AuthorityChecker {
@Autowired
private MongoTemplate mongoTemplate;
public Boolean hasPermission(String range, String optionId, Integer btnBit, Long domainId) {
List<Long> dataRoleIdList = getUserDataRoleIds();
Criteria criteria=Criteria.where("range").is(range)
.and("dataRoleId").in(dataRoleIdList).and("optionId").is(optionId);
List<Authority> authorityList = mongoTemplate.find(Query.query(criteria), Authority.class);
boolean result = false;
for (Authority authority : authorityList) {
if ((authority.getGrantedFeature() & btnBit) > 0) {
result = true;
break;
}
}
return result;
}
public Boolean hasPermission(Long dataRoleId,String range, String optionId, Integer btnBit) {
List<Authority> authorityList = mongoTemplate.find(Query.query(Criteria.where("range").is(range)
.and("dataRoleId").is(dataRoleId).and("optionId").is(optionId)), Authority.class);
boolean result = false;
for (Authority authority : authorityList) {
if ((authority.getGrantedFeature() & btnBit) > 0) {
result = true;
break;
}
}
return result;
}
private List<Long> getUserDataRoleIds() {
return GrantedAuthHelper.getGrantedRoles();
}
}
package com.keymobile.metadata.metadataRelation.auth;
import com.google.common.collect.Lists;
import com.keymobile.auth.common.security.GrantedAuthHelper;
import org.apache.commons.lang3.StringUtils;
import org.bson.BsonRegularExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class AuthorityService {
private static String ROLE_ID_FIELD_NAME = "roleId";
private static String MENU_ID_FIELD_NAME = "menuId";
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private OptionMakerFactory optionMakerFactory;
@Autowired
private AuthorityChecker authorityChecker;
private OptionMaker optionMaker;
public List<OptionImpl> getOptions(Long dataRoleId, String range, Long domainId) {
OptionMaker optionMaker = optionMakerFactory.getOptionMakerByRange(range);
return optionMaker.getOptions(dataRoleId, domainId);
}
public List<OptionImpl> saveOptions(Long dataRoleId, String range, List<OptionImpl> optionList, Long domainId) {
OptionMaker optionMaker = optionMakerFactory.getOptionMakerByRange(range);
return optionMaker.saveOptions(dataRoleId, optionList, domainId);
}
public List<OptionPropertyDataImpl> getOptionProperties(String range) {
OptionMaker optionMaker = optionMakerFactory.getOptionMakerByRange(range);
return optionMaker.getOptionProperties();
}
public List<Map<String, Object>> listAllRanges() {
List<Map<String, Object>> rangeList = new ArrayList<>();
for (Range range : Range.values()) {
Map<String, Object> map = new HashMap<>();
map.put("id", range.getId());
map.put("name", range.getName());
rangeList.add(map);
}
return rangeList;
}
public List<Feature> getAllowButtons(String range, String optionId, Long domainId) {
OptionMaker optionMaker = optionMakerFactory.getOptionMakerByRange(range);
AbstractOptionMaker abstractOptionMaker = (AbstractOptionMaker) optionMaker;
List<Feature> featureList = abstractOptionMaker.getMyFeatureList();
List<Feature> allowButtons = new ArrayList<>();
featureList.forEach(feature -> {
if (authorityChecker.hasPermission(range, optionId, feature.getBit(), domainId)) {
allowButtons.add(feature);
}
});
return allowButtons;
}
}
package com.keymobile.metadata.metadataRelation.auth;
public class Feature {
private String name;
private String displayName;
private int bit;
public Feature(String name, String displayName, int bit) {
this.name = name;
this.displayName = displayName;
this.bit = bit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public int getBit() {
return bit;
}
public void setBit(int bit) {
this.bit = bit;
}
}
package com.keymobile.metadata.metadataRelation.auth;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class MetaRelationMaker extends AbstractOptionMaker {
public static final String RANGE = Range.METADATA_RELATION.getId();
private static final String RANGE_FIELD = "range";
private static final String DATA_ROLE_ID_FIELD = "dataRoleId";
@Autowired
private MongoTemplate mongoTemplate;
public List<OptionPropertyDataImpl> getOptionProperties() {
return Lists.newArrayList(new OptionPropertyDataImpl(featureTotalBit - (Auth.Btn.IMPORT_BIT), featureNameList.toArray(new String[0]), featureBitList.toArray(new Integer[0])));
}
public List<OptionImpl> getOptions(Long dataRoleId, Long domainId) {
//根据角色id获取选中的option
List<Authority> authorityList = mongoTemplate.find(Query.query(Criteria.where(RANGE_FIELD).is(RANGE).and(DATA_ROLE_ID_FIELD).is(dataRoleId)), Authority.class);
Map<String, Integer> savedGrantedFeature;
if (null != authorityList) {
savedGrantedFeature = authorityList.stream().collect(Collectors.toMap(Authority::getOptionId, Authority::getGrantedFeature));
} else {
savedGrantedFeature = new HashMap<>();
}
List<OptionImpl> optionList = new ArrayList<>();
addOption(savedGrantedFeature, optionList, "关系图谱", "关系图谱",featureTotalBit - Auth.Btn.IMPORT_BIT);
return optionList;
}
private void addOption(Map<String, Integer> savedGrantedFeature, List<OptionImpl> optionList, String name, String id,Integer mask) {
Integer modelGrantedFeature = savedGrantedFeature.get(id);
optionList.add(dirToOption(id, name, new String[]{}, modelGrantedFeature,mask));
}
@Override
public List<OptionImpl> saveOptions(Long dataRoleId, List<OptionImpl> options, Long domainId) {
List<Authority> exist = mongoTemplate.find(Query.query(Criteria.where("dataRoleId").is(dataRoleId).and("range").is(RANGE)), Authority.class);
List<Authority> authorityList = options.stream().map(option -> {
Authority authority = new Authority();
authority.setOptionId(option.getId());
authority.setRange(RANGE);
authority.setDataRoleId(dataRoleId);
authority.setGrantedFeature(option.getGrantedFeature());
return authority;
}).collect(Collectors.toList());
mongoTemplate.insertAll(authorityList);
List<String> existIds = exist.stream().map(option -> option.getId()).collect(Collectors.toList());
mongoTemplate.findAllAndRemove(Query.query(Criteria.where("_id").in(existIds)), Authority.class);
return options;
}
@Override
public List<Feature> getMyFeatureList() {
return Arrays.asList(
new Feature("import","导入", Auth.Btn.IMPORT_BIT)
);
}
private OptionImpl dirToOption(String id, String name, String[] parentPath, Integer grantedFeature,Integer mask) {
if (null == grantedFeature) {
grantedFeature = 0;
}
OptionPropertyDataImpl optionPropertyData = new OptionPropertyDataImpl();
optionPropertyData.setFeatureBits(featureBitList.toArray(new Integer[0]));
optionPropertyData.setFeatures(featureNameList.toArray(new String[0]));
optionPropertyData.setMask(mask);
OptionImpl optionData = new OptionImpl(id, name, parentPath, optionPropertyData, grantedFeature, true);
return optionData;
}
}
package com.keymobile.metadata.metadataRelation.auth;
import com.keymobile.auth.common.conf.Option;
import com.keymobile.auth.common.conf.OptionProperty;
public class OptionImpl implements Option {
private String id;
private String name;
private String[] parentFullQualifiedName;
private OptionPropertyDataImpl optionProperty;
private String optionPropertyType="default";
private Integer grantedFeature;
private Boolean isGrantable;
public OptionImpl() {
}
public OptionImpl(String id, String name, String[] parentFullQualifiedName, OptionPropertyDataImpl optionProperty, Integer grantedFeature, boolean isGrantable) {
this.id = id;
this.name = name;
this.parentFullQualifiedName = parentFullQualifiedName;
this.optionProperty = optionProperty;
this.grantedFeature = grantedFeature;
this.isGrantable = isGrantable;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setOptionProperty(OptionPropertyDataImpl optionProperty) {
this.optionProperty = optionProperty;
}
public void setGrantedFeature(Integer grantedFeature) {
this.grantedFeature = grantedFeature;
}
public void setOptionPropertyType(String optionPropertyType) {
this.optionPropertyType = optionPropertyType;
}
public Boolean getGrantable() {
return isGrantable;
}
public void setGrantable(Boolean grantable) {
isGrantable = grantable;
}
@Override
public String getId() {
return id;
}
@Override
public String getName() {
return name;
}
@Override
public String[] getParentFullQualifiedName() {
return parentFullQualifiedName;
}
@Override
public OptionProperty getOptionProperty() {
return optionProperty;
}
@Override
public String getOptionPropertyType() {
return optionPropertyType;
}
@Override
public Integer getDisabledFeature() {
return 0;
}
@Override
public Integer getGrantedFeature() {
return grantedFeature;
}
@Override
public boolean isGrantable() {
return isGrantable;
}
}
package com.keymobile.metadata.metadataRelation.auth;
import java.util.List;
public interface OptionMaker {
List<OptionPropertyDataImpl> getOptionProperties();
List<OptionImpl> getOptions(Long dataRoleId,Long domainId);
List<OptionImpl> saveOptions(Long dataRoleId, List<OptionImpl> options,Long domainId);
}
package com.keymobile.metadata.metadataRelation.auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
@Service
public class OptionMakerFactory {
HashMap<String, OptionMaker> optionProviderMakers = new HashMap<>();
@Autowired
public void setMetaRelationMaker(MetaRelationMaker metaModelOptionMaker) {
optionProviderMakers.put(Range.METADATA_RELATION.getId(), metaModelOptionMaker);
}
public OptionMaker getOptionMakerByRange(String range) {
if (null != optionProviderMakers.get(range)) {
return optionProviderMakers.get(range);
} else {
throw new RuntimeException("unSupport range " + range);
}
}
public List<OptionPropertyDataImpl> getOptionProperties(String range) {
if (null != optionProviderMakers.get(range)) {
return optionProviderMakers.get(range).getOptionProperties();
} else {
throw new RuntimeException("unSupport range " + range);
}
}
}
package com.keymobile.metadata.metadataRelation.auth;
import com.keymobile.auth.common.conf.OptionProperty;
public class OptionPropertyDataImpl implements OptionProperty {
private Integer mask;
private String[] features;
private Integer[] featureBits;
private String optionPropertyType="default";
public OptionPropertyDataImpl() {
}
public OptionPropertyDataImpl(Integer mask, String[] features, Integer[] featureBits) {
this.mask = mask;
this.features = features;
this.featureBits = featureBits;
}
public OptionPropertyDataImpl(Integer mask, String[] features, Integer[] featureBits, String optionPropertyType) {
this.mask = mask;
this.features = features;
this.featureBits = featureBits;
this.optionPropertyType = optionPropertyType;
}
public void setMask(Integer mask) {
this.mask = mask;
}
public void setFeatures(String[] features) {
this.features = features;
}
public void setFeatureBits(Integer[] featureBits) {
this.featureBits = featureBits;
}
@Override
public Integer getMask() {
return mask;
}
@Override
public String[] getFeatures() {
return features;
}
@Override
public Integer[] getFeatureBits() {
return featureBits;
}
@Override
public String getOptionPropertyType() {
return optionPropertyType;
}
}
package com.keymobile.metadata.metadataRelation.auth;
public enum Range {
METADATA_RELATION("metadataRelation", "关系图谱");
private String id;
private String name;
Range(String id, String name) {
this.id =id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public static String getTypeById(String rangeId) {
for (Range range : Range.values()) {
if (range.getId().equals(rangeId)) return range.getName();
}
return "";
}
}
package com.keymobile.metadata.metadataRelation.controller;
import com.keymobile.auth.common.conf.OptionProvider;
import com.keymobile.metadata.metadataRelation.auth.AuthorityService;
import com.keymobile.metadata.metadataRelation.auth.Feature;
import com.keymobile.metadata.metadataRelation.auth.OptionImpl;
import com.keymobile.metadata.metadataRelation.auth.OptionPropertyDataImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@Api(value = "授权信息相关", tags = "授权信息相关")
@RestController
@RequestMapping(value = "/rest/auth")
public class AuthorityApi implements OptionProvider<OptionImpl, OptionPropertyDataImpl> {
@Autowired
private AuthorityService authorityService;
@Override
@ApiOperation(value = "获取权限属性", notes = "获取权限属性")
@GetMapping(value = "/getOptionProperties")
public List<OptionPropertyDataImpl> getOptionProperties(@RequestParam String range) {
return authorityService.getOptionProperties(range);
}
@Override
@ApiOperation(value = "获取权限数据", notes = "获取权限数据")
@GetMapping(value = "/getOptions")
public List<OptionImpl> getOptions(@RequestParam Long dataRoleId, @RequestParam String range, @RequestParam(required = false) Long domainId) {
return authorityService.getOptions(dataRoleId, range,domainId);
}
@Override
@ApiOperation(value = "保存权限数据", notes = "保存权限数据")
@PostMapping(value = "/saveOptions")
public List<OptionImpl> saveOptions(@RequestParam Long dataRoleId, @RequestParam String range, @RequestBody List<OptionImpl> optionList,@RequestParam(required = false) Long domainId) {
return authorityService.saveOptions(dataRoleId, range, optionList,domainId);
}
@ApiOperation(value = "获取所有的range", notes = "获取所有的range")
@GetMapping(value = "/listAllRanges")
public List<Map<String, Object>> listAllRanges() {
return authorityService.listAllRanges();
}
@ApiOperation(value = "返回可操作按钮权限", notes = "返回可操作按钮权限")
@GetMapping(value = "/getAllowButtons")
public List<Feature> getAllowButtons(@RequestParam String range, @RequestParam String optionId, @RequestParam(required = false) Long domainId) {
return authorityService.getAllowButtons(range,optionId,domainId);
}
}
......@@ -17,7 +17,7 @@ spring:
main:
allow-bean-definition-overriding: true
datasource:
url: jdbc:mysql://192.168.0.192:3306/p0?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
url: jdbc:mysql://dev-vm-00:3306/p0?autoReconnect=true&useUnicode=true&characterEncoding=utf-8
username: p_test
password: p_test
initialization-mode: always
......@@ -25,18 +25,18 @@ spring:
hikari:
maximum-pool-size: 5
redis:
host: 192.168.0.100
host: p-00
port: 6379
eureka:
client:
registerWithEureka: false
registerWithEureka: true
region: default
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://192.168.0.111:8081/eureka/
instance:
prefer-ip-address: true
defaultZone: http://p-01:8081/eureka/
#instance:
# prefer-ip-address: true
security:
permit: false
authUser: root
......@@ -44,7 +44,18 @@ security:
mongodb:
database: szsedev
uri: 192.168.0.192
uri: dev-vm-00
username: szse
password: szse
maxConnectionIdleTime: 10000
management:
metrics:
tags:
application: ${spring.application.name}
endpoints:
health:
show-details: always
web:
exposure:
include: prometheus
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment