001/*
002 * (C) Copyright 2015 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.automation.core.operations.document;
021
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.api.security.ACL;
032import org.nuxeo.ecm.core.api.security.ACP;
033import org.nuxeo.ecm.core.api.security.impl.ACPImpl;
034
035/**
036 * Operation blocking permission inheritance on a given ACL.
037 *
038 * @since 7.4
039 */
040@Operation(id = BlockPermissionInheritance.ID, category = Constants.CAT_DOCUMENT, label = "Block Permission Inheritance", description = "Block the permission inheritance on the input document(s). Returns the document(s).")
041public class BlockPermissionInheritance {
042
043    public static final String ID = "Document.BlockPermissionInheritance";
044
045    @Context
046    protected CoreSession session;
047
048    @Param(name = "acl", required = false, values = { ACL.LOCAL_ACL }, description = "ACL name.")
049    String aclName = ACL.LOCAL_ACL;
050
051    @OperationMethod(collector = DocumentModelCollector.class)
052    public DocumentModel run(DocumentModel doc) {
053        blockPermissionInheritance(doc);
054        return session.getDocument(doc.getRef());
055    }
056
057    @OperationMethod(collector = DocumentModelCollector.class)
058    public DocumentModel run(DocumentRef docRef) {
059        DocumentModel doc = session.getDocument(docRef);
060        blockPermissionInheritance(doc);
061        return doc;
062    }
063
064    protected void blockPermissionInheritance(DocumentModel doc) {
065        ACP acp = doc.getACP() != null ? doc.getACP() : new ACPImpl();
066        String username = session.getPrincipal().getName();
067        boolean permissionChanged = acp.blockInheritance(aclName, username);
068        if (permissionChanged) {
069            doc.setACP(acp, true);
070        }
071    }
072}