001/*
002 * (C) Copyright 2011 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 * Contributors:
016 * Nuxeo - initial API and implementation
017 */
018
019package org.nuxeo.ecm.user.registration;
020
021import static org.nuxeo.ecm.user.registration.DocumentRegistrationInfo.ACL_NAME;
022
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.api.security.ACE;
030import org.nuxeo.ecm.user.invite.DefaultInvitationUserFactory;
031import org.nuxeo.ecm.user.invite.UserRegistrationConfiguration;
032import org.nuxeo.ecm.user.invite.UserRegistrationException;
033
034import java.util.Calendar;
035
036public class DefaultRegistrationUserFactory extends DefaultInvitationUserFactory implements RegistrationUserFactory {
037
038    private static final Log log = LogFactory.getLog(DefaultRegistrationUserFactory.class);
039
040    @Override
041    public DocumentModel doAddDocumentPermission(CoreSession session, DocumentModel registrationDoc,
042            UserRegistrationConfiguration configuration) {
043        String docId = (String) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_ID_FIELD);
044
045        if (StringUtils.isEmpty(docId)) {
046            log.info("No document rights needed");
047            return null;
048        }
049        String login = (String) registrationDoc.getPropertyValue(configuration.getUserInfoUsernameField());
050        String permission = (String) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_RIGHT_FIELD);
051        Calendar beginCal = (Calendar) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_BEGIN_FIELD);
052        Calendar endCal = (Calendar) registrationDoc.getPropertyValue(DocumentRegistrationInfo.DOCUMENT_END_FIELD);
053        if (StringUtils.isEmpty(permission)) {
054            throw new UserRegistrationException("Permission must be specified");
055        }
056
057        DocumentModel document = session.getDocument(new IdRef(docId));
058        if (!document.getACP().getAccess(login, permission).toBoolean()) {
059            ACE ace = ACE.builder(login, permission).isGranted(true).begin(beginCal).end(endCal).build();
060            // Always append ACL to the first place to be after the block
061            // rights inheritance ACE.
062            document.getACP().getOrCreateACL(ACL_NAME).add(0, ace);
063
064            session.setACP(document.getRef(), document.getACP(), true);
065        } else {
066            log.info(String.format("User %s already have %s on doc %s", login, permission, docId));
067        }
068
069        return document;
070    }
071
072    @Override
073    public void doPostAddDocumentPermission(CoreSession session, DocumentModel registrationDoc, DocumentModel document)
074            {
075        // Nothing to do in the default implementation
076    }
077}