You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2020/03/26 02:41:30 UTC

[james-project] 08/16: JAMES-3088 Make LDAPUserRepository inherit AbstractUserRepository

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

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 22e116647e705ea4f5b3ffa69ee76cbeb444053b
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Thu Mar 5 12:27:16 2020 +0700

    JAMES-3088 Make LDAPUserRepository inherit AbstractUserRepository
    
    Reasons:
     - AbstractUserRepository and LDAPUserRepository all carry the information
     of domainList
     - getMailAddressFor() is 100% same in both classes, can get rid of
     that duplication
     - reuse the well implemented `assertValid(Username)` in AbstractUserRepository
     in the future to serve this JAMES-XXXX
     - inherit the rich Contract from AbstractUserRepository
---
 server/data/data-ldap/pom.xml                      |  4 +++
 .../user/ldap/ReadOnlyUsersLDAPRepository.java     | 29 ++++++++--------------
 2 files changed, 14 insertions(+), 19 deletions(-)

diff --git a/server/data/data-ldap/pom.xml b/server/data/data-ldap/pom.xml
index 1e0ccfc..6eb5460 100644
--- a/server/data/data-ldap/pom.xml
+++ b/server/data/data-ldap/pom.xml
@@ -45,6 +45,10 @@
         </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-data-library</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>james-server-lifecycle-api</artifactId>
         </dependency>
         <dependency>
diff --git a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
index e9af7ac..c1ec084 100644
--- a/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
+++ b/server/data/data-ldap/src/main/java/org/apache/james/user/ldap/ReadOnlyUsersLDAPRepository.java
@@ -46,14 +46,13 @@ import org.apache.commons.configuration2.ex.ConfigurationException;
 import org.apache.commons.configuration2.tree.ImmutableNode;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.directory.api.ldap.model.filter.FilterEncoder;
-import org.apache.james.core.MailAddress;
 import org.apache.james.core.Username;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.lifecycle.api.Configurable;
-import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
 import org.apache.james.user.api.model.User;
 import org.apache.james.user.ldap.api.LdapConstants;
+import org.apache.james.user.lib.AbstractUsersRepository;
 import org.apache.james.util.OptionalUtils;
 import org.apache.james.util.retry.DoublingRetrySchedule;
 import org.apache.james.util.retry.api.RetrySchedule;
@@ -243,7 +242,7 @@ import com.google.common.base.Strings;
  * @see ReadOnlyLDAPGroupRestriction
  *
  */
-public class ReadOnlyUsersLDAPRepository implements UsersRepository, Configurable {
+public class ReadOnlyUsersLDAPRepository extends AbstractUsersRepository implements Configurable {
     private static final Logger LOGGER = LoggerFactory.getLogger(ReadOnlyUsersLDAPRepository.class);
 
     // The name of the factory class which creates the initial context
@@ -264,13 +263,11 @@ public class ReadOnlyUsersLDAPRepository implements UsersRepository, Configurabl
     // The schedule for retry attempts
     private RetrySchedule schedule = null;
 
-    private final DomainList domainList;
     private LdapRepositoryConfiguration ldapConfiguration;
 
     @Inject
     public ReadOnlyUsersLDAPRepository(DomainList domainList) {
-        super();
-        this.domainList = domainList;
+        super(domainList);
     }
 
     /**
@@ -666,6 +663,13 @@ public class ReadOnlyUsersLDAPRepository implements UsersRepository, Configurabl
     }
 
     @Override
+    protected void doAddUser(Username username, String password) throws UsersRepositoryException {
+        LOGGER.error("This user-repository is read-only. Modifications are not permitted.");
+        throw new UsersRepositoryException(
+                "This user-repository is read-only. Modifications are not permitted.");
+    }
+
+    @Override
     public boolean isAdministrator(Username username) {
         if (ldapConfiguration.getAdministratorId().isPresent()) {
             return ldapConfiguration.getAdministratorId().get().equals(username);
@@ -677,17 +681,4 @@ public class ReadOnlyUsersLDAPRepository implements UsersRepository, Configurabl
     public boolean isReadOnly() {
         return true;
     }
-
-
-    @Override
-    public MailAddress getMailAddressFor(Username username) throws UsersRepositoryException {
-        try {
-            if (supportVirtualHosting()) {
-                return new MailAddress(username.asString());
-            }
-            return new MailAddress(username.getLocalPart(), domainList.getDefaultDomain());
-        } catch (Exception e) {
-            throw new UsersRepositoryException("Failed to compute mail address associated with the user", e);
-        }
-    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org