001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: UserSessionBean.java 30577 2008-02-26 13:46:19Z ogrisel $
020 */
021
022package org.nuxeo.ecm.webapp.security;
023
024import static org.jboss.seam.ScopeType.SESSION;
025
026import java.io.Serializable;
027
028import javax.faces.context.FacesContext;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.jboss.seam.annotations.Destroy;
033import org.jboss.seam.annotations.Factory;
034import org.jboss.seam.annotations.Name;
035import org.jboss.seam.annotations.Scope;
036import org.jboss.seam.annotations.Startup;
037import org.nuxeo.ecm.core.api.NuxeoPrincipal;
038
039@Startup
040@Name("userSession")
041@Scope(SESSION)
042public class UserSessionBean implements Serializable, UserSession {
043
044    private static final long serialVersionUID = 7639281445209754L;
045
046    private NuxeoPrincipal currentUser;
047
048    private static final Log log = LogFactory.getLog(UserSessionBean.class);
049
050    @Factory(value = "currentUser", scope = SESSION)
051    public NuxeoPrincipal getCurrentUser() {
052        if (currentUser == null) {
053            FacesContext fContext = FacesContext.getCurrentInstance();
054            if (fContext == null) {
055                currentUser = null;
056                log.error("Can not fetch user principal from FacesContext: "
057                        + "there is no FacesContext attached to the current request");
058            } else {
059                // if seam identify filter is available, we can not get the UserPrincipal directly from the request
060                // currentUser =
061                // ((HttpServletRequest)((HttpServletRequestWrapper)(fContext.getExternalContext().getRequest())).getRequest()).getUserPrincipal();
062                currentUser = (NuxeoPrincipal) fContext.getExternalContext().getUserPrincipal();
063            }
064        }
065        return currentUser;
066    }
067
068    @Factory(value = "currentNuxeoPrincipal", scope = SESSION)
069    public NuxeoPrincipal getCurrentNuxeoPrincipal() {
070        return getCurrentUser();
071    }
072
073    public boolean isAdministrator() {
074        NuxeoPrincipal user = getCurrentNuxeoPrincipal();
075        if (user == null) {
076            return false;
077        } else {
078            return user.isAdministrator();
079        }
080    }
081
082    @Destroy
083    public void destroy() {
084    }
085
086}