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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.ecm.automation.core.operations.document;
020
021import java.io.Serializable;
022import java.util.Calendar;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.DocumentRef;
035import org.nuxeo.ecm.core.api.security.ACE;
036import org.nuxeo.ecm.core.api.security.ACL;
037
038/**
039 * Replaces a given ACE.
040 *
041 * @since 7.10
042 */
043@Operation(id = ReplacePermission.ID, category = Constants.CAT_DOCUMENT, label = "Replace Permission", description = "Replace a given permission on the input document(s). Returns the document(s).")
044public class ReplacePermission {
045
046    public static final String ID = "Document.ReplacePermission";
047
048    public static final String NOTIFY_KEY = "notify";
049
050    public static final String COMMENT_KEY = "comment";
051
052    @Context
053    protected CoreSession session;
054
055    @Param(name = "username", alias = "user", description = "ACE target user/group.")
056    protected String user;
057
058    @Param(name = "permission", description = "ACE permission.")
059    String permission;
060
061    @Param(name = "acl", required = false, values = { ACL.LOCAL_ACL }, description = "ACL name.")
062    String aclName = ACL.LOCAL_ACL;
063
064    @Param(name = "begin", required = false, description = "ACE begin date.")
065    Calendar begin;
066
067    @Param(name = "end", required = false, description = "ACE end date.")
068    Calendar end;
069
070    @Param(name = "id", description = "ACE id.")
071    String id;
072
073    @Param(name = "notify", required = false, description = "Notify the user or not")
074    boolean notify = false;
075
076    @Param(name = "comment", required = false, description = "Comment")
077    String comment;
078
079    @OperationMethod(collector = DocumentModelCollector.class)
080    public DocumentModel run(DocumentModel doc) {
081        replacePermission(doc);
082        return session.getDocument(doc.getRef());
083    }
084
085    @OperationMethod(collector = DocumentModelCollector.class)
086    public DocumentModel run(DocumentRef docRef) {
087        DocumentModel doc = session.getDocument(docRef);
088        replacePermission(doc);
089        return doc;
090    }
091
092    protected void replacePermission(DocumentModel doc) {
093        Map<String, Serializable> contextData = new HashMap<>();
094        contextData.put(NOTIFY_KEY, notify);
095        contextData.put(COMMENT_KEY, comment);
096
097        ACE oldACE = ACE.fromId(id);
098
099        ACE newACE = ACE.builder(user, permission)
100                        .creator(session.getPrincipal().getName())
101                        .begin(begin)
102                        .end(end)
103                        .contextData(contextData)
104                        .build();
105
106        session.replaceACE(doc.getRef(), aclName, oldACE, newACE);
107    }
108
109}