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