001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.automation.core.operations.services.directory;
019
020import org.jboss.el.ExpressionFactoryImpl;
021import org.nuxeo.ecm.automation.OperationContext;
022import org.nuxeo.ecm.core.api.NuxeoException;
023import org.nuxeo.ecm.core.api.NuxeoPrincipal;
024import org.nuxeo.ecm.platform.actions.ActionContext;
025import org.nuxeo.ecm.platform.actions.ELActionContext;
026import org.nuxeo.ecm.platform.actions.ejb.ActionManager;
027import org.nuxeo.ecm.platform.el.ExpressionContext;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Base class for operations on directories.
032 *
033 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
034 * @since 5.7
035 */
036public class AbstractDirectoryOperation {
037
038    protected void validateCanManageDirectories(OperationContext ctx) {
039        if (!canManageDirectories(ctx)) {
040            throw new NuxeoException("Unauthorized user");
041        }
042    }
043
044    protected boolean canManageDirectories(OperationContext ctx) {
045        ActionManager actionManager = Framework.getLocalService(ActionManager.class);
046        return actionManager.checkFilter("directoriesManagementAccess", createActionContext(ctx));
047    }
048
049    protected ActionContext createActionContext(OperationContext ctx) {
050        ActionContext actionContext = new ELActionContext(new ExpressionContext(), new ExpressionFactoryImpl());
051        actionContext.setDocumentManager(ctx.getCoreSession());
052        actionContext.setCurrentPrincipal((NuxeoPrincipal) ctx.getPrincipal());
053        return actionContext;
054    }
055}