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