[SYNCOPE-1228] Graceful handling of parent info for create

Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/edb004d8
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/edb004d8
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/edb004d8

Branch: refs/heads/2_0_X
Commit: edb004d8da264aae453ef3b46ca37cd9d5d4d3e9
Parents: c3acdc3
Author: Francesco Chicchiriccò <ilgro...@apache.org>
Authored: Thu Nov 2 15:36:11 2017 +0100
Committer: Francesco Chicchiriccò <ilgro...@apache.org>
Committed: Thu Nov 2 15:36:11 2017 +0100

----------------------------------------------------------------------
 .../apache/syncope/core/logic/RealmLogic.java   | 29 ++++++++++++++++++--
 .../provisioning/api/data/RealmDataBinder.java  |  2 +-
 .../java/data/RealmDataBinderImpl.java          |  4 +--
 .../pushpull/DefaultRealmPullResultHandler.java |  2 +-
 4 files changed, 31 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/edb004d8/core/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
----------------------------------------------------------------------
diff --git 
a/core/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java 
b/core/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
index e82e495..261cb42 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/RealmLogic.java
@@ -92,12 +92,37 @@ public class RealmLogic extends 
AbstractTransactionalLogic<RealmTO> {
 
     @PreAuthorize("hasRole('" + StandardEntitlement.REALM_CREATE + "')")
     public ProvisioningResult<RealmTO> create(final String parentPath, final 
RealmTO realmTO) {
-        String fullPath = StringUtils.appendIfMissing(parentPath, "/") + 
realmTO.getName();
+        Realm parent;
+        if (StringUtils.isBlank(realmTO.getParent())) {
+            parent = realmDAO.findByFullPath(parentPath);
+            if (parent == null) {
+                LOG.error("Could not find parent realm " + parentPath);
+
+                throw new NotFoundException(parentPath);
+            }
+
+            realmTO.setParent(parent.getFullPath());
+        } else {
+            parent = realmDAO.find(realmTO.getParent());
+            if (parent == null) {
+                LOG.error("Could not find parent realm " + 
realmTO.getParent());
+
+                throw new NotFoundException(realmTO.getParent());
+            }
+
+            if (!parent.getFullPath().equals(parentPath)) {
+                SyncopeClientException sce = 
SyncopeClientException.build(ClientExceptionType.InvalidPath);
+                sce.getElements().add("Mismatching parent realm: " + 
parentPath + " Vs " + parent.getFullPath());
+                throw sce;
+            }
+        }
+
+        String fullPath = StringUtils.appendIfMissing(parent.getFullPath(), 
"/") + realmTO.getName();
         if (realmDAO.findByFullPath(fullPath) != null) {
             throw new DuplicateException(fullPath);
         }
 
-        Realm realm = realmDAO.save(binder.create(parentPath, realmTO));
+        Realm realm = realmDAO.save(binder.create(parent, realmTO));
 
         PropagationByResource propByRes = new PropagationByResource();
         for (String resource : realm.getResourceKeys()) {

http://git-wip-us.apache.org/repos/asf/syncope/blob/edb004d8/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/RealmDataBinder.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/RealmDataBinder.java
 
b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/RealmDataBinder.java
index 44e00fc..1e3183c 100644
--- 
a/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/RealmDataBinder.java
+++ 
b/core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/data/RealmDataBinder.java
@@ -24,7 +24,7 @@ import org.apache.syncope.core.persistence.api.entity.Realm;
 
 public interface RealmDataBinder {
 
-    Realm create(String parentPath, RealmTO realmTO);
+    Realm create(Realm parent, RealmTO realmTO);
 
     PropagationByResource update(Realm realm, RealmTO realmTO);
 

http://git-wip-us.apache.org/repos/asf/syncope/blob/edb004d8/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java
index 32678cc..4f7eda9 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/data/RealmDataBinderImpl.java
@@ -100,11 +100,11 @@ public class RealmDataBinderImpl implements 
RealmDataBinder {
     }
 
     @Override
-    public Realm create(final String parentPath, final RealmTO realmTO) {
+    public Realm create(final Realm parent, final RealmTO realmTO) {
         Realm realm = entityFactory.newEntity(Realm.class);
 
         realm.setName(realmTO.getName());
-        realm.setParent(realmDAO.findByFullPath(parentPath));
+        realm.setParent(parent);
 
         if (realmTO.getPasswordPolicy() != null) {
             Policy policy = policyDAO.find(realmTO.getPasswordPolicy());

http://git-wip-us.apache.org/repos/asf/syncope/blob/edb004d8/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/DefaultRealmPullResultHandler.java
----------------------------------------------------------------------
diff --git 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/DefaultRealmPullResultHandler.java
 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/DefaultRealmPullResultHandler.java
index 69c0604..d4a54e7 100644
--- 
a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/DefaultRealmPullResultHandler.java
+++ 
b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/pushpull/DefaultRealmPullResultHandler.java
@@ -232,7 +232,7 @@ public class DefaultRealmPullResultHandler
         Result resultStatus;
 
         try {
-            Realm realm = 
realmDAO.save(binder.create(profile.getTask().getDestinatioRealm().getFullPath(),
 realmTO));
+            Realm realm = 
realmDAO.save(binder.create(profile.getTask().getDestinatioRealm(), realmTO));
 
             PropagationByResource propByRes = new PropagationByResource();
             for (String resource : realm.getResourceKeys()) {

Reply via email to