View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.businessfacades.security;
16  
17  import org.figure8.join.core.DuplicateEntityException;
18  import org.figure8.join.businessobjects.security.User;
19  import org.figure8.join.businessobjects.security.persistence.UserDao;
20  import org.figure8.join.services.security.PasswordEncoder;
21  import org.figure8.join.services.security.InvalidLoginException;
22  import org.figure8.join.view.UserView;
23  import org.figure8.join.util.LogUtil;
24  
25  import org.apache.commons.logging.Log;
26  
27  import java.util.List;
28  /**
29   * Default implementation of <code>UserManager</code>.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version $Revision: 1.2 $
32   */
33  public class DefaultUserManager implements UserManager{
34  
35     // Static -------------------------------------------------------------------
36  
37     /** Get a commons logger. */
38     private static Log log = LogUtil.getLog(DefaultUserManager.class);
39  
40  
41     // Attributes ---------------------------------------------------------------
42  
43     /** User data access object */
44     protected UserDao dao = null;
45     /** Password encryption and check helper object */
46     protected PasswordEncoder passwordEncoder = null;
47     /** Permission manager for retrieving security rghts */
48     protected PermissionManager permissionManager = null;
49  
50  
51     // Constructors -------------------------------------------------------------
52  
53     /** Creates a new instance of DefaultUserManager */
54     public DefaultUserManager(){
55     }
56  
57  
58     // Public -------------------------------------------------------------------
59  
60     /** @param dao A User data access object */
61     public void setUserDao(UserDao dao){
62        this.dao = dao;
63     }
64  
65  
66     // Implementation of UserManager --------------------------------------------
67  
68     /**
69      * The setPasswordEncoder() method must be called during the implementation
70      * initialization phase. It must allowed the registration of a PasswordEncoder
71      * for the deployed Join application.
72      * @param encoder Implementation of PaswordEncoder to check and encode users password
73      */
74     public void setPasswordEncoder(PasswordEncoder encoder){
75        this.passwordEncoder = encoder;
76     }
77  
78     /**
79      * The setPermissionManager() method must be called during the implementation
80      * initilization phase. It must allowed the registration of a PermissionManager
81      * useful for retrieving security permissions acquired by a user.
82      * @param manager Implementation of PermissionManager to retrieve security rights
83      */
84     public void setPermissionManager(PermissionManager manager){
85        this.permissionManager = manager;
86     }
87  
88     /**
89      * The login method is called when a user wishes to login to the Join application.
90      * This method <code>should</code> use the PasswordEncoder validatePassword() method for
91      * user's credential validation.
92      * @param userId The user identifier.
93      * @param userCredential The user credential.
94      * @return A ValueHolder object representing the user's view (data + permissions).
95      * @throws InvalidLoginException if the credentials are invalid.
96      */
97     public UserView login(String userId, String userCredential) throws InvalidLoginException{
98        if (log.isInfoEnabled())
99           log.info("Trying to login user with id: " + userId);
100 
101       // Retrieve user corresponding to identifier.
102       User foundUser = dao.getUser(userId);
103       if (foundUser != null){
104          // Check its credential using the password encoder.
105          if (passwordEncoder.validatePassword(userId, userCredential, foundUser.getPassword())){
106             // Build a view corresponding to user and containing its permissions.
107             List permissions = permissionManager.getUserPermissions(foundUser);
108             UserView view = new UserView(foundUser, permissions);
109             return view;
110          }
111          else{
112             // Throw an InvalidLoginException.
113             log.warn("Credential conflict for user with id:" + userId);
114             throw new InvalidLoginException("Credential conflict");
115          }
116       }
117       else{
118          // Throw an InvalidLoginException.
119          log.warn("Unkown user trying to loggin with id: " + userId);
120          throw new InvalidLoginException("Unknow user");
121       }
122    }
123 
124    /**
125     * Save or update a given <b>user</b> into datastore.<br/>
126     * Warning: this method should be used when first creating a user or
127     * when updating a user. In this 2 use-cases, password validation is required
128     * so password should be in clear text within the User object.
129     * @param user User object to save within datastore
130     * @throws org.figure8.join.core.DuplicateEntityException if a user with this login already exists.
131     */
132    public void saveUser(User user) throws DuplicateEntityException{
133       if (log.isDebugEnabled())
134          log.debug("Saving user with login: " + user.getLogin());
135       // Encode password before saving user.
136       String encodedPwd = passwordEncoder.encodePassword(user.getLogin(), user.getPassword(), null);
137       user.setPassword(encodedPwd);
138       
139       // If creation, check user with same login.
140       if (user.isTransient()){
141          User other = dao.getUser(user.getLogin());
142          if (other != null){
143             log.error("A User with same login already exists: " + other.getLogin());
144             throw new DuplicateEntityException("A User with same login already exists", other);
145          }
146          log.info("Creating a new user with login: " + user.getLogin() + ". Welcome !");
147       }
148       // Save it to datastore.
149       dao.save(user);
150    }
151 
152    /**
153     * Retrieves a specific User using its login identifier
154     * @param login The user to retrieve unique login
155     * @return The user having this specified login
156     */
157    public User getUser(String login){
158       if (log.isDebugEnabled())
159          log.debug("Retrieving user with login: " + login);
160       return dao.getUser(login);
161    }
162 
163    /**
164     * The getUsers method must be used when you want to list all users.
165     * @return List of <code>org.figure8.join.businessobjects.security.User</code>.
166     */
167    public List getUsers(){
168       // Call findAll() on dao.
169       List result = dao.findAll();
170       if (log.isDebugEnabled())
171          log.debug("Found " + result.size() + " users");
172       return result;
173    }
174 
175    /**
176     * The getUsers method must be used when you want to list users using lastname
177     * criterion. Users have their lastname "LIKE" given arguments.
178     * @param lastname String to use as criterion for user's lastname
179     * @return List of <code>org.figure8.join.businessobjects.security.User</code>.
180     */
181    public List getUsers(String lastname){
182       // Call findAll() if no lastname is specified.
183       List result = null;
184       if (lastname == null || lastname.trim().length() == 0)
185          result = dao.findAllSorted("lastname");
186       else
187          result = dao.getUsersWithLastnameLike(lastname);
188       if (log.isDebugEnabled())
189          log.debug("Found " + result.size() + " users with lastname like '" + lastname + "'");
190       return result;
191    }
192 }