1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.implementations;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.TreeSet;
32
33 import org.apache.log4j.Logger;
34 import org.itracker.model.Issue;
35 import org.itracker.model.Permission;
36 import org.itracker.model.PermissionType;
37 import org.itracker.model.Project;
38 import org.itracker.model.User;
39 import org.itracker.model.UserPreferences;
40 import org.itracker.persistence.dao.NoSuchEntityException;
41 import org.itracker.persistence.dao.PermissionDAO;
42 import org.itracker.persistence.dao.ProjectDAO;
43 import org.itracker.persistence.dao.ReportDAO;
44 import org.itracker.persistence.dao.UserDAO;
45 import org.itracker.persistence.dao.UserPreferencesDAO;
46 import org.itracker.services.ConfigurationService;
47 import org.itracker.services.ProjectService;
48 import org.itracker.services.UserService;
49 import org.itracker.services.authentication.PluggableAuthenticator;
50 import org.itracker.services.exceptions.AuthenticatorException;
51 import org.itracker.services.exceptions.PasswordException;
52 import org.itracker.services.exceptions.UserException;
53 import org.itracker.services.util.AuthenticationConstants;
54 import org.itracker.services.util.ProjectUtilities;
55 import org.itracker.services.util.UserUtilities;
56
57
58
59
60
61
62
63 public class UserServiceImpl implements UserService {
64
65 private static final String DEFAULT_AUTHENTICATOR =
66 "org.itracker.services.authentication.DefaultAuthenticator";
67
68
69
70 private String authenticatorClassName = null;
71 private Class<?> authenticatorClass = null;
72 private boolean allowSelfRegister = false;
73
74 private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
75
76 private PermissionDAO permissionDAO = null;
77
78 private UserDAO userDAO = null;
79 private UserPreferencesDAO userPreferencesDAO = null;
80 private ProjectService projectService;
81 private ConfigurationService configurationService;
82
83
84
85
86
87
88
89
90 public UserServiceImpl(ConfigurationService configurationService,
91 ProjectService projectService,
92 UserDAO userDAO,
93 PermissionDAO permissionDAO,
94 UserPreferencesDAO userPreferencesDAO) {
95
96
97 this.configurationService = configurationService;
98 this.projectService = projectService;
99 this.userDAO = userDAO;
100 this.userPreferencesDAO = userPreferencesDAO;
101 this.permissionDAO = permissionDAO;
102
103 try {
104 allowSelfRegister = configurationService.getBooleanProperty("allow_self_register", false);
105
106 authenticatorClassName = configurationService.getProperty("authenticator_class", DEFAULT_AUTHENTICATOR);
107 authenticatorClass = Class.forName(authenticatorClassName);
108 } catch (ClassNotFoundException ex) {
109 throw new RuntimeException(ex);
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122 public UserServiceImpl(ConfigurationService configurationService,
123 ProjectService projectService,
124 UserDAO userDAO,
125 ProjectDAO projectDAO,
126 ReportDAO reportDAO,
127 PermissionDAO permissionDAO,
128 UserPreferencesDAO userPreferencesDAO) {
129 this(configurationService, projectService, userDAO, permissionDAO, userPreferencesDAO);
130 }
131
132 public User getUser(Integer userId) {
133 User user = userDAO.findByPrimaryKey(userId);
134 return user;
135 }
136
137 public User getUserByLogin(String login) throws NoSuchEntityException {
138 User user = userDAO.findByLogin(login);
139 if (user == null)
140 throw new NoSuchEntityException("User " + login + " not found.");
141 return user;
142 }
143
144 public String getUserPasswordByLogin(String login) {
145 User user = userDAO.findByLogin(login);
146 return user.getPassword();
147 }
148
149 public List<User> getAllUsers() {
150 List<User> users = userDAO.findAll();
151
152 return users;
153 }
154
155 public int getNumberUsers() {
156 Collection<User> users = userDAO.findAll();
157 return users.size();
158 }
159
160 public List<User> getActiveUsers() {
161 List<User> users = userDAO.findActive();
162
163 return users;
164 }
165
166 public List<User> getSuperUsers() {
167 List<User> superUsers = userDAO.findSuperUsers();
168 return superUsers;
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public User createUser(User user) throws UserException {
199 try {
200 if (user == null || user.getLogin() == null || user.getLogin().equals("")) {
201 throw new UserException("User data was null, or login was empty.");
202 }
203
204 try {
205 this.getUserByLogin(user.getLogin());
206 throw new UserException("User already exists with login: " + user.getLogin());
207 } catch (NoSuchEntityException e) {
208
209 }
210
211 try {
212 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
213 if (authenticator != null) {
214 HashMap<String, Object> values = new HashMap<String, Object>();
215 values.put("userService", this);
216 values.put("configurationService", configurationService);
217 authenticator.initialize(values);
218 authenticator.createProfile(user, null, AuthenticationConstants.AUTH_TYPE_UNKNOWN,
219 AuthenticationConstants.REQ_SOURCE_UNKNOWN);
220 } else {
221 throw new AuthenticatorException("Unable to create new authenticator.", AuthenticatorException.SYSTEM_ERROR);
222 }
223 } catch (IllegalAccessException ex) {
224 throw new AuthenticatorException(
225 "Authenticator class " + authenticatorClassName + " can not be instantiated.",
226 AuthenticatorException.SYSTEM_ERROR, ex);
227 } catch (InstantiationException ex) {
228 throw new AuthenticatorException(
229 "Authenticator class " + authenticatorClassName + " can not be instantiated.",
230 AuthenticatorException.SYSTEM_ERROR, ex);
231 } catch (ClassCastException ex) {
232 throw new AuthenticatorException("Authenticator class " + authenticatorClassName
233 + " does not extend the PluggableAuthenticator class.",
234 AuthenticatorException.SYSTEM_ERROR, ex);
235 }
236 user.setStatus(UserUtilities.STATUS_ACTIVE);
237 user.setRegistrationType(user.getRegistrationType());
238
239
240 userDAO.save(user);
241 return user;
242 } catch (AuthenticatorException ex) {
243 throw new UserException("Could not create user.", ex);
244 }
245
246 }
247
248 public User updateUser(User user) throws UserException {
249 try {
250 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
251 if (authenticator != null) {
252 HashMap<String, Object> values = new HashMap<String, Object>();
253 values.put("userService", this);
254 values.put("configurationService", configurationService);
255 authenticator.initialize(values);
256 authenticator.updateProfile(user, AuthenticationConstants.UPDATE_TYPE_CORE, null,
257 AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN);
258 } else {
259 logger.warn("updateUser: no authenticator, throwing AuthenticatorException");
260 throw new AuthenticatorException("Unable to create new authenticator.",
261 AuthenticatorException.SYSTEM_ERROR);
262 }
263 } catch (IllegalAccessException ex) {
264 logger.error("updateUser: IllegalAccessException caught, throwing AuthenticatorException", ex);
265 throw new AuthenticatorException(
266 "Authenticator class " + authenticatorClassName + " can not be instantiated.",
267 AuthenticatorException.SYSTEM_ERROR, ex);
268 } catch (InstantiationException ex) {
269 logger.error("updateUser: InstantiationException caught, throwing AuthenticatorException", ex);
270 throw new AuthenticatorException(
271 "Authenticator class " + authenticatorClassName + " can not be instantiated.",
272 AuthenticatorException.SYSTEM_ERROR, ex);
273 } catch (ClassCastException ex) {
274 logger.error("updateUser: ClassCastException caught, throwing AuthenticatorException", ex);
275 throw new AuthenticatorException(
276 "Authenticator class " + authenticatorClassName
277 + " does not extend the PluggableAuthenticator class.",
278 AuthenticatorException.SYSTEM_ERROR, ex);
279 } catch (AuthenticatorException ex) {
280 logger.error("updateUser: AuthenticatorException caught, throwing AuthenticatorException", ex);
281 throw new UserException("Unable to update user.", ex);
282 }
283
284
285 Integer id = user.getId();
286 userDAO.detach(user);
287
288 User existinguser = userDAO.findByPrimaryKey(id);
289 userDAO.refresh(existinguser);
290
291 existinguser.setLogin(user.getLogin());
292 existinguser.setFirstName(user.getFirstName());
293 existinguser.setLastName(user.getLastName());
294 existinguser.setEmail(user.getEmail());
295 existinguser.setSuperUser(user.isSuperUser());
296
297 existinguser.setStatus(user.getStatus());
298
299
300
301
302 if (user.getPassword() != null && (!user.getPassword().equals(""))) {
303
304 if (logger.isInfoEnabled()) {
305 logger.info("updateUser: setting new password for " + user.getLogin());
306 }
307 existinguser.setPassword(user.getPassword());
308 }
309
310 userDAO.saveOrUpdate(existinguser);
311
312
313
314 return existinguser;
315 }
316
317 public String generateUserPassword(User user) throws PasswordException {
318 String password = UserUtilities.generatePassword();
319 user.setPassword(UserUtilities.encryptPassword(password));
320 return password;
321
322 }
323
324 public UserPreferences updateUserPreferences(UserPreferences userPrefs) throws UserException {
325 UserPreferences newUserPrefs = new UserPreferences();
326
327 try {
328 User user = userPrefs.getUser();
329
330 newUserPrefs = userPreferencesDAO.findByUserId(user.getId());
331
332 if (newUserPrefs == null) {
333 newUserPrefs = new UserPreferences();
334 }
335 newUserPrefs.setSaveLogin(userPrefs.getSaveLogin());
336 newUserPrefs.setUserLocale(userPrefs.getUserLocale());
337 newUserPrefs.setNumItemsOnIndex(userPrefs.getNumItemsOnIndex());
338 newUserPrefs.setNumItemsOnIssueList(userPrefs.getNumItemsOnIssueList());
339 newUserPrefs.setShowClosedOnIssueList(userPrefs.getShowClosedOnIssueList());
340 newUserPrefs.setSortColumnOnIssueList(userPrefs.getSortColumnOnIssueList());
341 newUserPrefs.setHiddenIndexSections(userPrefs.getHiddenIndexSections());
342
343 newUserPrefs.setRememberLastSearch(userPrefs.getRememberLastSearch());
344 newUserPrefs.setUseTextActions(userPrefs.getUseTextActions());
345
346
347 newUserPrefs.setUser(user);
348
349 if (userPrefs.isNew()) {
350 newUserPrefs.setCreateDate(new Date());
351 newUserPrefs.setLastModifiedDate(userPrefs.getCreateDate());
352
353
354 user.setPreferences(newUserPrefs);
355 userDAO.saveOrUpdate(user);
356 } else {
357 this.userPreferencesDAO.saveOrUpdate(newUserPrefs);
358 newUserPrefs = userPreferencesDAO.findByUserId(user.getId());
359 user.setUserPreferences(newUserPrefs);
360 }
361
362 try {
363 PluggableAuthenticator authenticator =
364 (PluggableAuthenticator) authenticatorClass.newInstance();
365
366 if (authenticator != null) {
367 HashMap<String, Object> values = new HashMap<String, Object>();
368 values.put("userService", this);
369 values.put("configurationService", configurationService);
370 authenticator.initialize(values);
371 authenticator.updateProfile(user, AuthenticationConstants.UPDATE_TYPE_PREFERENCE, null,
372 AuthenticationConstants.AUTH_TYPE_UNKNOWN, AuthenticationConstants.REQ_SOURCE_UNKNOWN);
373 } else {
374 throw new AuthenticatorException("Unable to create new authenticator.",
375 AuthenticatorException.SYSTEM_ERROR);
376 }
377 } catch (IllegalAccessException ex) {
378 throw new AuthenticatorException(
379 "Authenticator class " + authenticatorClassName + " can not be instantiated.",
380 AuthenticatorException.SYSTEM_ERROR, ex);
381 } catch (InstantiationException ex) {
382 throw new AuthenticatorException(
383 "Authenticator class " + authenticatorClassName + " can not be instantiated.",
384 AuthenticatorException.SYSTEM_ERROR, ex);
385 } catch (ClassCastException ex) {
386 throw new AuthenticatorException(
387 "Authenticator class " + authenticatorClassName
388 + " does not extend the PluggableAuthenticator class.",
389 AuthenticatorException.SYSTEM_ERROR, ex);
390 }
391
392 if (newUserPrefs != null)
393 return newUserPrefs;
394
395 } catch (AuthenticatorException ex) {
396 throw new UserException("Unable to create new preferences.", ex);
397 }
398
399 return userPrefs;
400
401 }
402
403 public void clearOwnedProjects(User user) {
404 user.getProjects().clear();
405 userDAO.save(user);
406 }
407
408 public List<User> findUsersForProjectByPermissionTypeList(Integer projectID, Integer[] permissionTypes) {
409 return userDAO.findUsersForProjectByAllPermissionTypeList(projectID, permissionTypes);
410 }
411
412 public List<User> getUsersWithPermissionLocal(Integer projectId, int permissionType) {
413
414 List<User> users = new ArrayList<User>();
415
416 if (projectId != null) {
417 List<Permission> permissions = permissionDAO.findByProjectIdAndPermission(
418 projectId, permissionType);
419
420 for (Permission permission : permissions) {
421 users.add(permission.getUser());
422 }
423
424 }
425
426 return users;
427
428 }
429
430 public List<Permission> getUserPermissionsLocal(User user) {
431 List<Permission> permissions = permissionDAO.findByUserId(user.getId());
432 return permissions;
433 }
434
435 public List<Permission> getPermissionsByUserId(Integer userId) {
436 List<Permission> permissions = new ArrayList<Permission>();
437
438 User user = getUser(userId);
439 if (user != null) {
440 try {
441 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
442 if (authenticator != null) {
443 HashMap<String, Object> values = new HashMap<String, Object>();
444 values.put("userService", this);
445 values.put("configurationService", configurationService);
446 authenticator.initialize(values);
447 permissions = authenticator.getUserPermissions(user, AuthenticationConstants.REQ_SOURCE_UNKNOWN);
448 }
449 logger.debug("Found " + permissions.size() + " permissions for user " + user.getLogin());
450 } catch (IllegalAccessException ex) {
451 throw new RuntimeException("Authenticator class "
452 + authenticatorClassName + " can not be instantiated.", ex);
453 } catch (InstantiationException ex) {
454 throw new RuntimeException("Authenticator class "
455 + authenticatorClassName + " can not be instantiated.", ex);
456 } catch (ClassCastException ex) {
457 throw new RuntimeException("Authenticator class " + authenticatorClassName
458 + " does not extend the PluggableAuthenticator class.", ex);
459 } catch (AuthenticatorException ex) {
460 throw new RuntimeException("Authenticator exception: ", ex);
461 }
462 }
463 return permissions;
464 }
465
466 public boolean updateAuthenticator(Integer userId, List<Permission> permissions) {
467 boolean successful = false;
468
469 try {
470 User user = userDAO.findByPrimaryKey(userId);
471 user.getPermissions().addAll(permissions);
472
473 try {
474 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
475 if (authenticator != null) {
476 HashMap<String, Object> values = new HashMap<String, Object>();
477 values.put("userService", this);
478 values.put("configurationService", configurationService);
479 authenticator.initialize(values);
480 if (authenticator
481 .updateProfile(user, AuthenticationConstants.UPDATE_TYPE_PERMISSION_SET, null,
482 AuthenticationConstants.AUTH_TYPE_UNKNOWN,
483 AuthenticationConstants.REQ_SOURCE_UNKNOWN)) {
484
485 }
486 } else {
487 logger.error("Unable to create new authenticator.");
488 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
489 }
490 successful = true;
491 } catch (IllegalAccessException iae) {
492 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
493 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
494 } catch (InstantiationException ie) {
495 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
496 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
497 } catch (ClassCastException cce) {
498 logger.error("Authenticator class " + authenticatorClassName
499 + " does not extend the PluggableAuthenticator class.");
500 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
501 }
502
503 } catch (AuthenticatorException ae) {
504 logger.warn("Error setting user (" + userId + ") permissions. AuthenticatorException.", ae);
505 successful = false;
506 }
507
508 return successful;
509 }
510
511 public boolean addUserPermissions(Integer userId, List<Permission> newPermissions) {
512 boolean successful = false;
513 if (newPermissions == null || newPermissions.size() == 0) {
514 return successful;
515 }
516
517 try {
518 newPermissions.addAll(getUserPermissionsLocal(getUser(userId)));
519 setUserPermissions(userId, newPermissions);
520 successful = true;
521 } catch (AuthenticatorException ae) {
522 logger.warn("Error setting user (" + userId + ") permissions. AuthenticatorException.", ae);
523 successful = false;
524 }
525
526 return successful;
527 }
528
529
530
531
532 private static final Permission find(Collection<Permission> permissions, Permission permission) {
533
534 Iterator<Permission> permssionsIt = permissions.iterator();
535 while (permssionsIt.hasNext()) {
536 Permission permission2 = (Permission) permssionsIt.next();
537 if (Permission.PERMISSION_PROPERTIES_COMPARATOR.compare(permission, permission2) == 0) {
538
539 return permission2;
540 }
541 }
542 return null;
543 }
544
545
546
547
548
549 public boolean setUserPermissions(final Integer userId, final List<Permission> newPermissions) {
550
551 boolean hasChanges = false;
552
553
554 TreeSet<Permission> pSet = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);
555 pSet.addAll(newPermissions);
556
557
558 User usermodel = this.getUser(userId);
559
560 Set<Permission> current = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);
561
562 current.addAll(usermodel.getPermissions());
563
564
565 Set<Permission> remove = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);
566 remove.addAll(current);
567 remove.removeAll(pSet);
568
569 Set<Permission> add = new TreeSet<Permission>(Permission.PERMISSION_PROPERTIES_COMPARATOR);
570 add.addAll(pSet);
571 add.removeAll(current);
572
573
574 Permission p;
575 Iterator<Permission> pIt = remove.iterator();
576 while (pIt.hasNext()) {
577 p = find(usermodel.getPermissions(), (Permission) pIt.next());
578 if (null == p) {
579 continue;
580 }
581 if (usermodel.getPermissions().contains(p)) {
582 usermodel.getPermissions().remove(p);
583 permissionDAO.delete(p);
584 hasChanges = true;
585 }
586 }
587
588 pIt = add.iterator();
589 while (pIt.hasNext()) {
590 p = pIt.next();
591 if (null == find(usermodel.getPermissions(), p) && !usermodel.getPermissions().contains(p)) {
592 p.setUser(usermodel);
593 usermodel.getPermissions().add(p);
594 permissionDAO.save(p);
595 hasChanges = true;
596 }
597 }
598
599 if (hasChanges) {
600 userDAO.saveOrUpdate(usermodel);
601 }
602
603 return hasChanges;
604 }
605
606 public boolean removeUserPermissions(Integer userId, List<Permission> newPermissions) {
607 boolean successful = false;
608 if (newPermissions == null || newPermissions.size() == 0) {
609 return successful;
610 }
611
612 try {
613 for (Iterator<Permission> delIterator = newPermissions.iterator(); delIterator.hasNext();) {
614 Permission permission = (Permission) delIterator.next();
615 permissionDAO.delete(permission);
616 }
617
618 successful = true;
619
620 } catch (AuthenticatorException ae) {
621 logger.warn("Error setting user (" + userId + ") permissions. AuthenticatorException.", ae);
622 successful = false;
623 }
624
625 return successful;
626 }
627
628 @Deprecated
629 public Map<Integer, Set<PermissionType>> getUsersMapOfProjectIdsAndSetOfPermissionTypes(User user, int reqSource) {
630 Map<Integer, Set<PermissionType>> permissionsMap = new HashMap<Integer, Set<PermissionType>>();
631
632 if (user == null) {
633 return permissionsMap;
634 }
635
636 List<Permission> permissionList = new ArrayList<Permission>();
637
638 try {
639 PluggableAuthenticator authenticator =
640 (PluggableAuthenticator) authenticatorClass.newInstance();
641
642 if (authenticator != null) {
643 HashMap<String, Object> values = new HashMap<String, Object>();
644 values.put("userService", this);
645 values.put("configurationService", configurationService);
646 authenticator.initialize(values);
647 permissionList = authenticator.getUserPermissions(user, (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
648 }
649 logger.debug("Found " + permissionList.size() + " permissions for user " + user.getLogin());
650 } catch (IllegalAccessException iae) {
651 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
652 } catch (InstantiationException ie) {
653 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
654 } catch (ClassCastException cce) {
655 logger.error("Authenticator class " + authenticatorClassName
656 + " does not extend the PluggableAuthenticator class.");
657 } catch (AuthenticatorException ae) {
658 logger.error("Authenticator exception: " + ae.getMessage());
659 logger.debug("Authenticator exception: ", ae);
660 }
661
662 permissionsMap = UserUtilities.mapPermissionTypesByProjectId(permissionList);
663
664 if (allowSelfRegister) {
665 List<Project> projects = projectService.getAllProjects();
666
667 for (int i = 0; i < projects.size(); i++) {
668 Project project = projects.get(i);
669
670 if (project.getOptions() >= ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_CREATE) {
671 Set<PermissionType> projectPermissions = permissionsMap.get(project.getId());
672
673 if (projectPermissions == null) {
674 projectPermissions = new HashSet<PermissionType>();
675 permissionsMap.put(project.getId(), projectPermissions);
676 }
677
678 if (ProjectUtilities.hasOption(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_CREATE, project.getOptions())) {
679 projectPermissions.add(PermissionType.ISSUE_VIEW_USERS);
680 projectPermissions.add(PermissionType.ISSUE_CREATE);
681 }
682
683 if (ProjectUtilities.hasOption(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL, project.getOptions())) {
684 projectPermissions.add(PermissionType.ISSUE_VIEW_ALL);
685 }
686 }
687 }
688 }
689
690 return permissionsMap;
691 }
692
693 public List<User> getUsersWithProjectPermission(Integer projectId, int permissionType) {
694 return getUsersWithProjectPermission(projectId, permissionType, true);
695 }
696
697 public List<User> getUsersWithProjectPermission(Integer projectId, int permissionType, boolean activeOnly) {
698 return getUsersWithAnyProjectPermission(projectId, new int[]{permissionType}, activeOnly);
699 }
700
701 public List<User> getUsersWithAnyProjectPermission(Integer projectId, int[] permissionTypes) {
702 return getUsersWithAnyProjectPermission(projectId, permissionTypes, true);
703 }
704 public Collection<User> getUsersWithAnyProjectPermission(Integer projectId, Integer[] permissionTypes) {
705 int[] perm = new int[permissionTypes.length];
706
707 for (int i = 0; i < permissionTypes.length; i++) {
708 perm[i] = permissionTypes[i];
709 }
710
711 return getUsersWithAnyProjectPermission(projectId, perm, true);
712 }
713
714 public List<User> getUsersWithAnyProjectPermission(Integer projectId, int[] permissionTypes, boolean activeOnly) {
715 return getUsersWithProjectPermission(projectId, permissionTypes, false, activeOnly);
716 }
717
718 public List<User> getUsersWithProjectPermission(Integer projectId, int[] permissionTypes, boolean requireAll,
719 boolean activeOnly) {
720 List<User> userList = new ArrayList<User>();
721
722 try {
723
724 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
725
726 if (authenticator != null) {
727 Map<String, Object> values = new HashMap<String, Object>();
728 values.put("userService", this);
729 values.put("configurationService", configurationService);
730 authenticator.initialize(values);
731
732 userList = authenticator.getUsersWithProjectPermission(projectId, permissionTypes, requireAll, activeOnly,
733 AuthenticationConstants.REQ_SOURCE_UNKNOWN);
734
735 }
736
737 if (logger.isDebugEnabled()) {
738 logger.debug("getUsersWithProjectPermission: Found " + userList.size() + " users with project " + projectId + " permissions "
739 + Arrays.toString(permissionTypes) + (requireAll ? "[AllReq," : "[AnyReq,")
740 + (activeOnly ? "ActiveUsersOnly]" : "AllUsers]"));
741 }
742
743
744 } catch (IllegalAccessException iae) {
745 logger.error("getUsersWithProjectPermission: Authenticator class " + authenticatorClassName + " can not be instantiated.", iae);
746 } catch (InstantiationException ie) {
747 logger.error("getUsersWithProjectPermission: Authenticator class " + authenticatorClassName + " can not be instantiated.", ie);
748 } catch (ClassCastException cce) {
749 logger.error("getUsersWithProjectPermission: Authenticator class " + authenticatorClassName
750 + " does not extend the PluggableAuthenticator class.", cce);
751 } catch (AuthenticatorException ae) {
752 logger.error("getUsersWithProjectPermission: Authenticator exception caught.", ae);
753 }
754
755 return userList;
756 }
757
758 public List<User> getPossibleOwners(Issue issue, Integer projectId, Integer userId) {
759 HashSet<User> users = new HashSet<User>();
760
761 List<User> editUsers = getUsersWithProjectPermission(projectId, UserUtilities.PERMISSION_EDIT, true);
762 for (int i = 0; i < editUsers.size(); i++) {
763 users.add(editUsers.get(i));
764 }
765 List<User> otherUsers = getUsersWithProjectPermission(projectId, new int[]{UserUtilities.PERMISSION_EDIT_USERS, UserUtilities.PERMISSION_ASSIGNABLE}, true, true);
766 for (int i = 0; i < otherUsers.size(); i++) {
767 users.add(otherUsers.get(i));
768 }
769
770 if (issue != null) {
771
772
773 User creator = issue.getCreator();
774
775 if (UserUtilities.hasPermission(getUsersMapOfProjectIdsAndSetOfPermissionTypes(creator, 0), projectId,
776 UserUtilities.PERMISSION_EDIT_USERS)) {
777 users.add(creator);
778 }
779 if (issue.getOwner() != null) {
780 User owner = issue.getOwner();
781 users.add(owner);
782 }
783 } else if (userId != null) {
784
785 User creator = getUser(userId);
786 if (UserUtilities.hasPermission(getUsersMapOfProjectIdsAndSetOfPermissionTypes(creator, 0), projectId,
787 UserUtilities.PERMISSION_EDIT_USERS)) {
788 users.add(creator);
789 }
790 }
791
792 int i = 0;
793 List<User> userList = new ArrayList<User>();
794 for (Iterator<User> iter = users.iterator(); iter.hasNext(); i++) {
795 userList.add((User) iter.next());
796 }
797 return userList;
798 }
799
800 public User checkLogin(String login, Object authentication, int authType, int reqSource)
801 throws AuthenticatorException {
802 try {
803 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
804 if (authenticator != null) {
805 HashMap<String, Object> values = new HashMap<String, Object>();
806 values.put("userService", this);
807 values.put("configurationService", configurationService);
808 authenticator.initialize(values);
809 return authenticator.checkLogin(login, authentication, authType,
810 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
811 }
812
813 logger.error("Unable to create new authenticator.");
814 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
815 } catch (IllegalAccessException iae) {
816 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
817 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
818 } catch (InstantiationException ie) {
819 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
820 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
821 } catch (ClassCastException cce) {
822 logger.error("Authenticator class " + authenticatorClassName
823 + " does not extend the PluggableAuthenticator class.");
824 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
825 }
826 }
827
828 public boolean allowRegistration(User user, Object authentication, int authType, int reqSource)
829 throws AuthenticatorException {
830 try {
831 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
832 if (authenticator != null) {
833 HashMap<String, Object> values = new HashMap<String, Object>();
834 values.put("userService", this);
835 values.put("configurationService", configurationService);
836 authenticator.initialize(values);
837 if (authenticator.allowProfileCreation(user, authentication, authType,
838 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource))) {
839 return authenticator.allowRegistration(user, authentication, authType,
840 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
841 }
842 return false;
843 }
844
845 logger.error("Unable to create new authenticator.");
846 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
847 } catch (IllegalAccessException iae) {
848 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
849 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
850 } catch (InstantiationException ie) {
851 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
852 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
853 } catch (ClassCastException cce) {
854 logger.error("Authenticator class " + authenticatorClassName
855 + " does not extend the PluggableAuthenticator class.");
856 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
857 }
858 }
859
860 public boolean allowProfileCreation(User user, Object authentication, int authType, int reqSource)
861 throws AuthenticatorException {
862 try {
863 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
864 if (authenticator != null) {
865 HashMap<String, Object> values = new HashMap<String, Object>();
866 values.put("userService", this);
867 values.put("configurationService", configurationService);
868 authenticator.initialize(values);
869 return authenticator.allowProfileCreation(user, authentication, authType,
870 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
871 }
872
873 logger.error("Unable to create new authenticator.");
874 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
875 } catch (IllegalAccessException iae) {
876 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
877 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
878 } catch (InstantiationException ie) {
879 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
880 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
881 } catch (ClassCastException cce) {
882 logger.error("Authenticator class " + authenticatorClassName
883 + " does not extend the PluggableAuthenticator class.");
884 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
885 }
886 }
887
888 public boolean allowProfileUpdates(User user, Object authentication, int authType, int reqSource)
889 throws AuthenticatorException {
890 try {
891 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
892 if (authenticator != null) {
893 HashMap<String, Object> values = new HashMap<String, Object>();
894 values.put("userService", this);
895 values.put("configurationService", configurationService);
896 authenticator.initialize(values);
897 return authenticator.allowProfileUpdates(user, authentication, authType,
898 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
899 }
900
901 logger.error("Unable to create new authenticator.");
902 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
903 } catch (IllegalAccessException iae) {
904 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
905 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
906 } catch (InstantiationException ie) {
907 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
908 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
909 } catch (ClassCastException cce) {
910 logger.error("Authenticator class " + authenticatorClassName
911 + " does not extend the PluggableAuthenticator class.");
912 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
913 }
914 }
915
916 public boolean allowPasswordUpdates(User user, Object authentication, int authType, int reqSource)
917 throws AuthenticatorException {
918 try {
919 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
920 if (authenticator != null) {
921 HashMap<String, Object> values = new HashMap<String, Object>();
922 values.put("userService", this);
923 values.put("configurationService", configurationService);
924 authenticator.initialize(values);
925 return authenticator.allowPasswordUpdates(user, authentication, authType,
926 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
927 }
928
929 logger.error("Unable to create new authenticator.");
930 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
931 } catch (IllegalAccessException iae) {
932 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
933 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
934 } catch (InstantiationException ie) {
935 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
936 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
937 } catch (ClassCastException cce) {
938 logger.error("Authenticator class " + authenticatorClassName
939 + " does not extend the PluggableAuthenticator class.");
940 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
941 }
942 }
943
944 public boolean allowPermissionUpdates(User user, Object authentication, int authType, int reqSource)
945 throws AuthenticatorException {
946 try {
947 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
948 if (authenticator != null) {
949 HashMap<String, Object> values = new HashMap<String, Object>();
950 values.put("userService", this);
951 values.put("configurationService", configurationService);
952 authenticator.initialize(values);
953 return authenticator.allowPermissionUpdates(user, authentication, authType,
954 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
955 }
956
957 logger.error("Unable to create new authenticator.");
958 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
959 } catch (IllegalAccessException iae) {
960 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
961 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
962 } catch (InstantiationException ie) {
963 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
964 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
965 } catch (ClassCastException cce) {
966 logger.error("Authenticator class " + authenticatorClassName
967 + " does not extend the PluggableAuthenticator class.");
968 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
969 }
970 }
971
972 public boolean allowPreferenceUpdates(User user, Object authentication, int authType, int reqSource)
973 throws AuthenticatorException {
974 try {
975 PluggableAuthenticator authenticator = (PluggableAuthenticator) authenticatorClass.newInstance();
976 if (authenticator != null) {
977 HashMap<String, Object> values = new HashMap<String, Object>();
978 values.put("userService", this);
979 values.put("configurationService", configurationService);
980 authenticator.initialize(values);
981 return authenticator.allowPreferenceUpdates(user, authentication, authType,
982 (reqSource == 0 ? AuthenticationConstants.REQ_SOURCE_UNKNOWN : reqSource));
983 }
984
985 logger.error("Unable to create new authenticator.");
986 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
987 } catch (IllegalAccessException iae) {
988 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
989 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
990 } catch (InstantiationException ie) {
991 logger.error("Authenticator class " + authenticatorClassName + " can not be instantiated.");
992 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
993 } catch (ClassCastException cce) {
994 logger.error("Authenticator class " + authenticatorClassName
995 + " does not extend the PluggableAuthenticator class.");
996 throw new AuthenticatorException(AuthenticatorException.SYSTEM_ERROR);
997 }
998 }
999
1000
1001
1002
1003
1004 }