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