This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch 3_0_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/3_0_X by this push:
     new d6cd81ad2d [SYNCOPE-1686] Adding relationship end checks for update
d6cd81ad2d is described below

commit d6cd81ad2d483fde0576b59e9063d55f3496e20c
Author: Francesco Chicchiriccò <[email protected]>
AuthorDate: Mon Dec 2 14:15:58 2024 +0100

    [SYNCOPE-1686] Adding relationship end checks for update
---
 .../java/data/AnyObjectDataBinderImpl.java         | 23 +++++++++----------
 .../provisioning/java/data/UserDataBinderImpl.java | 26 +++++++++-------------
 .../apache/syncope/fit/core/AnyObjectITCase.java   | 25 +++++++++------------
 3 files changed, 32 insertions(+), 42 deletions(-)

diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/AnyObjectDataBinderImpl.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/AnyObjectDataBinderImpl.java
index 89681ab0a0..87cb87b384 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/AnyObjectDataBinderImpl.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/AnyObjectDataBinderImpl.java
@@ -235,18 +235,12 @@ public class AnyObjectDataBinderImpl extends 
AbstractAnyDataBinder implements An
                 if (otherEnd == null) {
                     LOG.debug("Ignoring invalid anyObject {}", 
relationshipTO.getOtherEndKey());
                 } else if (relationshipTO.getEnd() == 
RelationshipTO.End.RIGHT) {
-                    LOG.error("Invalid relationship end: {} is not allowed for 
this operation",
-                            relationshipTO.getEnd());
-                    SyncopeClientException assigned =
+                    SyncopeClientException noRight =
                             
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
-                    assigned.getElements().add("Invalid relationship end: "
-                                               + relationshipTO.getEnd()
-                                               + " is not allowed for this 
operation");
-                    scce.addException(assigned);
+                    noRight.getElements().add(
+                            "Relationships shall be created or updated only 
from their left end");
+                    scce.addException(noRight);
                 } else if (relationships.contains(Pair.of(otherEnd.getKey(), 
relationshipTO.getType()))) {
-                    LOG.error("{} was already in relationship {} with {}",
-                            otherEnd, relationshipTO.getType(), anyObject);
-
                     SyncopeClientException assigned =
                             
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                     assigned.getElements().add("AnyObject was already in 
relationship "
@@ -365,12 +359,15 @@ public class AnyObjectDataBinderImpl extends 
AbstractAnyDataBinder implements An
                         AnyObject otherEnd = 
anyObjectDAO.find(patch.getRelationshipTO().getOtherEndKey());
                         if (otherEnd == null) {
                             LOG.debug("Ignoring invalid any object {}", 
patch.getRelationshipTO().getOtherEndKey());
+                        } else if (patch.getRelationshipTO().getEnd() == 
RelationshipTO.End.RIGHT) {
+                            SyncopeClientException noRight =
+                                    
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
+                            noRight.getElements().add(
+                                    "Relationships shall be created or updated 
only from their left end");
+                            scce.addException(noRight);
                         } else if (relationships.contains(
                                 Pair.of(otherEnd.getKey(), 
patch.getRelationshipTO().getType()))) {
 
-                            LOG.error("{} was already in relationship {} with 
{}",
-                                    anyObject, 
patch.getRelationshipTO().getType(), otherEnd);
-
                             SyncopeClientException assigned =
                                     
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                             assigned.getElements().add("AnyObject was already 
in relationship "
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
index a72b5451f1..60027eb448 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/UserDataBinderImpl.java
@@ -357,17 +357,12 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
             if (otherEnd == null) {
                 LOG.debug("Ignoring invalid anyObject {}", 
relationshipTO.getOtherEndKey());
             } else if (relationshipTO.getEnd() == RelationshipTO.End.RIGHT) {
-                LOG.error("Invalid relationship end: {} is not allowed for 
this operation",
-                        relationshipTO.getEnd());
-                SyncopeClientException assigned =
+                SyncopeClientException noRight =
                         
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
-                assigned.getElements().add("Invalid relationship end: "
-                                           + relationshipTO.getEnd()
-                                           + " is not allowed for this 
operation");
-                scce.addException(assigned);
+                noRight.getElements().add(
+                        "Relationships shall be created or updated only from 
their left end");
+                scce.addException(noRight);
             } else if (relationships.contains(Pair.of(otherEnd.getKey(), 
relationshipTO.getType()))) {
-                LOG.error("{} was already in relationship {} with {}", 
otherEnd, relationshipTO.getType(), user);
-
                 SyncopeClientException assigned =
                         
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                 assigned.getElements().add(otherEnd.getType().getKey() + " " + 
otherEnd.getName()
@@ -388,8 +383,6 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
                     user.add(relationship);
                 }
             } else {
-                LOG.error("{} cannot be related to {}", otherEnd, user);
-
                 SyncopeClientException unrelatable =
                         
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                 unrelatable.getElements().add(otherEnd.getType().getKey() + " 
" + otherEnd.getName()
@@ -568,15 +561,18 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
                     } else if (relationships.contains(
                             Pair.of(otherEnd.getKey(), 
patch.getRelationshipTO().getType()))) {
 
-                        LOG.error("{} was already in relationship {} with {}",
-                                user, patch.getRelationshipTO().getType(), 
otherEnd);
-
                         SyncopeClientException assigned =
                                 
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
                         assigned.getElements().add("User was already in 
relationship "
                                 + patch.getRelationshipTO().getType() + " with 
"
                                 + otherEnd.getType().getKey() + " " + 
otherEnd.getName());
                         scce.addException(assigned);
+                    } else if (patch.getRelationshipTO().getEnd() == 
RelationshipTO.End.RIGHT) {
+                        SyncopeClientException noRight =
+                                
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
+                        noRight.getElements().add(
+                                "Relationships shall be created or updated 
only from their left end");
+                        scce.addException(noRight);
                     } else if 
(user.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
                         relationships.add(Pair.of(otherEnd.getKey(), 
patch.getRelationshipTO().getType()));
 
@@ -840,7 +836,7 @@ public class UserDataBinderImpl extends 
AbstractAnyDataBinder implements UserDat
             // relationships
             userTO.getRelationships().addAll(user.getRelationships().stream().
                     map(relationship -> 
getRelationshipTO(relationship.getType().getKey(),
-                            RelationshipTO.End.LEFT, 
relationship.getRightEnd())).
+                    RelationshipTO.End.LEFT, relationship.getRightEnd())).
                     collect(Collectors.toList()));
 
             // memberships
diff --git 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/AnyObjectITCase.java
 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/AnyObjectITCase.java
index c4d288b8ce..cdc7d8738d 100644
--- 
a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/AnyObjectITCase.java
+++ 
b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/AnyObjectITCase.java
@@ -259,17 +259,14 @@ public class AnyObjectITCase extends AbstractITCase {
     public void issueSYNCOPE1686() {
         // Create printers
         AnyObjectCR printer1CR = getSample("printer1");
-        printer1CR.setRealm("/");
         printer1CR.getResources().clear();
         String key1 = createAnyObject(printer1CR).getEntity().getKey();
 
         AnyObjectCR printer2CR = getSample("printer2");
-        printer2CR.setRealm("/");
         printer2CR.getResources().clear();
         String key2 = createAnyObject(printer2CR).getEntity().getKey();
 
         AnyObjectCR printer3CR = getSample("printer3");
-        printer3CR.setRealm("/");
         printer3CR.getResources().clear();
         String key3 = createAnyObject(printer3CR).getEntity().getKey();
 
@@ -303,14 +300,14 @@ public class AnyObjectITCase extends AbstractITCase {
         assertEquals(2, printer2.getRelationships().size());
         assertTrue(printer2.getRelationships().stream()
                 .anyMatch(r -> r.getEnd() == RelationshipTO.End.LEFT
-                               && printer3.getKey().equals(r.getOtherEndKey())
-                               && 
printer3.getType().equals(r.getOtherEndType())
-                               && 
printer3.getName().equals(r.getOtherEndName())));
+                && printer3.getKey().equals(r.getOtherEndKey())
+                && printer3.getType().equals(r.getOtherEndType())
+                && printer3.getName().equals(r.getOtherEndName())));
         assertTrue(printer2.getRelationships().stream()
                 .anyMatch(r -> r.getEnd() == RelationshipTO.End.RIGHT
-                               && printer1.getKey().equals(r.getOtherEndKey())
-                               && 
printer1.getType().equals(r.getOtherEndType())
-                               && 
printer1.getName().equals(r.getOtherEndName())));
+                && printer1.getKey().equals(r.getOtherEndKey())
+                && printer1.getType().equals(r.getOtherEndType())
+                && printer1.getName().equals(r.getOtherEndName())));
 
         // Verify relationships for printer3
         assertEquals(1, printer3.getRelationships().size());
@@ -322,14 +319,14 @@ public class AnyObjectITCase extends AbstractITCase {
 
         // Test invalid relationship with End.RIGHT
         AnyObjectCR printer4CR = getSample("printer4");
-        printer4CR.setRealm("/");
         printer4CR.getResources().clear();
-        printer4CR.getRelationships().add(new 
RelationshipTO.Builder("neighborhood", RelationshipTO.End.RIGHT)
-                .otherEnd(PRINTER, key1).build());
+        printer4CR.getRelationships().add(
+                new RelationshipTO.Builder("neighborhood", 
RelationshipTO.End.RIGHT).otherEnd(PRINTER, key1).build());
 
         SyncopeClientException exception =
                 assertThrows(SyncopeClientException.class, () -> 
createAnyObject(printer4CR));
-        assertEquals("InvalidRelationship [Invalid relationship end: RIGHT is 
not allowed for this operation]",
-                exception.getMessage());
+        assertEquals(ClientExceptionType.InvalidRelationship, 
exception.getType());
+        assertTrue(exception.getMessage().
+                contains("Relationships shall be created or updated only from 
their left end"));
     }
 }

Reply via email to