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