001/*
002 * (C) Copyright 2016-2017 Nuxeo (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 *     Michael Vachette
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.automation.core.operations.users;
021
022import static org.nuxeo.ecm.platform.usermanager.UserConfig.COMPANY_COLUMN;
023import static org.nuxeo.ecm.platform.usermanager.UserConfig.EMAIL_COLUMN;
024import static org.nuxeo.ecm.platform.usermanager.UserConfig.FIRSTNAME_COLUMN;
025import static org.nuxeo.ecm.platform.usermanager.UserConfig.GROUPS_COLUMN;
026import static org.nuxeo.ecm.platform.usermanager.UserConfig.LASTNAME_COLUMN;
027import static org.nuxeo.ecm.platform.usermanager.UserConfig.PASSWORD_COLUMN;
028import static org.nuxeo.ecm.platform.usermanager.UserConfig.SCHEMA_NAME;
029import static org.nuxeo.ecm.platform.usermanager.UserConfig.TENANT_ID_COLUMN;
030import static org.nuxeo.ecm.platform.usermanager.UserConfig.USERNAME_COLUMN;
031
032import java.util.AbstractMap.SimpleEntry;
033import java.util.Arrays;
034import java.util.Map.Entry;
035
036import org.apache.commons.lang.StringUtils;
037import org.nuxeo.ecm.automation.OperationException;
038import org.nuxeo.ecm.automation.core.Constants;
039import org.nuxeo.ecm.automation.core.annotations.Context;
040import org.nuxeo.ecm.automation.core.annotations.Operation;
041import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
042import org.nuxeo.ecm.automation.core.annotations.Param;
043import org.nuxeo.ecm.automation.core.util.Properties;
044import org.nuxeo.ecm.automation.core.util.StringList;
045import org.nuxeo.ecm.core.api.DocumentModel;
046import org.nuxeo.ecm.platform.usermanager.UserManager;
047
048/**
049 * Operation to create or update a user.
050 *
051 * @since 9.1
052 */
053@Operation(id = CreateOrUpdateUser.ID, //
054        aliases = { "Services.CreateUser" }, //
055        category = Constants.CAT_USERS_GROUPS, //
056        label = "Create or Update User", //
057        description = "Create or Update User.")
058public class CreateOrUpdateUser {
059
060    public static final String ID = "User.CreateOrUpdate";
061
062    public static final String CREATE_OR_UPDATE = "createOrUpdate";
063
064    public static final String CREATE = "create";
065
066    public static final String UPDATE = "update";
067
068    protected static final String USER_COLON = SCHEMA_NAME + ':';
069
070    @Context
071    protected UserManager userManager;
072
073    @Param(name = "username")
074    protected String username;
075
076    @Param(name = "password", required = false)
077    protected String password;
078
079    @Param(name = "email", required = false)
080    protected String email;
081
082    @Param(name = "firstName", required = false)
083    protected String firstName;
084
085    @Param(name = "lastName", required = false)
086    protected String lastName;
087
088    @Param(name = "company", required = false)
089    protected String company;
090
091    @Param(name = "tenantId", required = false)
092    protected String tenantId;
093
094    @Param(name = "groups", required = false)
095    protected StringList groups;
096
097    @Param(name = "properties", required = false)
098    protected Properties properties = new Properties();
099
100    @Param(name = "mode", required = false, values = { CREATE_OR_UPDATE, CREATE, UPDATE })
101    protected String mode;
102
103    @OperationMethod
104    public void run() throws OperationException {
105        boolean create;
106        DocumentModel userDoc = userManager.getUserModel(username);
107        if (userDoc == null) {
108            if (UPDATE.equals(mode)) {
109                throw new OperationException("Cannot update non-existent user: " + username);
110            }
111            create = true;
112            userDoc = userManager.getBareUserModel();
113            userDoc.setProperty(SCHEMA_NAME, USERNAME_COLUMN, username);
114        } else {
115            if (CREATE.equals(mode)) {
116                throw new OperationException("Cannot create already-existing user: " + username);
117            }
118            create = false;
119        }
120        if (groups != null) {
121            userDoc.setProperty(SCHEMA_NAME, GROUPS_COLUMN, groups);
122        }
123        for (Entry<String, String> entry : Arrays.asList( //
124                new SimpleEntry<>(TENANT_ID_COLUMN, tenantId), //
125                new SimpleEntry<>(PASSWORD_COLUMN, password), //
126                new SimpleEntry<>(EMAIL_COLUMN, email), //
127                new SimpleEntry<>(FIRSTNAME_COLUMN, firstName), //
128                new SimpleEntry<>(LASTNAME_COLUMN, lastName), //
129                new SimpleEntry<>(COMPANY_COLUMN, company))) {
130            String key = entry.getKey();
131            String value = entry.getValue();
132            if (StringUtils.isNotBlank(value)) {
133                properties.put(key, value);
134            }
135        }
136        for (Entry<String, String> entry : properties.entrySet()) {
137            String key = entry.getKey();
138            String value = entry.getValue();
139            if (key.startsWith(USER_COLON)) {
140                key = key.substring(USER_COLON.length());
141            }
142            userDoc.setProperty(SCHEMA_NAME, key, value);
143        }
144        if (create) {
145            userDoc = userManager.createUser(userDoc);
146        } else {
147            userManager.updateUser(userDoc);
148            userDoc = userManager.getUserModel(username);
149        }
150    }
151
152}