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