View Javadoc

1   package com.atlassian.user.impl.delegation;
2   
3   import com.atlassian.user.Entity;
4   import com.atlassian.user.EntityException;
5   import com.atlassian.user.User;
6   import com.atlassian.user.UserManager;
7   import com.atlassian.user.impl.DuplicateEntityException;
8   import com.atlassian.user.impl.delegation.repository.DelegatingRepository;
9   import com.atlassian.user.repository.RepositoryIdentifier;
10  import com.atlassian.user.search.page.Pager;
11  import com.atlassian.user.search.page.PagerFactory;
12  import com.atlassian.user.util.Assert;
13  
14  import java.util.ArrayList;
15  import java.util.Collections;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  /*
20   * User manager with a list of other user managers to delegate to.
21   * The user managers are tried in the iteration order of the list.
22   */
23  public class DelegatingUserManager implements UserManager
24  {
25      private final List<UserManager> userManagers;
26  
27      public DelegatingUserManager(List<UserManager> userManagers)
28      {
29          this.userManagers = userManagers;
30      }
31  
32      public Pager<User> getUsers() throws EntityException
33      {
34          ArrayList<Pager<User>> pagers = new ArrayList<Pager<User>>();
35  
36          for (UserManager userManager : userManagers)
37          {
38              pagers.add(userManager.getUsers());
39          }
40  
41          return PagerFactory.getPager(pagers);
42      }
43  
44      public Pager<String> getUserNames() throws EntityException
45      {
46          ArrayList<Pager<String>> pagers = new ArrayList<Pager<String>>();
47  
48          for (UserManager userManager : userManagers)
49          {
50              pagers.add(userManager.getUserNames());
51          }
52  
53          return PagerFactory.getPager(pagers);
54      }
55  
56      /**
57       * @return - an {@link com.atlassian.user.User} if one could be found, otherwise null.
58       * @throws com.atlassian.user.EntityException
59       *          - representing the exception which prohibited looking for or
60       *          retrieving the user.
61       */
62      public User getUser(String username) throws EntityException
63      {
64          User foundUser = null;
65  
66          for (UserManager userManager : userManagers)
67          {
68              foundUser = userManager.getUser(username);
69  
70              if (foundUser != null)
71                  break;
72          }
73  
74          return foundUser;
75      }
76  
77      /**
78       * @return the created user
79       * @throws com.atlassian.user.EntityException If user cannot be created in any user managers.
80       */
81      public User createUser(String username) throws EntityException
82      {
83          User preexistingUser;
84          try
85          {
86              preexistingUser = getUser(username);
87          }
88          catch (EntityException e)
89          {
90              throw new EntityException("Couldn't check whether user already exists", e);
91          }
92  
93          if (preexistingUser != null)
94              throw new DuplicateEntityException("User [" + username + "] already exists in " + getRepository(preexistingUser).getName());
95  
96  
97          User createdUser = null;
98  
99          for (UserManager userManager : userManagers)
100         {
101 
102             if (userManager.isCreative())
103                 createdUser = userManager.createUser(username);
104 
105             if (createdUser != null)
106                 break;
107         }
108 
109         if (createdUser == null)
110             throw new EntityException("Could not create user: " + username + ". " +
111                 "Ensure you have a read-write repository configured.");
112 
113         return createdUser;
114     }
115 
116     /**
117      * Encrypts the plain password, sets it on the user, and saves the user.
118      */
119     public void alterPassword(User user, String plainTextPass) throws EntityException
120     {
121         UserManager userManager = getMatchingUserManager(user);
122 
123         if (userManager != null)
124             userManager.alterPassword(user, plainTextPass);
125         else
126             throw new EntityException("Cannot find a userManager responsible for user [" + user.getName() + "]");
127     }
128 
129     public void saveUser(User user) throws EntityException
130     {
131         UserManager userManager = getMatchingUserManager(user);
132 
133         if (userManager != null)
134             userManager.saveUser(user);
135         else
136             throw new EntityException("Cannot find a userManager responsible for user [" + user.getName() + "]");
137     }
138 
139     public void removeUser(User user) throws EntityException
140     {
141         UserManager userManager = getMatchingUserManager(user);
142 
143         if (userManager != null)
144             userManager.removeUser(user);
145         else
146             throw new IllegalArgumentException("Cannot find a userManager responsible for user [" + user.getName() + "]");
147     }
148 
149     /**
150      * @return true indicates that information on the user object cannot be altered in the storage system
151      *         (see {@link com.atlassian.user.repository.RepositoryIdentifier}),
152      *         false indicates that the storage system will save changes or that this {@link com.atlassian.user.UserManager} does not
153      *         know about the {@link com.atlassian.user.User}.
154      * @throws com.atlassian.user.EntityException
155      *
156      */
157     public boolean isReadOnly(User user) throws EntityException
158     {
159         UserManager userManager = getMatchingUserManager(user);
160 
161         if (userManager != null)
162             return userManager.isReadOnly(user);
163         else
164             throw new EntityException("Cannot find a userManager responsible for user [" + user.getName() + "]");
165     }
166 
167     /**
168      * @return the {@link com.atlassian.user.repository.RepositoryIdentifier} which is managed by this instance.
169      */
170     public RepositoryIdentifier getIdentifier()
171     {
172         Iterator iter = userManagers.iterator();
173         ArrayList<RepositoryIdentifier> repositories = new ArrayList<RepositoryIdentifier>();
174 
175         while (iter.hasNext())
176         {
177             UserManager userManager = (UserManager) iter.next();
178             repositories.add(userManager.getIdentifier());
179         }
180 
181         return new DelegatingRepository(repositories);
182     }
183 
184     /**
185      * @return the {@link com.atlassian.user.repository.RepositoryIdentifier} in which the entity is stored, otherwise null.
186      * @throws com.atlassian.user.EntityException
187      *
188      */
189     public RepositoryIdentifier getRepository(Entity entity) throws EntityException
190     {
191 
192         for (UserManager userManager : userManagers)
193         {
194             RepositoryIdentifier repo = userManager.getRepository(entity);
195 
196             if (repo != null)
197                 return repo;
198         }
199 
200         return null;
201     }
202 
203     /**
204      * Used to detemine whether an entity can be created (eg, can call {@link com.atlassian.user.UserManager#createUser(String)} or
205      * {@link com.atlassian.user.GroupManager#createGroup(String)}
206      *
207      * @return true to indicate that {@link com.atlassian.user.Entity} objects can be created by this manager, or false to indicate
208      *         not.
209      */
210     public boolean isCreative()
211     {
212         for (UserManager userManager : userManagers)
213         {
214             if (userManager.isCreative())
215                 return true;
216         }
217 
218         return false;
219     }
220 
221     /**
222      * Helper method to locate the first userManager responsible for the given user, in the delegation.
223      */
224     protected UserManager getMatchingUserManager(User user) throws EntityException
225     {
226         Assert.notNull(user, "User must not be null");
227 
228         for (UserManager userManager : userManagers)
229         {
230             User foundUser = userManager.getUser(user.getName());
231 
232             if (foundUser != null)
233                 return userManager;
234         }
235 
236         return null;
237     }
238 
239     /**
240      * @return an unmodifiable list of {@link UserManager}s which this manager delegates to,
241      * in the order of delegation.
242      */
243     public List getUserManagers()
244     {
245         return Collections.unmodifiableList(userManagers);
246     }
247 }