001/*
002 * (C) Copyright 2013 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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.jaxrs.io.usermanager;
018
019import java.io.IOException;
020import java.util.List;
021
022import javax.ws.rs.Produces;
023import javax.ws.rs.core.Context;
024import javax.ws.rs.ext.Provider;
025
026import org.codehaus.jackson.JsonFactory;
027import org.codehaus.jackson.JsonGenerationException;
028import org.codehaus.jackson.JsonGenerator;
029import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
030import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentWriter;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoGroup;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034import org.nuxeo.ecm.core.api.model.DocumentPart;
035import org.nuxeo.ecm.core.api.model.Property;
036import org.nuxeo.ecm.directory.api.DirectoryService;
037import org.nuxeo.ecm.platform.usermanager.UserManager;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Serialization for a Nuxeo principal.
042 *
043 * @since 5.7.3
044 */
045@Provider
046@Produces({ "application/json+nxentity", "application/json" })
047public class NuxeoPrincipalWriter extends EntityWriter<NuxeoPrincipal> {
048
049    /**
050     *
051     */
052    public static final String ENTITY_TYPE = "user";
053
054    @Context
055    JsonFactory factory;
056
057    /**
058     * @param createGenerator
059     * @throws IOException
060     * @throws JsonGenerationException
061     * @since 5.7.3
062     */
063    @Override
064    public void writeEntityBody(JsonGenerator jg, NuxeoPrincipal principal) throws JsonGenerationException,
065            IOException {
066
067        jg.writeStringField("id", principal.getName());
068
069        writeProperties(jg, principal.getModel());
070        writeExtendedGroups(jg, principal.getAllGroups());
071
072        jg.writeBooleanField("isAdministrator", principal.isAdministrator());
073        jg.writeBooleanField("isAnonymous", principal.isAnonymous());
074
075    }
076
077    /**
078     * @param jg
079     * @param model
080     * @throws IOException
081     * @throws JsonGenerationException
082     * @since 5.7.3
083     */
084    static private void writeProperties(JsonGenerator jg, DocumentModel doc) throws JsonGenerationException,
085            IOException {
086        UserManager um = Framework.getLocalService(UserManager.class);
087
088        jg.writeFieldName("properties");
089        jg.writeStartObject();
090
091        DocumentPart part = doc.getPart(um.getUserSchemaName());
092        if (part == null) {
093            return;
094        }
095
096        for (Property p : part.getChildren()) {
097            String fieldName = p.getField().getName().getLocalName();
098            jg.writeFieldName(fieldName);
099
100            if (!fieldName.equals(getPasswordField(um))) {
101                JsonDocumentWriter.writePropertyValue(jg, p, "");
102            } else {
103                jg.writeString("");
104            }
105        }
106        jg.writeEndObject();
107
108    }
109
110    /**
111     * @param um
112     * @return
113     * @since 5.8
114     */
115    private static String getPasswordField(UserManager um) {
116        String userDirectoryName = um.getUserDirectoryName();
117        DirectoryService dirService = Framework.getLocalService(DirectoryService.class);
118        return dirService.getDirectory(userDirectoryName).getPasswordField();
119    }
120
121    /**
122     * This part adds all groupe that the user belongs to directly or indirectly and adds the label in the result.
123     *
124     * @param jg
125     * @param allGroups
126     * @throws IOException
127     * @throws JsonGenerationException
128     * @since 5.7.3
129     */
130    static private void writeExtendedGroups(JsonGenerator jg, List<String> allGroups) throws JsonGenerationException,
131            IOException {
132        UserManager um = Framework.getLocalService(UserManager.class);
133
134        jg.writeArrayFieldStart("extendedGroups");
135        for (String strGroup : allGroups) {
136            NuxeoGroup group = um.getGroup(strGroup);
137            String label = group == null ? strGroup : group.getLabel();
138            jg.writeStartObject();
139            jg.writeStringField("name", strGroup);
140            jg.writeStringField("label", label);
141            jg.writeStringField("url", "group/" + strGroup);
142            jg.writeEndObject();
143        }
144        jg.writeEndArray();
145    }
146
147    @Override
148    protected String getEntityType() {
149        return ENTITY_TYPE;
150    }
151
152}