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