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 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.core.io.marshallers.json.enrichers;
019
020import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
021import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
022
023import java.io.IOException;
024
025import org.codehaus.jackson.JsonGenerator;
026import org.joda.time.DateTime;
027import org.joda.time.format.DateTimeFormatter;
028import org.joda.time.format.ISODateTimeFormat;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.security.ACE;
031import org.nuxeo.ecm.core.api.security.ACL;
032import org.nuxeo.ecm.core.api.security.ACP;
033import org.nuxeo.ecm.core.io.registry.reflect.Setup;
034
035/**
036 * Enrich {@link DocumentModel} Json.
037 * <p>
038 * Add {@link DocumentModel}'s ACP as json attachment.
039 * </p>
040 * <p>
041 * Enable if parameter enrichers.document=acls is present.
042 * </p>
043 * <p>
044 * Format is:
045 *
046 * <pre>
047 * {@code
048 * {
049 *   "entity-type":"document",
050 *   ...
051 *   "contextParameters": {
052 *     "acls": [
053 *       {
054 *         "name":"inherited",
055 *         "ace":[
056 *           {
057 *             "username":"administrators",
058 *             "permission":"Everything",
059 *             "granted":true
060 *           },
061 *           ...
062 *         ]
063 *       },
064 *       ...
065 *     ]
066 *   }
067 * }
068 * </pre>
069 *
070 * </p>
071 *
072 * @since 7.2
073 */
074@Setup(mode = SINGLETON, priority = REFERENCE)
075public class ACLJsonEnricher extends AbstractJsonEnricher<DocumentModel> {
076
077    public static final String NAME = "acls";
078
079    public ACLJsonEnricher() {
080        super(NAME);
081    }
082
083    @Override
084    public void write(JsonGenerator jg, DocumentModel document) throws IOException {
085        ACP item = document.getACP();
086        jg.writeArrayFieldStart(NAME);
087        for (ACL acl : item.getACLs()) {
088            jg.writeStartObject();
089            jg.writeStringField("name", acl.getName());
090            jg.writeArrayFieldStart("ace");
091            for (ACE ace : acl.getACEs()) {
092                jg.writeStartObject();
093                jg.writeStringField("id", ace.getId());
094                jg.writeStringField("username", ace.getUsername());
095                jg.writeStringField("permission", ace.getPermission());
096                jg.writeBooleanField("granted", ace.isGranted());
097                jg.writeStringField("creator", ace.getCreator());
098                DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
099                jg.writeStringField("begin",
100                        ace.getBegin() != null ? dateTimeFormatter.print(new DateTime(ace.getBegin())) : null);
101                jg.writeStringField("end", ace.getEnd() != null ? dateTimeFormatter.print(new DateTime(ace.getEnd()))
102                        : null);
103                jg.writeStringField("status", ace.getStatus().toString());
104                jg.writeEndObject();
105            }
106            jg.writeEndArray();
107            jg.writeEndObject();
108        }
109        jg.writeEndArray();
110    }
111
112}