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.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036
037/**
038 * Schedule a {@link PermissionsPurgeWork} to archive ACEs based on permissions_purge page provider from the input
039 * document.
040 *
041 * @since 9.1
042 */
043@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.")
044public class PermissionsPurge {
045    static final String ID = "PermissionsPurge";
046
047    @Param(name = "usernames", description = "Coma separate list of username")
048    protected String usernames = "";
049
050    @Context
051    protected CoreSession session;
052
053    @OperationMethod
054    public void purgeDocs(List<DocumentModel> docs) {
055        List<String> ancestorIds = docs.stream().map(DocumentModel::getId).collect(Collectors.toList());
056        List<String> usernames = Arrays.asList(this.usernames.split(",\\s*"));
057
058        DocumentModel searchDocument = session.createDocumentModel("PermissionsSearch");
059        searchDocument.setPropertyValue("rs:ace_username", (Serializable) usernames);
060        searchDocument.setPropertyValue("rs:ecm_ancestorIds", (Serializable) ancestorIds);
061
062        new PermissionsPurgeWork(searchDocument).launch();
063    }
064
065    @OperationMethod
066    public void purgeDoc(DocumentModel doc) {
067        purgeDocs(Collections.singletonList(doc));
068    }
069
070    @OperationMethod
071    public void purge() {
072        purgeDocs(Collections.emptyList());
073    }
074}