001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.multi.tenant.operations;
019
020import org.nuxeo.ecm.automation.core.Constants;
021import org.nuxeo.ecm.automation.core.annotations.Context;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.automation.core.annotations.Param;
025import org.nuxeo.ecm.automation.core.util.StringList;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.platform.usermanager.UserManager;
028
029/**
030 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
031 * @since 5.6
032 */
033@Operation(id = CreateUser.ID, category = Constants.CAT_SERVICES, label = "Creates a new user", description = "Creates a new user.")
034public class CreateUser {
035
036    public static final String ID = "Services.CreateUser";
037
038    @Context
039    protected UserManager userManager;
040
041    @Param(name = "username", required = true)
042    protected String username;
043
044    @Param(name = "password", required = true)
045    protected String password;
046
047    @Param(name = "email", required = true)
048    protected String email;
049
050    @Param(name = "firstName", required = false)
051    protected String firstName;
052
053    @Param(name = "lastName", required = false)
054    protected String lastName;
055
056    @Param(name = "company", required = false)
057    protected String company;
058
059    @Param(name = "tenantId", required = false)
060    protected String tenantId = "";
061
062    @Param(name = "groups", required = false)
063    protected StringList groups;
064
065    @OperationMethod
066    public void run() {
067        if (userManager.getPrincipal(username) != null) {
068            return;
069        }
070
071        DocumentModel newUser = userManager.getBareUserModel();
072        newUser.setProperty("user", "username", username);
073        newUser.setProperty("user", "password", password);
074        newUser.setProperty("user", "email", email);
075        newUser.setProperty("user", "firstName", firstName);
076        newUser.setProperty("user", "lastName", lastName);
077        newUser.setProperty("user", "company", company);
078        newUser.setProperty("user", "tenantId", tenantId);
079        newUser.setProperty("user", "groups", groups);
080
081        userManager.createUser(newUser);
082    }
083}