001/*
002 * (C) Copyright 2010 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 */
019package org.nuxeo.ecm.platform.routing.web;
020
021import java.io.Serializable;
022
023import org.jboss.seam.ScopeType;
024import org.jboss.seam.annotations.In;
025import org.jboss.seam.annotations.Install;
026import org.jboss.seam.annotations.Name;
027import org.jboss.seam.annotations.Scope;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
032import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
037 */
038@Scope(ScopeType.CONVERSATION)
039@Name("routeSecurityChecker")
040@Install(precedence = Install.FRAMEWORK)
041public class RouteSecurityChecker implements Serializable {
042
043    private static final long serialVersionUID = 1L;
044
045    @In(required = true, create = false)
046    protected NuxeoPrincipal currentUser;
047
048    @In(required = true, create = false)
049    protected CoreSession documentManager;
050
051    @In(create = true)
052    protected transient NavigationContext navigationContext;
053
054    @Deprecated
055    /**
056     * @deprecated use
057     *             {@link #canValidateRoute(DocumentModel)}
058     *             instead.
059     */
060    public boolean canValidateRoute() {
061        DocumentModel currentDoc = navigationContext.getCurrentDocument();
062        if (!documentManager.hasChildren(currentDoc.getRef())) {
063            // Cannot validate an empty route
064            return false;
065        }
066        return getDocumentRoutingService().canUserValidateRoute(currentUser);
067    }
068
069    public boolean canValidateRoute(DocumentModel routeDocument) {
070        return canValidateRoute() || getDocumentRoutingService().canValidateRoute(routeDocument, documentManager);
071    }
072
073    public DocumentRoutingService getDocumentRoutingService() {
074        return Framework.getService(DocumentRoutingService.class);
075    }
076
077}