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.platform.usermanager.io; 019 020import static org.nuxeo.ecm.core.io.marshallers.json.document.DocumentPropertiesJsonReader.DEFAULT_SCHEMA_NAME; 021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 023import static org.nuxeo.ecm.platform.usermanager.io.NuxeoPrincipalJsonWriter.ENTITY_TYPE; 024 025import java.io.Closeable; 026import java.io.IOException; 027import java.lang.reflect.ParameterizedType; 028import java.util.List; 029 030import javax.inject.Inject; 031 032import org.apache.commons.lang3.reflect.TypeUtils; 033import org.codehaus.jackson.JsonNode; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.ecm.core.api.model.Property; 037import org.nuxeo.ecm.core.io.marshallers.json.EntityJsonReader; 038import org.nuxeo.ecm.core.io.marshallers.json.document.DocumentPropertiesJsonReader; 039import org.nuxeo.ecm.core.io.registry.reflect.Setup; 040import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl; 041import org.nuxeo.ecm.platform.usermanager.UserManager; 042 043/** 044 * Convert Json as {@link NuxeoPrincipal}. 045 * <p> 046 * Format is (any additional json property is ignored): 047 * 048 * <pre> 049 * { 050 * "entity-type":"user", 051 * "id":"USERNAME", 052 * "properties":{ <- depending on the user schema / format is managed by {@link DocumentPropertiesJsonReader} 053 * "firstName":"FIRSTNAME", 054 * "lastName":"LASTNAME", 055 * "username":"USERNAME", 056 * "email":"user@mail.com", 057 * "company":"COMPANY", 058 * "password":"", <- ALWAYS EMPTY 059 * "groups":[ 060 * "GROUP1 NAME OF THE USER", 061 * "GROUP2 NAME OF THE USER", 062 * ... 063 * ] 064 * } 065 * } 066 * </pre> 067 * 068 * </p> 069 * 070 * @since 7.2 071 */ 072@Setup(mode = SINGLETON, priority = REFERENCE) 073public class NuxeoPrincipalJsonReader extends EntityJsonReader<NuxeoPrincipal> { 074 075 @Inject 076 private UserManager userManager; 077 078 public NuxeoPrincipalJsonReader() { 079 super(ENTITY_TYPE); 080 } 081 082 @Override 083 protected NuxeoPrincipal readEntity(JsonNode jn) throws IOException { 084 String id = getStringField(jn, "id"); 085 DocumentModel userDoc = null; 086 if (id != null) { 087 NuxeoPrincipal principal = userManager.getPrincipal(id); 088 if (principal != null) { 089 userDoc = principal.getModel(); 090 } 091 } 092 if (userDoc == null) { 093 userDoc = userManager.getBareUserModel(); 094 } 095 JsonNode propsNode = jn.get("properties"); 096 if (propsNode != null && !propsNode.isNull() && propsNode.isObject()) { 097 ParameterizedType genericType = TypeUtils.parameterize(List.class, Property.class); 098 String schema = userManager.getUserSchemaName(); 099 try (Closeable resource = ctx.wrap().with(DEFAULT_SCHEMA_NAME, schema).open()) { 100 List<Property> properties = readEntity(List.class, genericType, propsNode); 101 for (Property property : properties) { 102 userDoc.setPropertyValue(property.getName(), property.getValue()); 103 } 104 } 105 } 106 NuxeoPrincipal principal = new NuxeoPrincipalImpl(id); 107 principal.setModel(userDoc); 108 return principal; 109 } 110 111}