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.invite;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.api.NuxeoPrincipal;
026import org.nuxeo.ecm.core.event.Event;
027import org.nuxeo.ecm.core.event.EventContext;
028import org.nuxeo.ecm.core.event.EventListener;
029import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
030import org.nuxeo.runtime.api.Framework;
031
032public class UserInvitationListener implements EventListener {
033
034    protected static Log log = LogFactory.getLog(UserInvitationListener.class);
035
036    public void handleEvent(Event event) {
037        try {
038            UserInvitationService userRegistrationService = Framework.getService(UserInvitationService.class);
039
040            if (!event.getName().equals(userRegistrationService.getNameEventRegistrationValidated())) {
041                return;
042            }
043
044            EventContext ctx = event.getContext();
045            if (ctx instanceof DocumentEventContext) {
046                DocumentEventContext docCtx = (DocumentEventContext) ctx;
047                DocumentModel registration = docCtx.getSourceDocument();
048
049                UserRegistrationConfiguration config = userRegistrationService.getConfiguration(registration);
050                RegistrationRules rules = userRegistrationService.getRegistrationRules(config.getName());
051                if (rules.allowUserCreation()) {
052                    NuxeoPrincipal principal = userRegistrationService.createUser(ctx.getCoreSession(), registration);
053                    docCtx.setProperty("registeredUser", principal);
054                }
055
056            }
057
058        } catch (NuxeoException e) {
059            event.markRollBack();
060            e.addInfo("Unable to complete registration");
061            throw e;
062        }
063    }
064
065}