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