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