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 org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.core.api.NuxeoPrincipal;
021import org.nuxeo.ecm.core.event.Event;
022import org.nuxeo.ecm.core.event.EventContext;
023import org.nuxeo.ecm.core.event.EventListener;
024import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
025import org.nuxeo.ecm.user.invite.RegistrationRules;
026import org.nuxeo.ecm.user.invite.UserRegistrationConfiguration;
027import org.nuxeo.runtime.api.Framework;
028
029public class UserRegistrationListener implements EventListener {
030
031    @Override
032    public void handleEvent(Event event) {
033        UserRegistrationService userRegistrationService = Framework.getService(UserRegistrationService.class);
034        if (!event.getName().equals(userRegistrationService.getNameEventRegistrationValidated())) {
035            return;
036        }
037        EventContext ctx = event.getContext();
038        if (ctx instanceof DocumentEventContext) {
039            DocumentEventContext docCtx = (DocumentEventContext) ctx;
040            DocumentModel registration = docCtx.getSourceDocument();
041            UserRegistrationConfiguration config = userRegistrationService.getConfiguration(registration);
042            RegistrationRules rules = userRegistrationService.getRegistrationRules(config.getName());
043            if (rules.allowUserCreation()) {
044                NuxeoPrincipal principal = userRegistrationService.createUser(ctx.getCoreSession(), registration);
045                docCtx.setProperty("registeredUser", principal);
046            }
047            if (rules.allowUserCreation() || rules.isForcingRight()) {
048                userRegistrationService.addRightsOnDoc(ctx.getCoreSession(), registration);
049            }
050        }
051    }
052
053}