use of org.wso2.carbon.identity.entitlement.dto.EntitledAttributesDTO in project carbon-identity-framework by wso2.
the class PolicySearch method getEntitledAttributes.
/**
* gets all entitled attributes for given set of attributes
* this an universal method to do policy search and find entitlement attributes
*
* @param identifier identifier to separate out the attributes that is used for search
* this is not required and can be null
* @param givenAttributes user provided attributes
* @return all the attributes that is entitled
*/
public EntitledResultSetDTO getEntitledAttributes(String identifier, AttributeDTO[] givenAttributes) {
String cacheKey = "";
if (cachingEnable) {
int hashCode = 0;
for (AttributeDTO dto : givenAttributes) {
hashCode = hashCode + (31 * dto.hashCode());
}
cacheKey = identifier + hashCode;
SearchResult searchResult = policySearchCache.getFromCache(cacheKey);
if (searchResult != null) {
if (log.isDebugEnabled()) {
log.debug("PDP Search Cache Hit");
}
return searchResult.getResultSetDTO();
} else {
if (log.isDebugEnabled()) {
log.debug("PDP Search Cache Miss");
}
}
}
EntitledResultSetDTO result = new EntitledResultSetDTO();
Set<EntitledAttributesDTO> resultAttributes = new HashSet<EntitledAttributesDTO>();
Set<AttributeDTO> attributeDTOs = new HashSet<AttributeDTO>(Arrays.asList(givenAttributes));
for (PolicyFinderModule finderModule : finderModules) {
Map<String, Set<AttributeDTO>> attributesMap = finderModule.getSearchAttributes(identifier, attributeDTOs);
int supportedSearchScheme = finderModule.getSupportedSearchAttributesScheme();
Set<List<AttributeDTO>> requestSet = getPossibleRequests(attributesMap, supportedSearchScheme);
if (requestSet == null) {
log.error("Invalid Search scheme in policy finder : " + finderModule.getModuleName());
} else {
for (List<AttributeDTO> attributeDTOList : requestSet) {
if (getResponse(attributeDTOList)) {
EntitledAttributesDTO dto = new EntitledAttributesDTO();
dto.setAttributeDTOs(attributeDTOList.toArray(new AttributeDTO[attributeDTOList.size()]));
resultAttributes.add(dto);
}
}
}
}
result.setAdvanceResult(true);
result.setEntitledAttributesDTOs(resultAttributes.toArray(new EntitledAttributesDTO[resultAttributes.size()]));
if (cachingEnable) {
SearchResult searchResult = new SearchResult();
searchResult.setResultSetDTO(result);
policySearchCache.addToCache(cacheKey, searchResult);
if (log.isDebugEnabled()) {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
log.debug("PDP Decision Cache Updated for tenantId " + tenantId);
}
}
return result;
}
use of org.wso2.carbon.identity.entitlement.dto.EntitledAttributesDTO in project carbon-identity-framework by wso2.
the class PolicySearch method getEntitledAttributes.
/**
* This returns resource name as the list of the entitled attributes for given
* user or role and action, after evaluating the all the active policies in the PDP
*
* @param subjectName subject name
* @param resourceName resource name
* @param subjectId subject attribute Id
* @param action Action Name
* @param enableChildSearch whether search is done for the child resources under the given resource name
* @return entitled resource id set
* @throws EntitlementException throws
*/
public EntitledResultSetDTO getEntitledAttributes(String subjectName, String resourceName, String subjectId, String action, boolean enableChildSearch) throws EntitlementException {
String cacheKey = "";
if (cachingEnable) {
cacheKey = (subjectId != null ? subjectId : "") + (subjectName != null ? subjectName : "") + (resourceName != null ? resourceName : "") + (action != null ? action : "") + enableChildSearch;
SearchResult searchResult = policySearchCache.getFromCache(cacheKey);
if (searchResult != null) {
return searchResult.getResultSetDTO();
}
}
AttributeDTO subjectAttributeDTO;
boolean hierarchicalResource = false;
EntitledResultSetDTO resultSetDTO = new EntitledResultSetDTO();
Set<EntitledAttributesDTO> resultSet = new HashSet<EntitledAttributesDTO>();
if (subjectName != null && subjectName.trim().length() > 0) {
subjectAttributeDTO = new AttributeDTO();
subjectAttributeDTO.setCategory(PDPConstants.SUBJECT_CATEGORY_URI);
subjectAttributeDTO.setAttributeValue(subjectName);
subjectAttributeDTO.setAttributeDataType(PDPConstants.STRING_DATA_TYPE);
if (subjectId != null && subjectId.trim().length() > 0) {
subjectAttributeDTO.setAttributeId(subjectId);
} else {
subjectAttributeDTO.setAttributeId(PDPConstants.SUBJECT_ID_DEFAULT);
}
} else {
throw new EntitlementException("Error : subject value can not be null");
}
if (getResponse(Arrays.asList(subjectAttributeDTO))) {
EntitledAttributesDTO dto = new EntitledAttributesDTO();
dto.setAllActions(true);
dto.setAllResources(true);
EntitledResultSetDTO setDTO = new EntitledResultSetDTO();
setDTO.setEntitledAttributesDTOs(new EntitledAttributesDTO[] { dto });
return setDTO;
}
for (PolicyFinderModule module : finderModules) {
if (module.isDefaultCategoriesSupported() && PolicyFinderModule.COMBINATIONS_BY_CATEGORY_AND_PARAMETER == module.getSupportedSearchAttributesScheme()) {
Map<String, Set<AttributeDTO>> requestMap = module.getSearchAttributes(null, new HashSet<AttributeDTO>(Arrays.asList(subjectAttributeDTO)));
for (Map.Entry<String, Set<AttributeDTO>> entry : requestMap.entrySet()) {
Set<AttributeDTO> attributeDTOs = entry.getValue();
if (attributeDTOs != null) {
Set<AttributeDTO> actions = new HashSet<AttributeDTO>();
Set<AttributeDTO> resources = new HashSet<AttributeDTO>();
Set<AttributeDTO> requestAttributes = new HashSet<AttributeDTO>();
if (resourceName != null && resourceName.trim().length() > 0) {
AttributeDTO resourceAttribute = new AttributeDTO();
resourceAttribute.setAttributeValue(resourceName);
resourceAttribute.setAttributeDataType(PDPConstants.STRING_DATA_TYPE);
resourceAttribute.setAttributeId(PDPConstants.RESOURCE_ID_DEFAULT);
resourceAttribute.setCategory(PDPConstants.RESOURCE_CATEGORY_URI);
resources.add(resourceAttribute);
hierarchicalResource = true;
}
AttributeDTO resourceScopeAttribute = new AttributeDTO();
resourceScopeAttribute.setAttributeValue(PDPConstants.RESOURCE_DESCENDANTS);
resourceScopeAttribute.setAttributeDataType(PDPConstants.STRING_DATA_TYPE);
resourceScopeAttribute.setAttributeId(PDPConstants.RESOURCE_SCOPE_ID);
resourceScopeAttribute.setCategory(PDPConstants.RESOURCE_CATEGORY_URI);
for (AttributeDTO attributeDTO : attributeDTOs) {
if (PDPConstants.ENVIRONMENT_CATEGORY_URI.equals(attributeDTO.getCategory()) || PDPConstants.ENVIRONMENT_ELEMENT.equals(attributeDTO.getCategory())) {
requestAttributes.add(attributeDTO);
attributeDTO.setAttributeId(PDPConstants.ENVIRONMENT_ID_DEFAULT);
requestAttributes.add(attributeDTO);
} else if (PDPConstants.ACTION_CATEGORY_URI.equals(attributeDTO.getCategory()) || PDPConstants.ACTION_ELEMENT.equals(attributeDTO.getCategory())) {
if (action != null && action.trim().length() > 0) {
attributeDTO.setAttributeValue(action);
}
actions.add(attributeDTO);
attributeDTO.setAttributeId(PDPConstants.ACTION_ID_DEFAULT);
actions.add(attributeDTO);
} else if ((PDPConstants.RESOURCE_CATEGORY_URI.equals(attributeDTO.getCategory()) || PDPConstants.RESOURCE_ELEMENT.equals(attributeDTO.getCategory())) && !hierarchicalResource) {
attributeDTO.setAttributeId(PDPConstants.RESOURCE_ID_DEFAULT);
resources.add(attributeDTO);
}
}
if (resultSetDTO.getMessage() == null) {
List<String> entitledActions = new ArrayList<String>();
for (AttributeDTO actionDTO : actions) {
List<AttributeDTO> currentRequestAttributes = new ArrayList<AttributeDTO>();
currentRequestAttributes.add(subjectAttributeDTO);
currentRequestAttributes.add(actionDTO);
if (getResponse(currentRequestAttributes)) {
EntitledAttributesDTO dto = new EntitledAttributesDTO();
dto.setAllResources(true);
dto.setAction(actionDTO.getAttributeValue());
resultSet.add(dto);
entitledActions.add(actionDTO.getAttributeValue());
}
}
for (AttributeDTO resource : resources) {
if (PDPConstants.RESOURCE_CATEGORY_URI.equals(resource.getCategory()) || PDPConstants.RESOURCE_ELEMENT.equals(resource.getCategory())) {
boolean allActionsAllowed = false;
int noOfRequests = 1;
if (enableChildSearch) {
noOfRequests = 0;
}
while (noOfRequests < 2) {
List<AttributeDTO> currentRequestAttributes = new ArrayList<AttributeDTO>();
for (AttributeDTO dto : requestAttributes) {
currentRequestAttributes.add(dto);
}
if (noOfRequests < 1) {
currentRequestAttributes.add(resourceScopeAttribute);
}
currentRequestAttributes.add(subjectAttributeDTO);
currentRequestAttributes.add(resource);
if (getResponse(currentRequestAttributes)) {
EntitledAttributesDTO dto = new EntitledAttributesDTO();
dto.setResourceName(resource.getAttributeValue());
dto.setAllActions(true);
resultSet.add(dto);
allActionsAllowed = true;
}
noOfRequests++;
}
if (allActionsAllowed) {
continue;
}
for (AttributeDTO actionAttributeDTO : actions) {
if (entitledActions.contains(actionAttributeDTO.getAttributeValue())) {
continue;
}
noOfRequests = 1;
if (enableChildSearch) {
noOfRequests = 0;
}
while (noOfRequests < 2) {
List<AttributeDTO> currentRequestAttributes = new ArrayList<AttributeDTO>();
for (AttributeDTO dto : requestAttributes) {
currentRequestAttributes.add(dto);
}
if (noOfRequests < 1) {
currentRequestAttributes.add(resourceScopeAttribute);
}
currentRequestAttributes.add(subjectAttributeDTO);
currentRequestAttributes.add(resource);
currentRequestAttributes.add(actionAttributeDTO);
if (getResponse(currentRequestAttributes)) {
EntitledAttributesDTO dto = new EntitledAttributesDTO();
dto.setResourceName(resource.getAttributeValue());
dto.setAction(actionAttributeDTO.getAttributeValue());
resultSet.add(dto);
}
noOfRequests++;
}
}
}
}
}
}
}
}
}
resultSetDTO.setEntitledAttributesDTOs(resultSet.toArray(new EntitledAttributesDTO[resultSet.size()]));
if (cachingEnable) {
SearchResult result = new SearchResult();
result.setResultSetDTO(resultSetDTO);
policySearchCache.addToCache(cacheKey, result);
if (log.isDebugEnabled()) {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
log.debug("PDP Decision Cache Updated for tenantId " + tenantId);
}
}
return resultSetDTO;
}
Aggregations