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    public String[] computesValidatorsFor(DocumentModel doc) {
043        UnrestrictedACPGetter acpg = new UnrestrictedACPGetter(doc);
044        acpg.runUnrestricted();
045        String[] writePermissions = doc.getCoreSession().getPermissionsToCheck(SecurityConstants.WRITE);
046        String[] reviewers = acpg.acp.listUsernamesForAnyPermission(new HashSet<String>(Arrays.asList(writePermissions)));
047        return reviewers;
048    }
049
050    protected static class UnrestrictedACPGetter extends UnrestrictedSessionRunner {
051
052        public final DocumentRef docRef;
053
054        public ACP acp;
055
056        public UnrestrictedACPGetter(DocumentModel doc) {
057            super(doc.getCoreSession());
058            this.docRef = doc.getRef();
059        }
060
061        @Override
062        public void run() {
063            acp = session.getACP(docRef);
064        }
065    }
066
067}