001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.core.operations.document;
018
019import java.io.Serializable;
020import java.util.Calendar;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
030import org.nuxeo.ecm.core.api.ClientException;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.api.security.ACE;
035import org.nuxeo.ecm.core.api.security.ACL;
036
037/**
038 * Updates a given ACE.
039 * <p>
040 * Updates only the non-null fields, otherwise keep the ones of the old ACE.
041 *
042 * @since 7.4
043 */
044@Operation(id = UpdatePermission.ID, category = Constants.CAT_DOCUMENT, label = "Add Permission", description = "Update a given permission on the input document(s). Returns the document(s).")
045public class UpdatePermission {
046
047    public static final String ID = "Document.UpdatePermission";
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;
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        updatePermission(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        updatePermission(doc);
090        return doc;
091    }
092
093    protected void updatePermission(DocumentModel doc) throws ClientException {
094        Map<String, Serializable> contextData = new HashMap<>();
095        if (notify != null && notify) {
096            contextData.put(NOTIFY_KEY, true);
097            if (comment != null) {
098                contextData.put(COMMENT_KEY, comment);
099            }
100        }
101
102        ACE oldACE = ACE.fromId(id);
103        String username = user != null ? user : oldACE.getUsername();
104        String permission = this.permission != null ? this.permission : oldACE.getPermission();
105        String creator = session.getPrincipal().getName();
106
107        ACE newACE = ACE.builder(username, permission)
108                        .creator(creator)
109                        .begin(begin != null ? begin : oldACE.getBegin())
110                        .end(end != null ? end : oldACE.getEnd())
111                        .contextData(contextData)
112                        .build();
113
114        session.replaceACE(doc.getRef(), aclName, oldACE, newACE);
115    }
116
117}