View Javadoc

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