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;
021
022import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_ACE_ID;
023import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_ACL_NAME;
024import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_COMMENT;
025import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_DOC_ID;
026import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_ID;
027import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_NOTIFY;
028import static org.nuxeo.ecm.permissions.Constants.ACE_INFO_REPOSITORY_NAME;
029import static org.nuxeo.ecm.permissions.Constants.ACE_KEY;
030import static org.nuxeo.ecm.permissions.Constants.ACL_NAME_KEY;
031import static org.nuxeo.ecm.permissions.Constants.PERMISSION_NOTIFICATION_EVENT;
032
033import java.util.HashMap;
034import java.util.Map;
035
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.security.ACE;
039import org.nuxeo.ecm.core.event.EventService;
040import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * @since 7.4
045 */
046public class PermissionHelper {
047
048    private PermissionHelper() {
049        // helper class
050    }
051
052    public static String computeDirectoryId(DocumentModel doc, String aclName, String aceId) {
053        return String.format("%s:%s:%s:%s", doc.getId(), doc.getRepositoryName(), aclName, aceId);
054    }
055
056    public static Map<String, Object> createDirectoryEntry(DocumentModel doc, String aclName, ACE ace, boolean notify,
057            String comment) {
058        Map<String, Object> m = new HashMap<>();
059        m.put(ACE_INFO_ID, computeDirectoryId(doc, aclName, ace.getId()));
060        m.put(ACE_INFO_REPOSITORY_NAME, doc.getRepositoryName());
061        m.put(ACE_INFO_DOC_ID, doc.getId());
062        m.put(ACE_INFO_ACL_NAME, aclName);
063        m.put(ACE_INFO_ACE_ID, ace.getId());
064        m.put(ACE_INFO_NOTIFY, notify);
065        m.put(ACE_INFO_COMMENT, comment);
066        return m;
067    }
068
069    public static void firePermissionNotificationEvent(CoreSession session, DocumentModel doc, String aclName, ACE ace) {
070        DocumentEventContext docCtx = new DocumentEventContext(session, session.getPrincipal(), doc);
071        docCtx.setProperty(ACE_KEY, ace);
072        docCtx.setProperty(ACL_NAME_KEY, aclName);
073        EventService eventService = Framework.getService(EventService.class);
074        eventService.fireEvent(PERMISSION_NOTIFICATION_EVENT, docCtx);
075    }
076}