KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > control > LoginController


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.war.control;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.ServletException JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.acegisecurity.Authentication;
33 import org.acegisecurity.context.SecurityContext;
34 import org.acegisecurity.context.SecurityContextHolder;
35 import org.acegisecurity.userdetails.UserDetails;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.springframework.web.servlet.ModelAndView;
39
40 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
41 import com.jaspersoft.jasperserver.api.metadata.user.domain.Role;
42 import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
43 import com.jaspersoft.jasperserver.api.metadata.user.domain.impl.client.MetadataUserDetails;
44
45 /**
46  * @author swood
47  *
48  */

49 public class LoginController extends JRBaseMultiActionController {
50     
51     private static Log log = LogFactory.getLog(LoginController.class);
52
53     /**
54      * Ordered list of home pages
55      */

56     private List JavaDoc homePageByRole;
57     private List JavaDoc homePageByRoleEntries;
58     private String JavaDoc defaultHomePage = "home";
59
60     /*
61      * Overridden method for handling the requests
62      * @args HttpServletRequest, HttpServletResponse
63      * @returns ModelAndView - Home Page
64      */

65     public ModelAndView homePage(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
66             throws ServletException JavaDoc {
67
68         if ((SecurityContextHolder.getContext() == null)
69             || !(SecurityContextHolder.getContext() instanceof SecurityContext)
70             || (((SecurityContext) SecurityContextHolder.getContext())
71             .getAuthentication() == null)) {
72             // Should never get here!
73
return new ModelAndView("loginError");
74         }
75
76         Authentication auth = SecurityContextHolder.getContext()
77                                                    .getAuthentication();
78
79         if (auth.getPrincipal() == null) {
80             // Should never get here!
81
return new ModelAndView("home");
82         }
83
84         User user = (User) auth.getPrincipal();
85         return new ModelAndView(getBestHomeURLForUser(user));
86     }
87
88     /**
89      * If successfully logged in, make sure we have a user in our database that corresponds
90      * to the UserDetails we have been given. This allows users that are managed and authenticated
91      * externally to us to become part of the environment.
92      *
93      * Also forward on to the correct home page for the user, based on external configuration.
94      *
95      * @param req
96      * @param res
97      * @return
98      * @throws ServletException
99      */

100     public ModelAndView loginSuccess(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
101         throws ServletException JavaDoc {
102
103         if ((SecurityContextHolder.getContext() == null)
104             || !(SecurityContextHolder.getContext() instanceof SecurityContext)
105             || (((SecurityContext) SecurityContextHolder.getContext())
106             .getAuthentication() == null)) {
107             // Should never get here!
108
return new ModelAndView("loginError");
109         }
110
111         Authentication auth = SecurityContextHolder.getContext()
112                                                    .getAuthentication();
113
114         if (auth.getPrincipal() == null) {
115             // Should never get here!
116
return new ModelAndView("loginError");
117         }
118         
119         User user;
120         
121         log.debug("Authentication class: " + auth.getClass() +
122                 " Authentication principal class: " + auth.getPrincipal().getClass());
123         
124         if (!(auth.getPrincipal() instanceof MetadataUserDetails)) {
125             
126             UserDetails userDetails = (UserDetails) auth.getPrincipal();
127
128             log.debug("External user: " + userDetails.getUsername());
129
130             user = getUserAuthService().getUser(new ExecutionContextImpl(), userDetails.getUsername());
131         } else {
132             user = (User) auth.getPrincipal();
133         }
134         
135         return new ModelAndView(getBestHomeURLForUser(user));
136     }
137     
138     /**
139      * Given a user, find their home page based on their role
140      *
141      * @param User user
142      * @return Home page URL for user
143      */

144     private String JavaDoc getBestHomeURLForUser(User user) {
145         if (getHomePageByRole() == null || getHomePageByRole().size() == 0) {
146             log.debug("Set home page for user: " + user.getUsername() + " to default: " + getDefaultHomePage());
147             return getDefaultHomePage();
148         }
149         Iterator JavaDoc it = getHomePageByRoleEntries().iterator();
150         while (it.hasNext()) {
151             RoleHomePage entry = (RoleHomePage) it.next();
152             
153             if (hasRole(user, entry.getRoleName())) {
154                 log.debug("Set home page for user: " + user.getUsername() +
155                                 " based on role: " + entry.getRoleName() +
156                                 " to: " + entry.getHomePageURL());
157                 return entry.getHomePageURL();
158             }
159             
160         }
161         log.debug("Set home page for user: " + user.getUsername() + " to default: " + getDefaultHomePage());
162         return getDefaultHomePage();
163     }
164     
165     /**
166      * Does this user have a role of this name?
167      *
168      * @param User u
169      * @param String roleName
170      * @return true if user has a role with the given name
171      */

172     private boolean hasRole(User u, String JavaDoc roleName) {
173         if (u == null || u.getRoles().size() == 0) {
174             log.debug("No roles on user: " + u + " to check for role: " + roleName);
175             return false;
176         }
177         Iterator JavaDoc it = u.getRoles().iterator();
178         while (it.hasNext()) {
179             Role r = (Role) it.next();
180             if (r.getRoleName().equalsIgnoreCase(roleName)) {
181                 log.debug("Found role " + roleName + " on user: " + u);
182                 return true;
183             }
184         }
185         log.debug("Found NO role " + roleName + " on user: " + u);
186         return false;
187     }
188
189     /**
190      * @return Returns the defaultHomePage.
191      */

192     public String JavaDoc getDefaultHomePage() {
193         return defaultHomePage;
194     }
195
196     /**
197      * @param defaultHomePage The defaultHomePage to set.
198      */

199     public void setDefaultHomePage(String JavaDoc defaultHomePage) {
200         this.defaultHomePage = defaultHomePage;
201     }
202
203     /**
204      * @return Returns the homePageByRole.
205      */

206     public List JavaDoc getHomePageByRole() {
207         return homePageByRole;
208     }
209
210     /**
211      * Converts basic Spring list to Map.Entry
212      *
213      * @param homePageByRole The homePageByRole to set.
214      */

215     public void setHomePageByRole(List JavaDoc homePageByRole) {
216         this.homePageByRole = homePageByRole;
217         
218         if (homePageByRole == null) {
219             setHomePageByRoleEntries(null);
220             return;
221         }
222         
223         List JavaDoc entriesList = new ArrayList JavaDoc(homePageByRole.size());
224         
225         Iterator JavaDoc it = homePageByRole.iterator();
226         
227         while (it.hasNext()) {
228             String JavaDoc str = (String JavaDoc) it.next();
229             
230             int pos = str.indexOf('|');
231             
232             if (pos == -1) {
233                 throw new RuntimeException JavaDoc("Invalid home page entry (needs | to separate role and URL:" + str);
234             }
235             
236             RoleHomePage entry = new RoleHomePage(str.substring(0, pos),str.substring(pos + 1));
237             entriesList.add(entry);
238         }
239         
240         setHomePageByRoleEntries(entriesList);
241         
242     }
243
244     /**
245      * @return Returns the homePageByRoleEntries.
246      */

247     public List JavaDoc getHomePageByRoleEntries() {
248         return homePageByRoleEntries;
249     }
250
251     /**
252      * @param homePageByRoleEntries The homePageByRoleEntries to set.
253      */

254     public void setHomePageByRoleEntries(List JavaDoc homePageByRoleEntries) {
255         this.homePageByRoleEntries = homePageByRoleEntries;
256     }
257     
258     private class RoleHomePage {
259         private String JavaDoc roleName;
260         private String JavaDoc homePageURL;
261         
262         public RoleHomePage(String JavaDoc roleName, String JavaDoc homePageURL) {
263             this.roleName = roleName;
264             this.homePageURL = homePageURL;
265         }
266         
267         /**
268          * @return Returns the homePageURL.
269          */

270         public String JavaDoc getHomePageURL() {
271             return homePageURL;
272         }
273         /**
274          * @param homePageURL The homePageURL to set.
275          */

276         public void setHomePageURL(String JavaDoc homePageURL) {
277             this.homePageURL = homePageURL;
278         }
279         /**
280          * @return Returns the roleName.
281          */

282         public String JavaDoc getRoleName() {
283             return roleName;
284         }
285         /**
286          * @param roleName The roleName to set.
287          */

288         public void setRoleName(String JavaDoc roleName) {
289             this.roleName = roleName;
290         }
291     }
292
293 }
294
Popular Tags