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