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