001/*
002 * (C) Copyright 2006-2010 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 *     Julien Anguenot
018 *     Florent Guillaume
019 */
020
021package org.nuxeo.ecm.platform.publisher.rules;
022
023import java.util.Arrays;
024import java.util.HashSet;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentRef;
028import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
029import org.nuxeo.ecm.core.api.security.ACP;
030import org.nuxeo.ecm.core.api.security.SecurityConstants;
031
032/**
033 * Default NXP validator.
034 * <p>
035 * Validators here will be principals having manage everything rights in the sections where the document has been
036 * published.
037 */
038public class DefaultValidatorsRule implements ValidatorsRule {
039
040    private static final long serialVersionUID = 1L;
041
042    @Override
043    public String[] computesValidatorsFor(DocumentModel doc) {
044        UnrestrictedACPGetter acpg = new UnrestrictedACPGetter(doc);
045        acpg.runUnrestricted();
046        String[] writePermissions = doc.getCoreSession().getPermissionsToCheck(SecurityConstants.WRITE);
047        String[] reviewers = acpg.acp.listUsernamesForAnyPermission(new HashSet<>(Arrays.asList(writePermissions)));
048        return reviewers;
049    }
050
051    protected static class UnrestrictedACPGetter extends UnrestrictedSessionRunner {
052
053        public final DocumentRef docRef;
054
055        public ACP acp;
056
057        public UnrestrictedACPGetter(DocumentModel doc) {
058            super(doc.getCoreSession());
059            this.docRef = doc.getRef();
060        }
061
062        @Override
063        public void run() {
064            acp = session.getACP(docRef);
065        }
066    }
067
068}