View Javadoc

1   package com.atlassian.user.impl.ldap;
2   
3   import com.atlassian.user.Entity;
4   import com.atlassian.user.EntityException;
5   import com.atlassian.user.User;
6   import com.atlassian.user.impl.ReadOnlyUserManager;
7   import com.atlassian.user.impl.RepositoryException;
8   import com.atlassian.user.impl.ldap.properties.LdapSearchProperties;
9   import com.atlassian.user.impl.ldap.repository.LdapContextFactory;
10  import com.atlassian.user.impl.ldap.search.DefaultLDAPUserAdaptor;
11  import com.atlassian.user.impl.ldap.search.LDAPPagerInfo;
12  import com.atlassian.user.impl.ldap.search.LDAPUserAdaptor;
13  import com.atlassian.user.impl.ldap.search.LdapFilterFactory;
14  import com.atlassian.user.impl.ldap.search.page.LDAPEntityPager;
15  import com.atlassian.user.impl.ldap.search.page.LDAPSingleStringPager;
16  import com.atlassian.user.repository.RepositoryIdentifier;
17  import com.atlassian.user.search.page.Pager;
18  import com.atlassian.util.profiling.UtilTimerStack;
19  import net.sf.ldaptemplate.support.filter.EqualsFilter;
20  import org.apache.log4j.Logger;
21  
22  public class LDAPUserManagerReadOnly extends ReadOnlyUserManager
23  {
24      protected final Logger log = Logger.getLogger(this.getClass());
25  
26      private final RepositoryIdentifier repositoryIdentifier;
27      private final LdapContextFactory repository;
28      private final LdapSearchProperties searchProperties;
29      private final LDAPUserAdaptor userAdaptor;
30  
31      public LDAPUserManagerReadOnly(RepositoryIdentifier repositoryIdentifier, LdapContextFactory repository,
32          LdapSearchProperties searchProperties, LdapFilterFactory filterFactory)
33      {
34          this.repositoryIdentifier = repositoryIdentifier;
35          this.repository = repository;
36          this.searchProperties = searchProperties;
37          userAdaptor = new DefaultLDAPUserAdaptor(repository, searchProperties, filterFactory);
38      }
39  
40      /**
41       * @return all users in the configured repository
42       * @throws RepositoryException
43       */
44      public Pager<User> getUsers() throws EntityException
45      {
46          profilePush(this.getClass().getName() + "_getUsers");
47  
48          LDAPPagerInfo info = userAdaptor.search(null);
49          Pager<User> pager = new LDAPEntityPager<User>(repository, new DefaultLDAPUserFactory(searchProperties), info);
50  
51          profilePop(this.getClass().getName() + "_getUsers");
52  
53          return pager;
54      }
55  
56      public Pager<String> getUserNames() throws EntityException
57      {
58          LDAPPagerInfo info = userAdaptor.search(null, new String[]{searchProperties.getUsernameAttribute()});
59          return new LDAPSingleStringPager(repository, info);
60      }
61  
62      public User getUser(String username) throws EntityException
63      {
64          profilePush(this.getClass().getName() + "_getUser(" + username + ")");
65  
66          User user = null;
67  
68          try
69          {
70              LDAPPagerInfo info =
71                  userAdaptor.search(new EqualsFilter(searchProperties.getUsernameAttribute(), username));
72  
73              Pager pager = new LDAPEntityPager<User>(repository, new DefaultLDAPUserFactory(searchProperties), info);
74  
75              if (pager.getCurrentPage().size() > 0)
76                  user = (User) pager.getCurrentPage().get(0);
77          }
78          catch (EntityException e)
79          {
80              log.error("Error retrieving user: '" + username + "' from LDAP.", e);
81          }
82  
83          profilePop(this.getClass().getName() + "_getUser(" + username + ")");
84  
85          return user;
86      }
87  
88      /**
89       * @return the {@link com.atlassian.user.repository.RepositoryIdentifier} which is managed by this instance.
90       */
91      public RepositoryIdentifier getIdentifier()
92      {
93          return repositoryIdentifier;
94      }
95  
96      public RepositoryIdentifier getRepository(Entity entity) throws EntityException
97      {
98          if (!LDAPValidator.validateLDAPEntity(entity))
99              return null;
100 
101         if (getUser(entity.getName()) == null) // not handled by this manager
102             return null;
103 
104         return repositoryIdentifier;
105 
106     }
107 
108     private void profilePush(String key)
109     {
110         if (UtilTimerStack.isActive())
111             UtilTimerStack.push(key);
112     }
113 
114     private void profilePop(String key)
115     {
116         if (UtilTimerStack.isActive())
117             UtilTimerStack.pop(key);
118     }
119 }