View Javadoc

1   package com.atlassian.user.impl.memory;
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.DefaultUser;
8   import com.atlassian.user.impl.DuplicateEntityException;
9   import com.atlassian.user.impl.memory.provider.MemoryProvider;
10  import com.atlassian.user.repository.RepositoryIdentifier;
11  import com.atlassian.user.search.page.Pager;
12  import com.atlassian.user.security.password.PasswordEncryptor;
13  import com.atlassian.user.util.Assert;
14  
15  public class MemoryUserManager implements UserManager
16  {
17      private final MemoryProvider provider;
18      private final RepositoryIdentifier repository;
19      private final PasswordEncryptor passwordEncryptor;
20  
21      public MemoryUserManager(RepositoryIdentifier repository, MemoryProvider provider, PasswordEncryptor passwordEncryptor)
22      {
23          this.provider = provider;
24          this.repository = repository;
25          this.passwordEncryptor = passwordEncryptor;
26      }
27  
28      public Pager<User> getUsers()
29      {
30          return provider.getUsers();
31      }
32  
33      public Pager<String> getUserNames() throws EntityException
34      {
35          return provider.getUserNames();
36      }
37  
38      public User getUser(String username)
39      {
40          return provider.getUser(username);
41      }
42  
43      public User createUser(String username) throws EntityException
44      {
45          User existingUser = provider.getUser(username);
46          if (existingUser != null)
47              throw new DuplicateEntityException("User already exists: [" + existingUser + "] in " + getRepository(existingUser));
48  
49          User user = new DefaultUser(username);
50          provider.addUser(user);
51  
52          return user;
53      }
54  
55      /**
56       * Encrypts the plain password, sets it on the user, and saves the user.
57       */
58      public void alterPassword(User user, String plainTextPass) throws EntityException
59      {
60          user.setPassword(passwordEncryptor.encrypt(plainTextPass));
61      }
62  
63      public void removeUser(com.atlassian.user.User user) throws EntityException
64      {
65          if(provider.getUser(user.getName())==null) {
66              throw new IllegalArgumentException("User can not be found in this user manager: [" + user + "]");
67          }
68          provider.removeUser(user);
69      }
70  
71      public boolean isReadOnly(User user)
72      {
73          return getUser(user.getName()) == null;
74      }
75  
76      public void saveUser(User user) throws EntityException
77      {
78          Assert.notNull(user, "User must not be null");
79          // do nothing -- a memory user doesn't require saving.
80      }
81  
82      /**
83       * @return the {@link RepositoryIdentifier} which is managed by this instance.
84       */
85      public RepositoryIdentifier getIdentifier()
86      {
87          return repository;
88      }
89  
90      public RepositoryIdentifier getRepository(Entity entity) throws EntityException
91      {
92          if (getUser(entity.getName()) == null)
93              return null;
94  
95          return repository;
96      }
97  
98      public boolean isCreative()
99      {
100         return true;
101     }
102 }