You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by mm...@apache.org on 2020/02/27 08:17:38 UTC

[syncope] 09/12: allow policies to reference modules in a chain

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

mmoayyed pushed a commit to branch SYNCOPE-163-1
in repository https://gitbox.apache.org/repos/asf/syncope.git

commit 682422642418a2cd03d0209e7131e138b8a50f0a
Author: Misagh Moayyed <mm...@gmail.com>
AuthorDate: Tue Feb 25 19:01:42 2020 +0330

    allow policies to reference modules in a chain
---
 .../AbstractAuthenticationPolicyConf.java          | 36 ++++++++++---------
 .../authentication/AuthenticationPolicyConf.java   | 20 +++++++----
 .../DefaultAuthenticationPolicyConf.java           | 42 ++++++++++++++++++++++
 .../api/entity/policy/AuthenticationPolicy.java    |  9 +++++
 .../jpa/entity/policy/JPAAuthenticationPolicy.java | 34 ++++++++++++++++++
 .../jpa/inner/AuthenticationPolicyTest.java        | 22 +++++++++++-
 6 files changed, 140 insertions(+), 23 deletions(-)

diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/AbstractAuthenticationPolicyConf.java
similarity index 54%
copy from core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java
copy to common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/AbstractAuthenticationPolicyConf.java
index f43fd7d..fc9352b 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java
+++ b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/AbstractAuthenticationPolicyConf.java
@@ -6,8 +6,7 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
@@ -15,33 +14,38 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
+ *
  */
-package org.apache.syncope.core.persistence.jpa.entity.policy;
 
-import org.apache.syncope.core.persistence.api.entity.policy.AuthenticationPolicy;
+package org.apache.syncope.common.lib.authentication;
 
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.Table;
+import javax.xml.bind.annotation.XmlSeeAlso;
+import javax.xml.bind.annotation.XmlType;
 
-@Entity
-@Table(name = JPAAuthenticationPolicy.TABLE)
-public class JPAAuthenticationPolicy extends AbstractPolicy implements AuthenticationPolicy {
+import java.io.Serializable;
 
-    public static final String TABLE = "AuthenticationPolicy";
+@XmlType
+@XmlSeeAlso({DefaultAuthenticationPolicyConf.class})
+public abstract class AbstractAuthenticationPolicyConf implements Serializable, AuthenticationPolicyConf {
 
-    private static final long serialVersionUID = -4190607009908888884L;
+    private static final long serialVersionUID = 9185127128182430142L;
 
-    @Column(unique = true, nullable = false)
     private String name;
 
+    public AbstractAuthenticationPolicyConf() {
+        setName(getClass().getName());
+    }
+
+    public AbstractAuthenticationPolicyConf(final String name) {
+        setName(name);
+    }
+
     @Override
-    public String getName() {
+    public final String getName() {
         return name;
     }
 
-    @Override
-    public void setName(final String name) {
+    public final void setName(final String name) {
         this.name = name;
     }
 }
diff --git a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/AuthenticationPolicyConf.java
similarity index 61%
copy from core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java
copy to common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/AuthenticationPolicyConf.java
index 774e83c..c8e0557 100644
--- a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java
+++ b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/AuthenticationPolicyConf.java
@@ -6,8 +6,7 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
@@ -15,12 +14,21 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
+ *
  */
-package org.apache.syncope.core.persistence.api.entity.policy;
+package org.apache.syncope.common.lib.authentication;
 
-public interface AuthenticationPolicy extends Policy {
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
 
-    String getName();
+import java.io.Serializable;
 
-    void setName(String name);
+@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
+public interface AuthenticationPolicyConf extends Serializable {
+
+    /**
+     * Give name of related authentication module instance.
+     *
+     * @return name of this authentication policy instance
+     */
+    String getName();
 }
diff --git a/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/DefaultAuthenticationPolicyConf.java b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/DefaultAuthenticationPolicyConf.java
new file mode 100644
index 0000000..467ecfb
--- /dev/null
+++ b/common/idrepo/lib/src/main/java/org/apache/syncope/common/lib/authentication/DefaultAuthenticationPolicyConf.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.syncope.common.lib.authentication;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@XmlRootElement(name = "defaultAuthenticationModuleConf")
+@XmlType
+public class DefaultAuthenticationPolicyConf extends AbstractAuthenticationModuleConf {
+    private static final long serialVersionUID = -2969836600059025380L;
+
+    private List<String> authenticationModules = new ArrayList<>();
+
+    public List<String> getAuthenticationModules() {
+        return authenticationModules;
+    }
+
+    public void setAuthenticationModules(final List<String> authenticationModules) {
+        this.authenticationModules = authenticationModules;
+    }
+}
diff --git a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java
index 774e83c..d3e8399 100644
--- a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java
+++ b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/entity/policy/AuthenticationPolicy.java
@@ -18,9 +18,18 @@
  */
 package org.apache.syncope.core.persistence.api.entity.policy;
 
+import org.apache.syncope.core.persistence.api.entity.Implementation;
+
+import java.util.List;
+
 public interface AuthenticationPolicy extends Policy {
 
     String getName();
 
     void setName(String name);
+
+    List<? extends Implementation> getConfigurations();
+
+    boolean addConfiguration(Implementation configuration);
+
 }
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java
index f43fd7d..f015063 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/entity/policy/JPAAuthenticationPolicy.java
@@ -18,11 +18,22 @@
  */
 package org.apache.syncope.core.persistence.jpa.entity.policy;
 
+import org.apache.syncope.common.lib.types.AMImplementationType;
+import org.apache.syncope.core.persistence.api.entity.Implementation;
 import org.apache.syncope.core.persistence.api.entity.policy.AuthenticationPolicy;
+import org.apache.syncope.core.persistence.jpa.entity.JPAImplementation;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.ManyToMany;
 import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
+
+import java.util.ArrayList;
+import java.util.List;
 
 @Entity
 @Table(name = JPAAuthenticationPolicy.TABLE)
@@ -35,6 +46,16 @@ public class JPAAuthenticationPolicy extends AbstractPolicy implements Authentic
     @Column(unique = true, nullable = false)
     private String name;
 
+    @ManyToMany(fetch = FetchType.EAGER)
+    @JoinTable(name = TABLE + "Conf",
+        joinColumns =
+        @JoinColumn(name = "authentication_policy_id"),
+        inverseJoinColumns =
+        @JoinColumn(name = "implementation_id"),
+        uniqueConstraints =
+        @UniqueConstraint(columnNames = {"authentication_policy_id", "implementation_id"}))
+    private List<JPAImplementation> configurations = new ArrayList<>();
+
     @Override
     public String getName() {
         return name;
@@ -44,4 +65,17 @@ public class JPAAuthenticationPolicy extends AbstractPolicy implements Authentic
     public void setName(final String name) {
         this.name = name;
     }
+
+    @Override
+    public List<? extends Implementation> getConfigurations() {
+        return configurations;
+    }
+
+    @Override
+    public boolean addConfiguration(final Implementation configuration) {
+        checkType(configuration, JPAImplementation.class);
+        checkImplementationType(configuration, AMImplementationType.AUTH_POLICY_CONFIGURATIONS);
+        return configurations.contains((JPAImplementation) configuration)
+            || configurations.add((JPAImplementation) configuration);
+    }
 }
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthenticationPolicyTest.java b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthenticationPolicyTest.java
index be9e0de..af31ce9 100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthenticationPolicyTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthenticationPolicyTest.java
@@ -18,9 +18,15 @@
  */
 package org.apache.syncope.core.persistence.jpa.inner;
 
+import org.apache.syncope.common.lib.authentication.DefaultAuthenticationPolicyConf;
+import org.apache.syncope.common.lib.types.AMImplementationType;
+import org.apache.syncope.common.lib.types.ImplementationEngine;
+import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
 import org.apache.syncope.core.persistence.api.dao.authentication.AuthenticationPolicyDAO;
+import org.apache.syncope.core.persistence.api.entity.Implementation;
 import org.apache.syncope.core.persistence.api.entity.policy.AuthenticationPolicy;
 import org.apache.syncope.core.persistence.jpa.AbstractTest;
+import org.apache.syncope.core.provisioning.api.serialization.POJOHelper;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
@@ -36,6 +42,9 @@ public class AuthenticationPolicyTest extends AbstractTest {
     @Autowired
     private AuthenticationPolicyDAO authenticationPolicyDAO;
 
+    @Autowired
+    private ImplementationDAO implementationDAO;
+
     @Test
     public void find() {
         AuthenticationPolicy authenticationPolicy = authenticationPolicyDAO.find("b912a0d4-a890-416f-9ab8-84ab077eb028");
@@ -53,11 +62,22 @@ public class AuthenticationPolicyTest extends AbstractTest {
 
     @Test
     public void save() {
-
         int beforeCount = authenticationPolicyDAO.findAll().size();
         AuthenticationPolicy authenticationPolicy = entityFactory.newEntity(AuthenticationPolicy.class);
         authenticationPolicy.setName("AuthenticationPolicyTest");
         authenticationPolicy.setDescription("This is a sample authentication policy");
+
+        DefaultAuthenticationPolicyConf conf = new DefaultAuthenticationPolicyConf();
+        conf.setAuthenticationModules(List.of("LdapAuthentication1", "DatabaseAuthentication2"));
+        
+        Implementation type = entityFactory.newEntity(Implementation.class);
+        type.setKey("AuthPolicyConfKey");
+        type.setEngine(ImplementationEngine.JAVA);
+        type.setType(AMImplementationType.AUTH_POLICY_CONFIGURATIONS);
+        type.setBody(POJOHelper.serialize(conf));
+        type = implementationDAO.save(type);
+
+        authenticationPolicy.addConfiguration(type);
         authenticationPolicyDAO.save(authenticationPolicy);
 
         assertNotNull(authenticationPolicy);