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.Credential;
13 import com.atlassian.user.security.password.PasswordEncryptor;
14 import com.atlassian.user.util.Assert;
15
16
17
18
19 public class MemoryUserManager implements UserManager
20 {
21 private final MemoryProvider provider;
22 private final RepositoryIdentifier repository;
23 private final PasswordEncryptor passwordEncryptor;
24
25 public MemoryUserManager(RepositoryIdentifier repository, MemoryProvider provider, PasswordEncryptor passwordEncryptor)
26 {
27 this.provider = provider;
28 this.repository = repository;
29 this.passwordEncryptor = passwordEncryptor;
30 }
31
32 public Pager<User> getUsers()
33 {
34 return provider.getUsers();
35 }
36
37 public Pager<String> getUserNames() throws EntityException
38 {
39 return provider.getUserNames();
40 }
41
42 public User getUser(String username)
43 {
44 return provider.getUser(username);
45 }
46
47 public User createUser(String username) throws EntityException
48 {
49 return createUser(new DefaultUser(username), Credential.NONE);
50 }
51
52 public User createUser(User userTemplate, Credential credential) throws EntityException, UnsupportedOperationException, IllegalArgumentException
53 {
54 User existingUser = provider.getUser(userTemplate.getName());
55 if (existingUser != null)
56 throw new DuplicateEntityException("User already exists: [" + existingUser + "] in " + getRepository(existingUser));
57
58 DefaultUser user = new DefaultUser(userTemplate.getName(), userTemplate.getFullName(), userTemplate.getEmail());
59 user.setPassword(passwordEncryptor.getEncryptedValue(credential));
60 provider.addUser(user);
61
62 return user;
63 }
64
65
66
67
68 public void alterPassword(User user, String plainTextPass) throws EntityException
69 {
70 user.setPassword(passwordEncryptor.encrypt(plainTextPass));
71 }
72
73 public void removeUser(com.atlassian.user.User user) throws EntityException
74 {
75 if(provider.getUser(user.getName())==null) {
76 throw new IllegalArgumentException("User can not be found in this user manager: [" + user + "]");
77 }
78 provider.removeUser(user);
79 }
80
81 public boolean isReadOnly(User user)
82 {
83 return getUser(user.getName()) == null;
84 }
85
86 public void saveUser(User user) throws EntityException
87 {
88 Assert.notNull(user, "User must not be null");
89 provider.saveUser(user);
90 }
91
92
93
94
95 public RepositoryIdentifier getIdentifier()
96 {
97 return repository;
98 }
99
100 public RepositoryIdentifier getRepository(Entity entity) throws EntityException
101 {
102 if (getUser(entity.getName()) == null)
103 return null;
104
105 return repository;
106 }
107
108 public boolean isCreative()
109 {
110 return true;
111 }
112 }