View Javadoc

1   /*******************************************************************************
2    *  Imixs Workflow 
3    *  Copyright (C) 2001, 2011 Imixs Software Solutions GmbH,  
4    *  http://www.imixs.com
5    *  
6    *  This program is free software; you can redistribute it and/or 
7    *  modify it under the terms of the GNU General Public License 
8    *  as published by the Free Software Foundation; either version 2 
9    *  of the License, or (at your option) any later version.
10   *  
11   *  This program is distributed in the hope that it will be useful, 
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
14   *  General Public License for more details.
15   *  
16   *  You can receive a copy of the GNU General Public
17   *  License at http://www.gnu.org/licenses/gpl.html
18   *  
19   *  Project: 
20   *  	http://www.imixs.org
21   *  	http://java.net/projects/imixs-workflow
22   *  
23   *  Contributors:  
24   *  	Imixs Software Solutions GmbH - initial API and implementation
25   *  	Ralph Soika - Software Developer
26   *******************************************************************************/
27  
28  package org.imixs.workflow.jee.faces;
29  
30  import javax.faces.context.ExternalContext;
31  import javax.faces.context.FacesContext;
32  import javax.faces.event.ActionEvent;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpSession;
35  
36  /**
37   * This Backing Bean acts as a Login Helper Class. Can be used to identify the
38   * login state
39   * 
40   * @author rsoika
41   * 
42   */
43  public class LoginMB {
44  
45  	/**
46  	 * returns true if user is authenticated 
47  	 * @return
48  	 */
49  	public boolean isAuthenticated() {		
50  		return (getUserPrincipal() != null);
51  	}
52  
53  	/**
54  	 * returns the userPrincipal Name 
55  	 * @return
56  	 */
57  	public String getUserPrincipal() {
58  		FacesContext context = FacesContext.getCurrentInstance();
59  		ExternalContext externalContext = context.getExternalContext();
60  		return externalContext.getUserPrincipal() != null ? externalContext
61  				.getUserPrincipal().toString() : null;
62  	}
63  
64  	/**
65  	 * returns the remote user Name
66  	 * @return
67  	 */
68  	public String getRemoteUser() {
69  		FacesContext context = FacesContext.getCurrentInstance();
70  		ExternalContext externalContext = context.getExternalContext();
71  		String remoteUser = externalContext.getRemoteUser();
72  		return remoteUser;
73  	}
74  
75  	/**
76  	 * returns the full qualified server URI from the current web context
77  	 * @return
78  	 */
79  	public String getServerURI() {
80  		HttpServletRequest servletRequest = (HttpServletRequest) FacesContext
81  				.getCurrentInstance().getExternalContext().getRequest();
82  
83  		
84  		String port=""+servletRequest.getLocalPort();
85  		
86  		String server = servletRequest.getServerName();
87  		return "http://"+ server+":"+port+"";
88  
89  	}
90  
91  	/**
92  	 * invalidates the current user session
93  	 * @param event
94  	 */
95  	public void doLogout(ActionEvent event) {
96  		FacesContext context = FacesContext.getCurrentInstance();
97  		ExternalContext externalContext = context.getExternalContext();
98  
99  		HttpSession session = (HttpSession) externalContext.getSession(false);
100 
101 		session.invalidate();
102 
103 	}
104 
105 }