Skip to content

Commit

Permalink
Add attribute profile related logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
PasinduYeshan committed Jan 6, 2025
1 parent 26dff4f commit b0ce7ba
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,5 @@ public String toString() {
public static final String PROP_REQUIRED = "Required";
public static final String PROP_SUPPORTED_BY_DEFAULT = "SupportedByDefault";
public static final String PROP_UNIQUENESS_SCOPE = "UniquenessScope";
public static final String PROP_PROFILES_PREFIX = "Profiles.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -55,6 +56,8 @@
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.dto.LinkDTO;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.dto.LocalClaimReqDTO;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.dto.LocalClaimResDTO;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.dto.ProfileAttributesDTO;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.dto.ProfilesDTO;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.dto.PropertyDTO;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.model.ClaimDialectConfiguration;
import org.wso2.carbon.identity.rest.api.server.claim.management.v1.model.ClaimErrorDTO;
Expand Down Expand Up @@ -126,6 +129,7 @@
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_DESCRIPTION;
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_DISPLAY_NAME;
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_DISPLAY_ORDER;
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_PROFILES_PREFIX;
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_READ_ONLY;
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_REG_EX;
import static org.wso2.carbon.identity.api.server.claim.management.common.Constant.PROP_REQUIRED;
Expand Down Expand Up @@ -358,7 +362,6 @@ public String addLocalClaim(LocalClaimReqDTO localClaimReqDTO) {
*/
public void deleteLocalClaim(String claimId) {


String claimURI;
try {
validateClaimModificationEligibility();
Expand Down Expand Up @@ -468,10 +471,10 @@ public void updateLocalClaim(String claimId, LocalClaimReqDTO localClaimReqDTO)
/**
* Updates a claim dialect with related claims from an uploaded file.
*
* @param fileInputStream InputStream representing the uploaded claim dialect file.
* @param fileDetail Attachment object with metadata about the uploaded claim dialect file.
* @param preserveClaims Boolean value to indicate whether to merge and preserve the existing claims
* or completely replace the existing claims set.
* @param fileInputStream InputStream representing the uploaded claim dialect file.
* @param fileDetail Attachment object with metadata about the uploaded claim dialect file.
* @param preserveClaims Boolean value to indicate whether to merge and preserve the existing claims
* or completely replace the existing claims set.
* @return a String representing the updated claim dialect's resource identifier.
*/
public String updateClaimDialectFromFile(InputStream fileInputStream, Attachment fileDetail,
Expand Down Expand Up @@ -533,12 +536,12 @@ private void updateLocalClaims(List<LocalClaimReqDTO> localClaimReqDTOList, bool
private void deleteObsoleteLocalClaims(List<LocalClaimReqDTO> localClaimReqDTOList, List<ClaimErrorDTO> errors)
throws ClaimMetadataException {

List<String> claimsToDelete = getLocalClaimResDTOs(getClaimMetadataManagementService()
.getLocalClaims(ContextLoader.getTenantDomainFromContext())).stream()
.map(LocalClaimResDTO::getClaimURI)
.filter(claimURI -> localClaimReqDTOList.stream()
.noneMatch(reqDTO -> reqDTO.getClaimURI().equals(claimURI)))
.collect(Collectors.toList());
List<String> claimsToDelete = getLocalClaimResDTOs(getClaimMetadataManagementService()
.getLocalClaims(ContextLoader.getTenantDomainFromContext())).stream()
.map(LocalClaimResDTO::getClaimURI)
.filter(claimURI -> localClaimReqDTOList.stream()
.noneMatch(reqDTO -> reqDTO.getClaimURI().equals(claimURI)))
.collect(Collectors.toList());

for (String claimURI : claimsToDelete) {
try {
Expand Down Expand Up @@ -590,8 +593,8 @@ private void updateExternalClaims(String dialectId, List<ExternalClaimReqDTO> ex
/**
* Exports a claim dialect with related claims in the specified file type.
*
* @param dialectId ID of the claim dialect to export.
* @param fileType Type of file to export the claim dialect to.
* @param dialectId ID of the claim dialect to export.
* @param fileType Type of file to export the claim dialect to.
* @return a FileContent object representing the exported claim dialect file.
*/
public FileContent exportClaimDialectToFile(String dialectId, String fileType) {
Expand All @@ -601,7 +604,7 @@ public FileContent exportClaimDialectToFile(String dialectId, String fileType) {
}
if (StringUtils.isBlank(fileType)) {
throw handleClaimManagementClientError(Constant.ErrorMessage.ERROR_CODE_MISSING_MEDIA_TYPE, BAD_REQUEST,
dialectId);
dialectId);
}

ClaimDialectConfiguration dialectConfiguration = new ClaimDialectConfiguration(getClaimDialect(dialectId));
Expand All @@ -617,7 +620,7 @@ public FileContent exportClaimDialectToFile(String dialectId, String fileType) {
List<ExternalClaim> externalClaimList = getClaimMetadataManagementService().getExternalClaims(
base64DecodeId(dialectId),
ContextLoader.getTenantDomainFromContext());
List<ExternalClaimResDTO> externalClaimResDTOList = getExternalClaimResDTOs(externalClaimList);
List<ExternalClaimResDTO> externalClaimResDTOList = getExternalClaimResDTOs(externalClaimList);
claimResDTOList.addAll(externalClaimResDTOList);
dialectConfiguration.setClaims(claimResDTOList);
}
Expand Down Expand Up @@ -1011,6 +1014,8 @@ private LocalClaimResDTO getLocalClaimResDTO(LocalClaim localClaim) {
}
}

addAttributeProfilesToLocalClaimRes(claimProperties, localClaimResDTO);

List<AttributeMappingDTO> attributeMappingDTOs = new ArrayList<>();
for (AttributeMapping attributeMapping : localClaim.getMappedAttributes()) {
AttributeMappingDTO attributeMappingDTO = new AttributeMappingDTO();
Expand All @@ -1026,6 +1031,51 @@ private LocalClaimResDTO getLocalClaimResDTO(LocalClaim localClaim) {
return localClaimResDTO;
}

/**
* Add attribute profiles to LocalClaimResDTO.
*
* @param claimProperties Claim properties.
* @param localClaimResDTO Local claim response DTO.
*/
private void addAttributeProfilesToLocalClaimRes(Map<String, String> claimProperties,
LocalClaimResDTO localClaimResDTO) {

if (MapUtils.isEmpty(claimProperties)) {
return;
}
ProfilesDTO attributeProfiles = new ProfilesDTO();
claimProperties.forEach((propertyKey, propertyValue) -> {
if (StringUtils.isBlank(propertyKey) || StringUtils.isBlank(propertyValue)) {
return;
}
if (!StringUtils.startsWithIgnoreCase(propertyKey, PROP_PROFILES_PREFIX)) {
return;
}
String[] propertyKeyArray = propertyKey.split("\\.");
if (propertyKeyArray.length != 3) {
return;
}
String profileName = propertyKeyArray[1];
String claimPropertyName = propertyKeyArray[2];

ProfileAttributesDTO profileAttributes =
attributeProfiles.computeIfAbsent(profileName, k -> new ProfileAttributesDTO());

switch (claimPropertyName) {
case PROP_READ_ONLY:
profileAttributes.setReadOnly(Boolean.valueOf(propertyValue));
break;
case PROP_REQUIRED:
profileAttributes.setRequired(Boolean.valueOf(propertyValue));
break;
case PROP_SUPPORTED_BY_DEFAULT:
profileAttributes.setSupportedByDefault(Boolean.valueOf(propertyValue));
break;
}
});
localClaimResDTO.setProfiles(attributeProfiles);
}

private List<LocalClaimResDTO> getLocalClaimResDTOs(List<LocalClaim> localClaimList) {

List<LocalClaimResDTO> localClaimResDTOList = new ArrayList<>();
Expand Down Expand Up @@ -1063,6 +1113,8 @@ private LocalClaim createLocalClaim(LocalClaimReqDTO localClaimReqDTO) {
claimProperties.put(PROP_UNIQUENESS_SCOPE, localClaimReqDTO.getUniquenessScope().toString());
}

addAttributeProfilesToClaimProperties(localClaimReqDTO.getProfiles(), claimProperties);

claimProperties.put(PROP_READ_ONLY, String.valueOf(localClaimReqDTO.getReadOnly()));
claimProperties.put(PROP_REQUIRED, String.valueOf(localClaimReqDTO.getRequired()));
claimProperties.put(PROP_SUPPORTED_BY_DEFAULT, String.valueOf(localClaimReqDTO.getSupportedByDefault()));
Expand All @@ -1077,6 +1129,36 @@ private LocalClaim createLocalClaim(LocalClaimReqDTO localClaimReqDTO) {
return new LocalClaim(localClaimReqDTO.getClaimURI(), attributeMappings, claimProperties);
}

/**
* Add profile attributes to claim properties.
*
* @param attributeProfiles - Profile attributes.
* @param claimProperties - Claim properties.
*/
private void addAttributeProfilesToClaimProperties(Map<String, ProfileAttributesDTO> attributeProfiles,
Map<String, String> claimProperties) {

if (MapUtils.isEmpty(attributeProfiles)) {
return;
}
attributeProfiles.forEach((profileName, profileAttributes) -> {
addProfileAttributeValue(claimProperties, profileName, PROP_READ_ONLY, profileAttributes.getReadOnly());
addProfileAttributeValue(claimProperties, profileName, PROP_REQUIRED, profileAttributes.getRequired());
addProfileAttributeValue(claimProperties, profileName, PROP_SUPPORTED_BY_DEFAULT,
profileAttributes.getSupportedByDefault());
});
}

private void addProfileAttributeValue(Map<String, String> claimProperties, String profileName, String propertyKey,
Object propertyValue) {

if (propertyValue == null) {
return;
}
String claimPropertyKey = PROP_PROFILES_PREFIX + profileName + "." + propertyKey;
claimProperties.put(claimPropertyKey, String.valueOf(propertyValue));
}

/**
* Import a claim dialect with related claims from an uploaded file.
*
Expand Down Expand Up @@ -1110,6 +1192,7 @@ public String importClaimDialectFromFile(InputStream fileInputStream, Attachment
throw handleClaimManagementException(e, Constant.ErrorMessage.ERROR_CODE_ERROR_IMPORTING_CLAIM_DIALECT);
}
}

private void importExternalClaims(String dialectID, List<ExternalClaimReqDTO> externalClaimReqDTOList) {

List<ClaimErrorDTO> errors = new ArrayList<>();
Expand Down

0 comments on commit b0ce7ba

Please sign in to comment.