001/* 002 * (C) Copyright 2016 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 * "Guillaume Renard" 018 */ 019 020package org.nuxeo.elasticsearch.audit.pageprovider; 021 022import java.util.ArrayList; 023import java.util.List; 024import org.apache.commons.lang3.StringUtils; 025import org.nuxeo.ecm.core.api.CoreSession; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.directory.Session; 028import org.nuxeo.ecm.directory.api.DirectoryService; 029import org.nuxeo.ecm.platform.audit.api.LogEntry; 030import org.nuxeo.ecm.platform.query.api.AbstractPageProvider; 031import org.nuxeo.ecm.platform.query.api.PageProvider; 032import org.nuxeo.ecm.platform.query.api.PageProviderService; 033import org.nuxeo.ecm.platform.usermanager.UserManager; 034import org.nuxeo.ecm.platform.usermanager.UserManagerImpl; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * @since 8.2 039 */ 040public class LatestCreatedUsersOrGroupsPageProvider extends AbstractPageProvider<DocumentModel> { 041 042 private static final long serialVersionUID = 1L; 043 044 public final static String LATEST_CREATED_USERS_OR_GROUPS_PROVIDER = "LATEST_CREATED_USERS_OR_GROUPS_PROVIDER"; 045 046 public final static String LATEST_AUDITED_CREATED_USERS_OR_GROUPS_PROVIDER = "LATEST_AUDITED_CREATED_USERS_OR_GROUPS_PROVIDER"; 047 048 protected List<DocumentModel> currentPage; 049 050 @Override 051 public List<DocumentModel> getCurrentPage() { 052 if (currentPage == null) { 053 currentPage = new ArrayList<DocumentModel>(); 054 PageProviderService pps = Framework.getService(PageProviderService.class); 055 CoreSession coreSession = (CoreSession) getProperties().get(ESAuditPageProvider.CORE_SESSION_PROPERTY); 056 PageProvider<?> pp = pps.getPageProvider(LATEST_AUDITED_CREATED_USERS_OR_GROUPS_PROVIDER, null, 057 getPageSize(), getCurrentPageIndex(), getProperties(), 058 coreSession != null ? coreSession.getRootDocument().getId() : new Object[] { null }); 059 @SuppressWarnings("unchecked") 060 List<LogEntry> entries = (List<LogEntry>) pp.getCurrentPage(); 061 if (entries != null) { 062 UserManager um = Framework.getService(UserManager.class); 063 DirectoryService directoryService = Framework.getService(DirectoryService.class); 064 String schema = directoryService.getDirectorySchema(um.getUserDirectoryName()); 065 try (Session userDir = directoryService.open(um.getUserDirectoryName(), null)) { 066 for (LogEntry e : entries) { 067 String id = (String) e.getExtendedInfos().get("id").getSerializableValue(); 068 if (StringUtils.isNotBlank(id)) { 069 DocumentModel doc; 070 if (UserManagerImpl.GROUPCREATED_EVENT_ID.equals(e.getEventId())) { 071 doc = um.getGroupModel(id); 072 } else if (UserManagerImpl.USERCREATED_EVENT_ID.equals(e.getEventId())) { 073 doc = um.getUserModel(id); 074 if (doc == null) { 075 break; 076 } 077 doc.setProperty(schema, userDir.getPasswordField(), null); 078 } else { 079 break; 080 } 081 if (doc == null) { 082 // probably user/group does not exist anymore 083 break; 084 } 085 currentPage.add(doc); 086 } 087 } 088 } 089 } 090 } 091 092 return currentPage; 093 } 094 095 @Override 096 protected void pageChanged() { 097 currentPage = null; 098 super.pageChanged(); 099 } 100 101}