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