You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2016/04/07 10:40:37 UTC

[2/2] syncope git commit: Enhancing checks when deleting a realm containig users, groups or any objects

Enhancing checks when deleting a realm containig users, groups or any objects


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

Branch: refs/heads/master
Commit: 0016d10c90659102986d9eb0405c56d6f1ee32f9
Parents: 27dae83
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Thu Apr 7 10:40:17 2016 +0200
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Thu Apr 7 10:40:17 2016 +0200

----------------------------------------------------------------------
 .../common/lib/types/ClientExceptionType.java   |  1 +
 .../syncope/core/logic/ConnectorLogic.java      | 20 +++++++-------
 .../apache/syncope/core/logic/RealmLogic.java   | 28 ++++++++++++++++++++
 .../apache/syncope/fit/core/RealmITCase.java    | 11 ++++++++
 4 files changed, 49 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/0016d10c/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
----------------------------------------------------------------------
diff --git a/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java b/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
index bceff27..951a4ca 100644
--- a/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
+++ b/common/lib/src/main/java/org/apache/syncope/common/lib/types/ClientExceptionType.java
@@ -22,6 +22,7 @@ import javax.ws.rs.core.Response;
 
 public enum ClientExceptionType {
 
+    AssociatedAnys(Response.Status.BAD_REQUEST),
     AssociatedResources(Response.Status.BAD_REQUEST),
     Composite(Response.Status.BAD_REQUEST),
     ConcurrentModification(Response.Status.PRECONDITION_FAILED),

http://git-wip-us.apache.org/repos/asf/syncope/blob/0016d10c/core/logic/src/main/java/org/apache/syncope/core/logic/ConnectorLogic.java
----------------------------------------------------------------------
diff --git a/core/logic/src/main/java/org/apache/syncope/core/logic/ConnectorLogic.java b/core/logic/src/main/java/org/apache/syncope/core/logic/ConnectorLogic.java
index 1d8f705..0546f8f 100644
--- a/core/logic/src/main/java/org/apache/syncope/core/logic/ConnectorLogic.java
+++ b/core/logic/src/main/java/org/apache/syncope/core/logic/ConnectorLogic.java
@@ -108,10 +108,10 @@ public class ConnectorLogic extends AbstractTransactionalLogic<ConnInstanceTO> {
     }
 
     @PreAuthorize("hasRole('" + StandardEntitlement.CONNECTOR_DELETE + "')")
-    public ConnInstanceTO delete(final Long connInstanceKey) {
-        ConnInstance connInstance = connInstanceDAO.find(connInstanceKey);
+    public ConnInstanceTO delete(final Long key) {
+        ConnInstance connInstance = connInstanceDAO.find(key);
         if (connInstance == null) {
-            throw new NotFoundException("Connector '" + connInstanceKey + "'");
+            throw new NotFoundException("Connector '" + key + "'");
         }
 
         if (!connInstance.getResources().isEmpty()) {
@@ -123,11 +123,9 @@ public class ConnectorLogic extends AbstractTransactionalLogic<ConnInstanceTO> {
             throw associatedResources;
         }
 
-        ConnInstanceTO connToDelete = binder.getConnInstanceTO(connInstance);
-
-        connInstanceDAO.delete(connInstanceKey);
-
-        return connToDelete;
+        ConnInstanceTO deleted = binder.getConnInstanceTO(connInstance);
+        connInstanceDAO.delete(key);
+        return deleted;
     }
 
     @PreAuthorize("hasRole('" + StandardEntitlement.CONNECTOR_LIST + "')")
@@ -157,12 +155,12 @@ public class ConnectorLogic extends AbstractTransactionalLogic<ConnInstanceTO> {
 
     @PreAuthorize("hasRole('" + StandardEntitlement.CONNECTOR_READ + "')")
     @Transactional(readOnly = true)
-    public ConnInstanceTO read(final Long connInstanceKey, final String lang) {
+    public ConnInstanceTO read(final Long key, final String lang) {
         CurrentLocale.set(StringUtils.isBlank(lang) ? Locale.ENGLISH : new Locale(lang));
 
-        ConnInstance connInstance = connInstanceDAO.find(connInstanceKey);
+        ConnInstance connInstance = connInstanceDAO.find(key);
         if (connInstance == null) {
-            throw new NotFoundException("Connector '" + connInstanceKey + "'");
+            throw new NotFoundException("Connector '" + key + "'");
         }
 
         return binder.getConnInstanceTO(connInstance);

http://git-wip-us.apache.org/repos/asf/syncope/blob/0016d10c/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 0b27a0f..c9822a5 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
@@ -20,14 +20,23 @@ package org.apache.syncope.core.logic;
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.Transformer;
 import org.apache.commons.lang3.ArrayUtils;
+import org.apache.syncope.common.lib.SyncopeClientException;
 import org.apache.syncope.common.lib.to.RealmTO;
+import org.apache.syncope.common.lib.types.AnyTypeKind;
+import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.lib.types.StandardEntitlement;
+import org.apache.syncope.core.persistence.api.dao.AnySearchDAO;
 import org.apache.syncope.core.persistence.api.dao.NotFoundException;
 import org.apache.syncope.core.persistence.api.dao.RealmDAO;
+import org.apache.syncope.core.persistence.api.dao.search.AnyCond;
+import org.apache.syncope.core.persistence.api.dao.search.AttributeCond;
+import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
 import org.apache.syncope.core.persistence.api.entity.Realm;
 import org.apache.syncope.core.provisioning.api.data.RealmDataBinder;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -41,6 +50,9 @@ public class RealmLogic extends AbstractTransactionalLogic<RealmTO> {
     private RealmDAO realmDAO;
 
     @Autowired
+    private AnySearchDAO searchDAO;
+
+    @Autowired
     private RealmDataBinder binder;
 
     @PreAuthorize("hasRole('" + StandardEntitlement.REALM_LIST + "')")
@@ -90,6 +102,22 @@ public class RealmLogic extends AbstractTransactionalLogic<RealmTO> {
             throw new NotFoundException(fullPath);
         }
 
+        Set<String> adminRealms = Collections.singleton(realm.getFullPath());
+        AnyCond idCond = new AnyCond(AttributeCond.Type.ISNOTNULL);
+        idCond.setSchema("id");
+        SearchCond allMatchingCond = SearchCond.getLeafCond(idCond);
+        int users = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.USER);
+        int groups = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.GROUP);
+        int anyObjects = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.ANY_OBJECT);
+
+        if (users + groups + anyObjects > 0) {
+            SyncopeClientException containedAnys = SyncopeClientException.build(ClientExceptionType.AssociatedAnys);
+            containedAnys.getElements().add(users + " user(s)");
+            containedAnys.getElements().add(groups + " group(s)");
+            containedAnys.getElements().add(anyObjects + " anyObject(s)");
+            throw containedAnys;
+        }
+
         RealmTO deleted = binder.getRealmTO(realm);
         realmDAO.delete(realm);
         return deleted;

http://git-wip-us.apache.org/repos/asf/syncope/blob/0016d10c/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
----------------------------------------------------------------------
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
index c2b27df..4d3360f 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/RealmITCase.java
@@ -185,4 +185,15 @@ public class RealmITCase extends AbstractITCase {
             assertEquals(ClientExceptionType.NotFound, e.getType());
         }
     }
+
+    @Test
+    public void deleteNonEmpty() {
+        try {
+            realmService.delete("/even/two");
+            fail();
+        } catch (SyncopeClientException e) {
+            assertEquals(ClientExceptionType.AssociatedAnys, e.getType());
+            assertEquals(3, e.getElements().size());
+        }
+    }
 }