001/*
002 * (C) Copyright 2015-2019 Nuxeo (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 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.core.io.marshallers.json.document;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024
025import java.io.IOException;
026
027import org.nuxeo.ecm.core.api.security.ACE;
028import org.nuxeo.ecm.core.api.security.ACL;
029import org.nuxeo.ecm.core.api.security.ACP;
030import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
031import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
032import org.nuxeo.ecm.core.io.registry.reflect.Setup;
033import org.nuxeo.ecm.core.schema.utils.DateParser;
034
035import com.fasterxml.jackson.core.JsonGenerator;
036
037/**
038 * Convert {@link ACP} to Json.
039 * <p>
040 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing {@link ACP}.
041 * <p>
042 * This marshaller is also extensible: extend it and simply override
043 * {@link ExtensibleEntityJsonWriter#extend(Object, JsonGenerator)}.
044 * <p>
045 * Format is:
046 *
047 * <pre>
048 * {@code
049 * {
050 *   "entity-type":"acls",
051 *   "acl": [
052 *     {
053 *       "name":"inherited",
054 *       "ace":[
055 *         {
056 *           "username":"administrators",
057 *           "permission":"Everything",
058 *           "granted":true
059 *         },
060 *         ...
061 *       ]
062 *     },
063 *     ...
064 *   ]
065 *             <-- contextParameters if there are enrichers activated
066 *             <-- additional property provided by extend() method
067 * }
068 * }
069 * </pre>
070 *
071 * @since 7.2
072 */
073@Setup(mode = SINGLETON, priority = REFERENCE)
074public class ACPJsonWriter extends ExtensibleEntityJsonWriter<ACP> {
075
076    public static final String ENTITY_TYPE = "acls";
077
078    public ACPJsonWriter() {
079        super(ENTITY_TYPE, ACP.class);
080    }
081
082    @Override
083    protected void writeEntityBody(ACP acp, JsonGenerator jg) throws IOException {
084        jg.writeArrayFieldStart("acl");
085        for (ACL acl : acp.getACLs()) {
086            jg.writeStartObject();
087            jg.writeStringField("name", acl.getName());
088            jg.writeArrayFieldStart("ace");
089            for (ACE ace : acl.getACEs()) {
090                jg.writeStartObject();
091                jg.writeStringField("id", ace.getId());
092                jg.writeStringField("username", ace.getUsername());
093                jg.writeStringField("permission", ace.getPermission());
094                jg.writeBooleanField("granted", ace.isGranted());
095                jg.writeStringField("creator", ace.getCreator());
096                jg.writeStringField("begin",
097                        ace.getBegin() != null ? DateParser.formatW3CDateTime(ace.getBegin().getTime()) : null);
098                jg.writeStringField("end", ace.getEnd() != null ? DateParser.formatW3CDateTime(ace.getEnd().getTime())
099                        : null);
100                jg.writeStringField("status", ace.getStatus().toString().toLowerCase());
101                jg.writeEndObject();
102            }
103
104            jg.writeEndArray();
105            jg.writeEndObject();
106        }
107        jg.writeEndArray();
108    }
109
110}