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: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.webapp.context;
021
022import static org.jboss.seam.ScopeType.SESSION;
023
024import java.io.Serializable;
025import java.security.Principal;
026
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Observer;
030import org.jboss.seam.annotations.Scope;
031import org.jboss.seam.annotations.Startup;
032import org.jboss.seam.annotations.intercept.BypassInterceptors;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
035import org.nuxeo.ecm.platform.util.RepositoryLocation;
036import org.nuxeo.ecm.webapp.helpers.EventNames;
037
038/**
039 * This should not refer other beans that may require heavy initialization. This component is initialized and used
040 * earlier than some resources become available.
041 *
042 * @author DM
043 */
044@Name("userServicesContext")
045@Scope(SESSION)
046@Startup
047public class UserServicesContext implements Serializable {
048
049    private static final long serialVersionUID = -4938620211123775744L;
050
051    @In(create = true)
052    private transient NavigationContext navigationContext;
053
054    @In(required = false, create = true)
055    private transient Principal currentUser;
056
057    private transient RepositoryLocation repoLocation;
058
059    private transient Boolean serverLocationRetrieved = false;
060
061    private boolean isServerLocationSelected() {
062        if (!serverLocationRetrieved) {
063            repoLocation = navigationContext.getCurrentServerLocation();
064            serverLocationRetrieved = true;
065        }
066
067        return repoLocation != null;
068    }
069
070    @Observer(value = { EventNames.LOCATION_SELECTION_CHANGED }, create = false)
071    @BypassInterceptors
072    public void invalidate() {
073        repoLocation = null;
074        serverLocationRetrieved = false;
075    }
076
077    public Boolean getSearchEnabled() {
078        return isServerLocationSelected();
079    }
080
081    public Boolean getDashboardEnabled() {
082        return isServerLocationSelected();
083    }
084
085    public Boolean getUserManagerEnabled() {
086        return isServerLocationSelected();
087    }
088
089    /**
090     * Checks if an user has the right to see the vocabularies management link.
091     *
092     * @return - true if the user has this right<br>
093     *         - false otherwise
094     */
095    public boolean getVocabulariesEnabled() {
096        if (currentUser == null) {
097            return false;
098        } else {
099            return ((NuxeoPrincipal) currentUser).isAdministrator();
100        }
101    }
102
103}