001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Arnaud Kervern
018 */
019
020package org.nuxeo.ecm.admin.operation;
021
022import java.io.Serializable;
023import java.util.Arrays;
024import java.util.Collections;
025import java.util.List;
026import java.util.stream.Collectors;
027
028import org.nuxeo.ecm.admin.permissions.PermissionsPurgeWork;
029import org.nuxeo.ecm.automation.core.Constants;
030import org.nuxeo.ecm.automation.core.annotations.Context;
031import org.nuxeo.ecm.automation.core.annotations.Operation;
032import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
033import org.nuxeo.ecm.automation.core.annotations.Param;
034import org.nuxeo.ecm.automation.core.collectors.DocumentModelListCollector;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037
038/**
039 * Schedule a {@link PermissionsPurgeWork} to archive ACEs based on permissions_purge page provider from the input
040 * document.
041 *
042 * @since 9.1
043 */
044@Operation(id = PermissionsPurge.ID, category = Constants.CAT_SERVICES, label = "Archiving ACEs", description = "Schedule a work to archive ACEs based on permissions_purge page provider from the input document.")
045public class PermissionsPurge {
046    static final String ID = "PermissionsPurge";
047
048    @Param(name = "usernames", description = "Coma separate list of username")
049    protected String usernames = "";
050
051    @Context
052    protected CoreSession session;
053
054    @OperationMethod
055    public void purgeDocs(List<DocumentModel> docs) {
056        List<String> ancestorIds = docs.stream().map(DocumentModel::getId).collect(Collectors.toList());
057        List<String> usernames = Arrays.asList(this.usernames.split(",\\s*"));
058
059        DocumentModel searchDocument = session.createDocumentModel("PermissionsSearch");
060        searchDocument.setPropertyValue("rs:ace_username", (Serializable) usernames);
061        searchDocument.setPropertyValue("rs:ecm_ancestorIds", (Serializable) ancestorIds);
062
063        new PermissionsPurgeWork(searchDocument).launch();
064    }
065
066    @OperationMethod
067    public void purgeDoc(DocumentModel doc) {
068        purgeDocs(Collections.singletonList(doc));
069    }
070
071    @OperationMethod
072    public void purge() {
073        purgeDocs(Collections.emptyList());
074    }
075}