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