001/* 002 * (C) Copyright 2006-2012 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 * Thomas Roger <troger@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.multi.tenant.operations; 021 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030 031import net.sf.json.JSONArray; 032import net.sf.json.JSONObject; 033 034import org.apache.commons.lang.StringUtils; 035import org.nuxeo.ecm.automation.core.Constants; 036import org.nuxeo.ecm.automation.core.annotations.Context; 037import org.nuxeo.ecm.automation.core.annotations.Operation; 038import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 039import org.nuxeo.ecm.automation.core.annotations.Param; 040import org.nuxeo.ecm.core.api.Blob; 041import org.nuxeo.ecm.core.api.Blobs; 042import org.nuxeo.ecm.core.api.DocumentModel; 043import org.nuxeo.ecm.platform.usermanager.UserManager; 044 045/** 046 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 047 * @since 5.6 048 */ 049@Operation(id = QueryUsers.ID, category = Constants.CAT_SERVICES, label = "Query users", description = "Query users filtered by a tenantId if provided.") 050public class QueryUsers { 051 052 public static final String ID = "Services.QueryUsers"; 053 054 public static final Set<String> FULLTEXT_FIELDS = new HashSet<String>(Arrays.asList("username", "firstName", 055 "lastName")); 056 057 @Context 058 protected UserManager userManager; 059 060 @Param(name = "pattern", required = false) 061 protected String pattern; 062 063 @Param(name = "tenantId", required = false) 064 protected String tenantId; 065 066 @OperationMethod 067 public Blob run() { 068 List<DocumentModel> users = new ArrayList<DocumentModel>(); 069 if (StringUtils.isBlank(pattern)) { 070 Map<String, Serializable> filter = new HashMap<String, Serializable>(); 071 if (!StringUtils.isBlank(tenantId)) { 072 filter.put("tenantId", tenantId); 073 } 074 075 users.addAll(userManager.searchUsers(filter, filter.keySet())); 076 } else { 077 for (String field : FULLTEXT_FIELDS) { 078 Map<String, Serializable> filter = new HashMap<String, Serializable>(); 079 filter.put(field, pattern); 080 081 if (!StringUtils.isBlank(tenantId)) { 082 filter.put("tenantId", tenantId); 083 } 084 085 users.addAll(userManager.searchUsers(filter, filter.keySet())); 086 } 087 } 088 089 return buildResponse(users); 090 } 091 092 protected Blob buildResponse(List<DocumentModel> users) { 093 094 JSONArray array = new JSONArray(); 095 for (DocumentModel user : users) { 096 JSONObject o = new JSONObject(); 097 o.element("username", user.getProperty("user", "username")); 098 o.element("firstName", user.getProperty("user", "firstName")); 099 o.element("lastName", user.getProperty("user", "lastName")); 100 o.element("email", user.getProperty("user", "email")); 101 o.element("company", user.getProperty("user", "company")); 102 o.element("tenantId", user.getProperty("user", "tenantId")); 103 array.add(o); 104 } 105 106 JSONObject result = new JSONObject(); 107 result.put("users", array); 108 109 return Blobs.createBlob(result.toString(), "application/json"); 110 } 111}