@@ -721,47 +721,13 @@ public void writeToPolicyMappingRecords(
721721 PolicyType policyType = PolicyType .fromCode (record .getPolicyTypeCode ());
722722 Preconditions .checkArgument (
723723 policyType != null , "Invalid policy type code: %s" , record .getPolicyTypeCode ());
724- String insertQuery =
724+ String insertPolicyMappingQuery =
725725 generateInsertQuery (
726726 ModelPolicyMappingRecord .fromPolicyMappingRecord (record ), realmId );
727727 if (policyType .isInheritable ()) {
728- List <PolarisPolicyMappingRecord > existingRecords =
729- loadPoliciesOnTargetByType (
730- callCtx ,
731- record .getTargetCatalogId (),
732- record .getTargetId (),
733- record .getPolicyTypeCode ());
734- if (existingRecords .size () > 1 ) {
735- throw new PolicyMappingAlreadyExistsException (existingRecords .getFirst ());
736- } else if (existingRecords .size () == 1 ) {
737- PolarisPolicyMappingRecord existingRecord = existingRecords .getFirst ();
738- if (existingRecord .getPolicyCatalogId () != record .getPolicyCatalogId ()
739- || existingRecord .getPolicyId () != record .getPolicyId ()) {
740- throw new PolicyMappingAlreadyExistsException (existingRecord );
741- }
742- Map <String , Object > updateClause =
743- Map .of (
744- "target_catalog_id" ,
745- record .getTargetCatalogId (),
746- "target_id" ,
747- record .getTargetId (),
748- "policy_type_code" ,
749- record .getPolicyTypeCode (),
750- "policy_id" ,
751- record .getPolicyId (),
752- "policy_catalog_id" ,
753- record .getPolicyCatalogId (),
754- "realm_id" ,
755- realmId );
756- String updateQuery =
757- generateUpdateQuery (
758- ModelPolicyMappingRecord .fromPolicyMappingRecord (record ), updateClause );
759- statement .executeUpdate (updateQuery );
760- } else {
761- statement .executeUpdate (insertQuery );
762- }
728+ return handleInheritablePolicy (callCtx , record , insertPolicyMappingQuery , statement );
763729 } else {
764- statement .executeUpdate (insertQuery );
730+ statement .executeUpdate (insertPolicyMappingQuery );
765731 }
766732 return true ;
767733 });
@@ -771,6 +737,51 @@ public void writeToPolicyMappingRecords(
771737 }
772738 }
773739
740+ private boolean handleInheritablePolicy (
741+ @ Nonnull PolarisCallContext callCtx ,
742+ @ Nonnull PolarisPolicyMappingRecord record ,
743+ @ Nonnull String insertQuery ,
744+ Statement statement )
745+ throws SQLException {
746+ List <PolarisPolicyMappingRecord > existingRecords =
747+ loadPoliciesOnTargetByType (
748+ callCtx , record .getTargetCatalogId (), record .getTargetId (), record .getPolicyTypeCode ());
749+ if (existingRecords .size () > 1 ) {
750+ throw new PolicyMappingAlreadyExistsException (existingRecords .getFirst ());
751+ } else if (existingRecords .size () == 1 ) {
752+ PolarisPolicyMappingRecord existingRecord = existingRecords .getFirst ();
753+ if (existingRecord .getPolicyCatalogId () != record .getPolicyCatalogId ()
754+ || existingRecord .getPolicyId () != record .getPolicyId ()) {
755+ // Only one policy of the same type can be attached to an entity when the policy is
756+ // inheritable.
757+ throw new PolicyMappingAlreadyExistsException (existingRecord );
758+ }
759+ Map <String , Object > updateClause =
760+ Map .of (
761+ "target_catalog_id" ,
762+ record .getTargetCatalogId (),
763+ "target_id" ,
764+ record .getTargetId (),
765+ "policy_type_code" ,
766+ record .getPolicyTypeCode (),
767+ "policy_id" ,
768+ record .getPolicyId (),
769+ "policy_catalog_id" ,
770+ record .getPolicyCatalogId (),
771+ "realm_id" ,
772+ realmId );
773+ // In case of the mapping exist, update the policy mapping with the new parameters.
774+ String updateQuery =
775+ generateUpdateQuery (
776+ ModelPolicyMappingRecord .fromPolicyMappingRecord (record ), updateClause );
777+ statement .executeUpdate (updateQuery );
778+ } else {
779+ // record doesn't exist do an insert.
780+ statement .executeUpdate (insertQuery );
781+ }
782+ return true ;
783+ }
784+
774785 @ Override
775786 public void deleteFromPolicyMappingRecords (
776787 @ Nonnull PolarisCallContext callCtx , @ Nonnull PolarisPolicyMappingRecord record ) {
0 commit comments