001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: UserSessionBean.java 30577 2008-02-26 13:46:19Z ogrisel $
018 */
019
020package org.nuxeo.ecm.webapp.security;
021
022import static org.jboss.seam.ScopeType.SESSION;
023
024import java.io.Serializable;
025import java.security.Principal;
026
027import javax.faces.context.FacesContext;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.jboss.seam.annotations.Destroy;
032import org.jboss.seam.annotations.Factory;
033import org.jboss.seam.annotations.Name;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.annotations.Startup;
036import org.nuxeo.ecm.core.api.NuxeoPrincipal;
037
038@Startup
039@Name("userSession")
040@Scope(SESSION)
041public class UserSessionBean implements Serializable, UserSession {
042
043    private static final long serialVersionUID = 7639281445209754L;
044
045    private Principal currentUser;
046
047    private static final Log log = LogFactory.getLog(UserSessionBean.class);
048
049    @Factory(value = "currentUser", scope = SESSION)
050    public Principal getCurrentUser() {
051        if (currentUser == null) {
052            FacesContext fContext = FacesContext.getCurrentInstance();
053            if (fContext == null) {
054                currentUser = null;
055                log.error("Can not fetch user principal from FacesContext: "
056                        + "there is no FacesContext attached to the current request");
057            } else {
058                // if seam identify filter is available, we can not get the UserPrincipal directly from the request
059                // currentUser =
060                // ((HttpServletRequest)((HttpServletRequestWrapper)(fContext.getExternalContext().getRequest())).getRequest()).getUserPrincipal();
061                currentUser = fContext.getExternalContext().getUserPrincipal();
062            }
063        }
064        return currentUser;
065    }
066
067    @Factory(value = "currentNuxeoPrincipal", scope = SESSION)
068    public NuxeoPrincipal getCurrentNuxeoPrincipal() {
069        return (NuxeoPrincipal) getCurrentUser();
070    }
071
072    public boolean isAdministrator() {
073        NuxeoPrincipal user = getCurrentNuxeoPrincipal();
074        if (user == null) {
075            return false;
076        } else {
077            return user.isAdministrator();
078        }
079    }
080
081    @Destroy
082    public void destroy() {
083    }
084
085}