From eda653843d01a9cd2d60036d81817bf948309b6f Mon Sep 17 00:00:00 2001 From: Amanda Ariyaratne Date: Fri, 1 Nov 2024 09:57:49 +0530 Subject: [PATCH] add system claim property --- .../mgt/UnifiedClaimMetadataManager.java | 46 +++++++++++++++---- .../metadata/mgt/util/ClaimConstants.java | 1 + .../metadata/mgt/util/ClaimMetadataUtils.java | 4 +- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/UnifiedClaimMetadataManager.java b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/UnifiedClaimMetadataManager.java index c866c4e1a9a6..c8b06ad91a37 100644 --- a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/UnifiedClaimMetadataManager.java +++ b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/UnifiedClaimMetadataManager.java @@ -134,14 +134,19 @@ public List getLocalClaims(int tenantId) throws ClaimMetadataExcepti List localClaimsInSystem = this.systemDefaultClaimMetadataManager.getLocalClaims(tenantId); List localClaimsInDB = this.dbBasedClaimMetadataManager.getLocalClaims(tenantId); - Set claimURIsInDB = localClaimsInDB.stream() - .map(LocalClaim::getClaimURI) - .collect(Collectors.toSet()); - List allLocalClaims = new ArrayList<>(localClaimsInDB); - localClaimsInSystem.stream() - .filter(localClaim -> !claimURIsInDB.contains(localClaim.getClaimURI())) - .forEach(allLocalClaims::add); + localClaimsInSystem.forEach(systemClaim -> { + Optional matchingClaimInDB = allLocalClaims.stream() + .filter(dbClaim -> dbClaim.getClaimURI().equals(systemClaim.getClaimURI())) + .findFirst(); + + if (matchingClaimInDB.isPresent()) { + matchingClaimInDB.get().setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); + } else { + systemClaim.setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); + allLocalClaims.add(systemClaim); + } + }); return allLocalClaims; } @@ -158,9 +163,17 @@ public Optional getLocalClaim(String localClaimURI, int tenantId) th Optional localClaimInDB = this.dbBasedClaimMetadataManager.getLocalClaim(localClaimURI, tenantId); if (localClaimInDB.isPresent()) { + if (isSystemDefaultLocalClaim(localClaimURI, tenantId)) { + localClaimInDB.get().setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); + } return localClaimInDB; } - return this.systemDefaultClaimMetadataManager.getLocalClaim(localClaimURI, tenantId); + Optional localClaimInSystem = this.systemDefaultClaimMetadataManager.getLocalClaim(localClaimURI, tenantId); + if (localClaimInSystem.isPresent()) { + localClaimInSystem.get().setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); + return localClaimInSystem; + } + return Optional.empty(); } /** @@ -172,6 +185,7 @@ public Optional getLocalClaim(String localClaimURI, int tenantId) th */ public void addLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException { + localClaim.getClaimProperties().remove(ClaimConstants.IS_SYSTEM_CLAIM); if (!isClaimDialectInDB(ClaimConstants.LOCAL_CLAIM_DIALECT_URI, tenantId)) { addSystemDefaultDialectToDB(ClaimConstants.LOCAL_CLAIM_DIALECT_URI, tenantId); } @@ -187,6 +201,7 @@ public void addLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetad */ public void updateLocalClaim(LocalClaim localClaim, int tenantId) throws ClaimMetadataException { + localClaim.getClaimProperties().remove(ClaimConstants.IS_SYSTEM_CLAIM); if (isLocalClaimInDB(localClaim.getClaimURI(), tenantId)) { this.dbBasedClaimMetadataManager.updateLocalClaim(localClaim, tenantId); } else { @@ -264,9 +279,11 @@ public List getExternalClaims(String externalClaimDialectURI, int for (ExternalClaim externalClaimInSystem : externalClaimsInSystem) { ExternalClaim matchingClaimInDB = externalClaimsInDBMap.get(externalClaimInSystem.getClaimURI()); if (matchingClaimInDB != null) { + matchingClaimInDB.setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); allExternalClaims.add(matchingClaimInDB); externalClaimsInDBMap.remove(externalClaimInSystem.getClaimURI()); } else { + externalClaimInSystem.setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); allExternalClaims.add(externalClaimInSystem); } } @@ -289,9 +306,18 @@ public Optional getExternalClaim(String externalClaimDialectURI, Optional externalClaim = this.dbBasedClaimMetadataManager.getExternalClaim( externalClaimDialectURI, claimURI, tenantId); if (externalClaim.isPresent()) { + if (isSystemDefaultExternalClaim(externalClaimDialectURI, claimURI, tenantId)) { + externalClaim.get().setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); + } return externalClaim; } - return this.systemDefaultClaimMetadataManager.getExternalClaim(externalClaimDialectURI, claimURI, tenantId); + Optional externalClaimInSystem = this.systemDefaultClaimMetadataManager.getExternalClaim( + externalClaimDialectURI, claimURI, tenantId); + if (externalClaimInSystem.isPresent()) { + externalClaimInSystem.get().setClaimProperty(ClaimConstants.IS_SYSTEM_CLAIM, Boolean.TRUE.toString()); + return externalClaimInSystem; + } + return Optional.empty(); } /** @@ -304,6 +330,7 @@ public Optional getExternalClaim(String externalClaimDialectURI, public void addExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException { + externalClaim.getClaimProperties().remove(ClaimConstants.IS_SYSTEM_CLAIM); if (!isClaimDialectInDB(externalClaim.getClaimDialectURI(), tenantId)) { addSystemDefaultDialectToDB(externalClaim.getClaimDialectURI(), tenantId); } @@ -323,6 +350,7 @@ public void addExternalClaim(ExternalClaim externalClaim, int tenantId) public void updateExternalClaim(ExternalClaim externalClaim, int tenantId) throws ClaimMetadataException { + externalClaim.getClaimProperties().remove(ClaimConstants.IS_SYSTEM_CLAIM); if (isExternalClaimInDB(externalClaim.getClaimURI(), externalClaim.getClaimDialectURI(), tenantId)) { this.dbBasedClaimMetadataManager.updateExternalClaim(externalClaim, tenantId); } else { diff --git a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimConstants.java b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimConstants.java index 0abdf874423d..d2ac03010392 100644 --- a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimConstants.java +++ b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimConstants.java @@ -41,6 +41,7 @@ public class ClaimConstants { public static final String MAPPED_LOCAL_CLAIM_PROPERTY = "MappedLocalClaim"; public static final String MIN_LENGTH = "minLength"; public static final String MAX_LENGTH = "maxLength"; + public static final String IS_SYSTEM_CLAIM = "isSystemClaim"; /** * Enum for error messages. diff --git a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtils.java b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtils.java index 68cae4270081..f75ad03383d4 100644 --- a/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtils.java +++ b/components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/util/ClaimMetadataUtils.java @@ -347,8 +347,7 @@ public static ClaimMapping convertExternalClaimToClaimMapping(ExternalClaim exte claim = createLocalClaim(claimKey, claimMapping, filterClaimProperties(claimConfig.getPropertyHolderMap().get(claimKey))); } else { - claim = createExternalClaim(claimKey, filterClaimProperties(claimConfig.getPropertyHolderMap() - .get(claimKey))); + claim = createExternalClaim(claimKey, claimConfig.getPropertyHolderMap().get(claimKey)); } claims.computeIfAbsent(claimDialectURI, k -> new ArrayList<>()).add(claim); } @@ -361,6 +360,7 @@ public static Map filterClaimProperties(Map clai claimProperties.remove(ClaimConstants.DIALECT_PROPERTY); claimProperties.remove(ClaimConstants.CLAIM_URI_PROPERTY); claimProperties.remove(ClaimConstants.ATTRIBUTE_ID_PROPERTY); + claimProperties.remove(ClaimConstants.IS_SYSTEM_CLAIM); claimProperties.putIfAbsent(ClaimConstants.DISPLAY_NAME_PROPERTY, "0"); claimProperties.computeIfPresent(ClaimConstants.SUPPORTED_BY_DEFAULT_PROPERTY,