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.permissions.operations;
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.ACE;
032import org.nuxeo.ecm.core.api.security.ACL;
033import org.nuxeo.ecm.core.api.security.ACP;
034import org.nuxeo.ecm.permissions.PermissionHelper;
035
036/**
037 * Operation that sends the notification email for a given {@code ACE}.
038 *
039 * @since 8.1
040 */
041@Operation(id = SendNotificationEmailForPermission.ID, category = Constants.CAT_DOCUMENT, label = "Send the notification email for a permission", description = "Send the notification email for a permission on the input document(s). Returns the document(s).")
042public class SendNotificationEmailForPermission {
043
044    public static final String ID = "Document.SendNotificationEmailForPermission";
045
046    @Context
047    protected CoreSession session;
048
049    @Param(name = "id", description = "ACE id.")
050    String id;
051
052    @OperationMethod(collector = DocumentModelCollector.class)
053    public DocumentModel run(DocumentModel doc) {
054        sendEmail(doc);
055        return session.getDocument(doc.getRef());
056    }
057
058    @OperationMethod(collector = DocumentModelCollector.class)
059    public DocumentModel run(DocumentRef docRef) {
060        DocumentModel doc = session.getDocument(docRef);
061        sendEmail(doc);
062        return doc;
063    }
064
065    protected void sendEmail(DocumentModel doc) {
066        ACE ace = ACE.fromId(id);
067
068        // check that the ACE exists
069        String aclName = null;
070        ACP acp = doc.getACP();
071        for (ACL acl : acp.getACLs()) {
072            if (acl.contains(ace)) {
073                aclName = acl.getName();
074                break;
075            }
076        }
077
078        if (aclName == null) {
079            // ACE was not found
080            return;
081        }
082
083        PermissionHelper.firePermissionNotificationEvent(session, doc, aclName, ace);
084    }
085}