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;
027import java.security.Principal;
028
029import javax.faces.context.FacesContext;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.jboss.seam.annotations.Destroy;
034import org.jboss.seam.annotations.Factory;
035import org.jboss.seam.annotations.Name;
036import org.jboss.seam.annotations.Scope;
037import org.jboss.seam.annotations.Startup;
038import org.nuxeo.ecm.core.api.NuxeoPrincipal;
039
040@Startup
041@Name("userSession")
042@Scope(SESSION)
043public class UserSessionBean implements Serializable, UserSession {
044
045    private static final long serialVersionUID = 7639281445209754L;
046
047    private Principal currentUser;
048
049    private static final Log log = LogFactory.getLog(UserSessionBean.class);
050
051    @Factory(value = "currentUser", scope = SESSION)
052    public Principal getCurrentUser() {
053        if (currentUser == null) {
054            FacesContext fContext = FacesContext.getCurrentInstance();
055            if (fContext == null) {
056                currentUser = null;
057                log.error("Can not fetch user principal from FacesContext: "
058                        + "there is no FacesContext attached to the current request");
059            } else {
060                // if seam identify filter is available, we can not get the UserPrincipal directly from the request
061                // currentUser =
062                // ((HttpServletRequest)((HttpServletRequestWrapper)(fContext.getExternalContext().getRequest())).getRequest()).getUserPrincipal();
063                currentUser = fContext.getExternalContext().getUserPrincipal();
064            }
065        }
066        return currentUser;
067    }
068
069    @Factory(value = "currentNuxeoPrincipal", scope = SESSION)
070    public NuxeoPrincipal getCurrentNuxeoPrincipal() {
071        return (NuxeoPrincipal) getCurrentUser();
072    }
073
074    public boolean isAdministrator() {
075        NuxeoPrincipal user = getCurrentNuxeoPrincipal();
076        if (user == null) {
077            return false;
078        } else {
079            return user.isAdministrator();
080        }
081    }
082
083    @Destroy
084    public void destroy() {
085    }
086
087}