001/* 002 * (C) Copyright 2017 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 * Kevin Leturc 019 */ 020package org.nuxeo.mongodb.audit.pageprovider; 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.platform.audit.api.LogEntry; 029import org.nuxeo.ecm.platform.query.api.AbstractPageProvider; 030import org.nuxeo.ecm.platform.query.api.PageProvider; 031import org.nuxeo.ecm.platform.query.api.PageProviderService; 032import org.nuxeo.ecm.platform.usermanager.UserManager; 033import org.nuxeo.ecm.platform.usermanager.UserManagerImpl; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * @since 9.1 038 */ 039public class LatestCreatedUsersOrGroupsPageProvider extends AbstractPageProvider<DocumentModel> { 040 041 private static final long serialVersionUID = 1L; 042 043 public final static String LATEST_CREATED_USERS_OR_GROUPS_PROVIDER = "LATEST_CREATED_USERS_OR_GROUPS_PROVIDER"; 044 045 public final static String LATEST_AUDITED_CREATED_USERS_OR_GROUPS_PROVIDER = "LATEST_AUDITED_CREATED_USERS_OR_GROUPS_PROVIDER"; 046 047 protected List<DocumentModel> currentPage; 048 049 @Override 050 public List<DocumentModel> getCurrentPage() { 051 if (currentPage != null) { 052 return currentPage; 053 } 054 currentPage = new ArrayList<>(); 055 PageProviderService pps = Framework.getService(PageProviderService.class); 056 CoreSession coreSession = (CoreSession) getProperties().get(MongoDBAuditPageProvider.CORE_SESSION_PROPERTY); 057 PageProvider<?> pp = pps.getPageProvider(LATEST_AUDITED_CREATED_USERS_OR_GROUPS_PROVIDER, null, getPageSize(), 058 getCurrentPageIndex(), getProperties(), 059 coreSession != null ? coreSession.getRootDocument().getId() : new Object[] { null }); 060 @SuppressWarnings("unchecked") 061 List<LogEntry> entries = (List<LogEntry>) pp.getCurrentPage(); 062 if (entries != null) { 063 UserManager um = Framework.getService(UserManager.class); 064 for (LogEntry e : entries) { 065 String id = (String) e.getExtendedInfos().get("id").getSerializableValue(); 066 if (StringUtils.isNotBlank(id)) { 067 DocumentModel doc; 068 if (UserManagerImpl.GROUPCREATED_EVENT_ID.equals(e.getEventId())) { 069 doc = um.getGroupModel(id); 070 } else if (UserManagerImpl.USERCREATED_EVENT_ID.equals(e.getEventId())) { 071 doc = um.getUserModel(id); 072 } else { 073 break; 074 } 075 if (doc == null) { 076 // probably user/group does not exist anymore 077 break; 078 } 079 currentPage.add(doc); 080 } 081 } 082 } 083 return currentPage; 084 } 085 086 @Override 087 protected void pageChanged() { 088 currentPage = null; 089 super.pageChanged(); 090 } 091 092}