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