1
2
3
4
5
6
7
8
9 package com.atlassian.core.user;
10
11 import com.atlassian.core.util.RandomGenerator;
12 import com.opensymphony.user.*;
13 import org.apache.commons.lang.StringUtils;
14 import org.apache.log4j.Logger;
15
16 import java.util.*;
17
18
19
20
21
22
23 public class UserUtils
24 {
25 private static final Logger log = Logger.getLogger(UserUtils.class);
26
27
28
29
30
31
32
33
34
35 public static User getUser(String username) throws EntityNotFoundException
36 {
37 return username == null ? null : UserManager.getInstance().getUser(username);
38 }
39
40
41
42
43
44
45
46
47 public static boolean existsUser(String username)
48 {
49 try
50 {
51
52
53
54 getUser(username);
55 return true;
56 }
57 catch (EntityNotFoundException e)
58 {
59 return false;
60 }
61 }
62
63 public static Collection getAllUsers()
64 {
65 return UserManager.getInstance().getUsers();
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79 public static User getUserByEmail(String email) throws EntityNotFoundException
80 {
81
82 String emailAddress = StringUtils.trimToNull(email);
83 if (emailAddress != null)
84 {
85 for (Iterator iterator = getAllUsers().iterator(); iterator.hasNext();)
86 {
87 User user = (User) iterator.next();
88
89 if (emailAddress.equalsIgnoreCase(user.getEmail()))
90 {
91 return user;
92 }
93 }
94 }
95
96 throw new EntityNotFoundException("Could not find user with email: " + email);
97 }
98
99
100
101
102
103
104
105
106
107 public static List getUsersByEmail(String email)
108 {
109 List users = new ArrayList();
110 String emailAddress = StringUtils.trimToNull(email);
111 if (emailAddress != null)
112 {
113 for (Iterator iterator = getAllUsers().iterator(); iterator.hasNext();)
114 {
115 User user = (User) iterator.next();
116
117 if (emailAddress.equalsIgnoreCase(user.getEmail()))
118 {
119 users.add(user);
120 }
121 }
122 }
123 return users;
124 }
125
126
127
128
129
130
131
132 public static Collection getUsers(Collection groups)
133 {
134 if (groups == null || groups.isEmpty())
135 {
136 return Collections.EMPTY_SET;
137 }
138
139 Set usernames = new HashSet();
140
141 for (Iterator iterator = groups.iterator(); iterator.hasNext();)
142 {
143 Object o = iterator.next();
144 if (o == null)
145 {
146 return UserUtils.getAllUsers();
147 }
148 else
149 {
150 Group group;
151 if (o instanceof Group)
152 {
153 group = (Group) o;
154 }
155 else
156 {
157 group = GroupUtils.getGroup((String) o);
158 }
159 if (group != null)
160 {
161 usernames.addAll(group.getUsers());
162 }
163 }
164 }
165
166 List users = new ArrayList();
167
168 for (Iterator iterator = usernames.iterator(); iterator.hasNext();)
169 {
170 String username = (String) iterator.next();
171 try
172 {
173 users.add(getUser(username));
174 }
175 catch (EntityNotFoundException e)
176 {
177 log.error("Could not find user " + username + " but user was returned as in groups " + groups);
178 }
179 }
180
181 Collections.sort(users, new BestNameComparator());
182
183 return users;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 public static User createUser(String username, String email) throws DuplicateEntityException, ImmutableException
200 {
201 return createUser(username, RandomGenerator.randomPassword(), email, null);
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 public static User createUser(String username, String email, String fullname)
219 throws DuplicateEntityException, ImmutableException
220 {
221 return createUser(username, RandomGenerator.randomPassword(), email, fullname);
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235 public static User createUser(String username, String password, String email, String fullname)
236 throws DuplicateEntityException, ImmutableException
237 {
238 return createUser(username, password, email, fullname, null);
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public static User createUser(String username, String password, String email, String fullname, Collection groups)
255 throws DuplicateEntityException, ImmutableException
256 {
257 log.debug("UserUtils.createUser");
258 try
259 {
260 UserManager.getInstance().getUser(username.toLowerCase());
261 throw new DuplicateEntityException("The user " + username + " already exists.");
262 }
263 catch (EntityNotFoundException ex)
264 {
265 User user = UserManager.getInstance().createUser(username.toLowerCase());
266 user.setEmail(email);
267 user.setPassword(password);
268
269 if (fullname == null)
270 {
271 fullname = username;
272 }
273
274 user.setFullName(fullname);
275
276 if (groups != null)
277 {
278 for (Iterator iterator = groups.iterator(); iterator.hasNext();)
279 {
280 String groupname = (String) iterator.next();
281 user.addToGroup(GroupUtils.getGroup(groupname));
282 }
283 }
284 return user;
285 }
286 }
287
288
289
290
291
292
293
294
295 public static void removeUser(User user) throws Exception
296 {
297 user.remove();
298 }
299
300
301
302
303
304
305
306
307 public static String resetPassword(User user) throws ImmutableException
308 {
309 String newPassword = RandomGenerator.randomPassword();
310 user.setPassword(newPassword);
311 return newPassword;
312 }
313
314
315
316
317
318
319
320
321 public static void changePassword(User user, String password) throws ImmutableException
322 {
323 user.setPassword(password);
324 }
325
326 }