001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.user.registration;
018
019import static org.nuxeo.ecm.user.registration.DocumentRegistrationInfo.ACL_NAME;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.IdRef;
027import org.nuxeo.ecm.core.api.security.ACE;
028import org.nuxeo.ecm.user.invite.DefaultInvitationUserFactory;
029import org.nuxeo.ecm.user.invite.UserRegistrationConfiguration;
030import org.nuxeo.ecm.user.invite.UserRegistrationException;
031
032import java.util.Calendar;
033
034public class DefaultRegistrationUserFactory extends DefaultInvitationUserFactory implements RegistrationUserFactory {
035
036    private static final Log log = LogFactory.getLog(DefaultRegistrationUserFactory.class);
037
038    @Override
039    public DocumentModel doAddDocumentPermission(CoreSession session, DocumentModel registrationDoc,
040            UserRegistrationConfiguration configuration) {
041        String docId = (String) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_ID_FIELD);
042
043        if (StringUtils.isEmpty(docId)) {
044            log.info("No document rights needed");
045            return null;
046        }
047        String login = (String) registrationDoc.getPropertyValue(configuration.getUserInfoUsernameField());
048        String permission = (String) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_RIGHT_FIELD);
049        Calendar beginCal = (Calendar) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_BEGIN_FIELD);
050        Calendar endCal = (Calendar) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_END_FIELD);
051        if (StringUtils.isEmpty(permission)) {
052            throw new UserRegistrationException("Permission must be specified");
053        }
054
055        DocumentModel document = session.getDocument(new IdRef(docId));
056        if (!document.getACP().getAccess(login, permission).toBoolean()) {
057            ACE ace = ACE.builder(login, permission).isGranted(true).begin(beginCal).end(endCal).build();
058            // Always append ACL to the first place to be after the block
059            // rights inheritance ACE.
060            document.getACP().getOrCreateACL(ACL_NAME).add(0, ace);
061
062            session.setACP(document.getRef(), document.getACP(), true);
063        } else {
064            log.info(String.format("User %s already have %s on doc %s", login, permission, docId));
065        }
066
067        return document;
068    }
069
070    @Override
071    public void doPostAddDocumentPermission(CoreSession session, DocumentModel registrationDoc, DocumentModel document)
072            {
073        // Nothing to do in the default implementation
074    }
075}