View Javadoc

1   /***
2   Copyright (C) 2005 The Java Community
3   
4   This program is free software; you can redistribute it and/or modify  it under
5   the terms of the GNU General Public License as published by  the Free Software
6   Foundation; either version 2 of the License, or  (at your option) any later
7   version.
8   
9   This program is distributed in the hope that it will be useful,  but WITHOUT
10  ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11  FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License  along with
14  this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15  Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17  
18  package org.bejug.javacareers.jobs.view.jsf.model;
19  
20  import java.util.HashMap;
21  
22  import net.sf.acegisecurity.UserDetails;
23  import net.sf.acegisecurity.context.security.SecureContextUtils;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.bejug.javacareers.jobs.model.User;
28  import org.bejug.javacareers.jobs.service.AdminService;
29  import org.bejug.javacareers.jobs.view.jsf.util.Utils;
30  
31  /***
32   * @author kva (last modified by $Author: shally $)
33   * @version $Revision: 1.13 $ - $Date: 2005/12/20 15:36:47 $
34   */
35  public class UserTracker extends HashMap{
36      
37      private static final Log LOG = LogFactory.getLog(UserTracker.class);
38      /***
39       * key to retrieve user object stored on a session.
40       */
41      public static final String USER_KEY = "user";
42  
43      /***
44       * injected adminService.
45       */
46      private AdminService adminService;
47  
48      /***
49       * Current User
50       */
51      private User user;
52  
53      /***
54       * Get the user object for the user currently logged in.
55       * @return user object.
56       */
57      public User getCurrentUser() {
58          String username = getCurrentUserName();
59          //LOG.info("Debug: Current username: "+username);
60          if (null == username && user == null) {
61              LOG.info("Debug: Current username in user: "+user);
62              return null;
63          }
64  		if (user == null)
65  		{
66  		    user = adminService.getUserByUserName(username);
67  		}
68  		//LOG.info("Debug: Current username in user: "+user);
69  		return user;
70  
71      }
72      
73      /***
74       * Get the name of the user currently logged in.
75       * 
76       * @return the username of the user currently logged in, null if no
77       *         user is logged in.
78       */
79      public String getCurrentUserName() {
80          String username = null;
81  
82          Object principal = SecureContextUtils.getSecureContext()
83                  .getAuthentication().getPrincipal();
84          if (principal instanceof UserDetails) {
85              username = ((UserDetails) principal).getUsername();
86          }
87  
88          return username;
89      }
90  
91  
92      /***
93       * Retrieve the user. This user is stored as a session attribute.
94       * @return the user stored in the session.
95       */
96      public User getSessionUser() {
97          return (User) Utils.getSession().getAttribute(USER_KEY);
98      }
99  
100     /***
101      * Set the user. This user is stored as a session attribute.
102      * @param user the user to remember.
103      */
104     public void setSessionUser(User user) {
105         //LOG.info("Debug: Session User set to: "+user);
106         Utils.getSession().setAttribute(USER_KEY, user);
107     }
108 
109     /***
110      * @param adminService The adminService to set.
111      */
112     public void setAdminService(AdminService adminService) {
113         this.adminService = adminService;
114     }
115     
116     /***
117      * sets the current user
118      * @param user the user to set.
119      */
120     public void setCurrentUser(User user) {
121         //LOG.info("Debug: Current user set to: "+user);
122         this.user = user;
123     }
124 }
125 
126 /***
127  * $Log: UserTracker.java,v $
128  * Revision 1.13  2005/12/20 15:36:47  shally
129  * CheckStyle and PMD changes.
130  *
131  * Revision 1.12  2005/11/29 12:30:12  shally
132  * Added edit capabilities to admin
133  *
134  * Revision 1.11  2005/09/30 14:38:07  bavo_jcs
135  * Fixed URL
136  *
137  * Revision 1.10  2005/09/13 08:11:06  schauwvliege
138  * organize imports
139  *
140  * Revision 1.9  2005/08/30 13:07:48  psong09
141  * renamed author: psong09
142  *
143  * Revision 1.8  2005/08/29 09:53:46  psong09
144  * removed showLinesPerPage method
145  *
146  * Revision 1.7  2005/08/16 13:53:05  bavo_jcs
147  * Fixed issues with IssueTracker
148  *
149  * Revision 1.6  2005/08/16 09:10:12  bme_jcs
150  * checkstyle errors resolved
151  *
152  * Revision 1.5  2005/08/11 12:24:42  bavo_jcs
153  * Role change rule
154  *
155  * Revision 1.4  2005/08/11 12:17:03  bavo_jcs
156  * Role change rule
157  *
158  * Revision 1.3  2005/08/09 12:59:56  bavo_jcs
159  * Optimized imports
160  *
161  * Revision 1.2  2005/06/09 08:18:53  bejug_cc
162  * Fix initial import
163  *
164  * Revision 1.4  2005/05/26 14:10:21  PSONG09
165  * removed logging
166  *
167  * Revision 1.3  2005/05/23 15:11:53  PSONG09
168  * integration pdf indexing update
169  *
170  * Revision 1.2  2005/05/18 19:23:15  kva
171  * added getLinesPerPage
172  *
173  * Revision 1.1  2005/05/18 14:32:13  kva
174  * extracted methods in UserTracker
175  *
176  */