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