001/* 002 * (C) Copyright 2016 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 * Nelson Silva <nsilva@nuxeo.com> 018 */ 019package org.nuxeo.ecm.automation.core.operations.services; 020 021import org.nuxeo.ecm.automation.core.Constants; 022import org.nuxeo.ecm.automation.core.annotations.Context; 023import org.nuxeo.ecm.automation.core.annotations.Operation; 024import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 025import org.nuxeo.ecm.automation.core.annotations.Param; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.NuxeoPrincipal; 028import org.nuxeo.ecm.platform.usermanager.UserManager; 029import org.nuxeo.ecm.user.invite.UserInvitationService; 030import org.nuxeo.ecm.user.invite.UserRegistrationConfiguration; 031 032import java.io.Serializable; 033import java.util.HashMap; 034import java.util.Map; 035 036import static org.nuxeo.ecm.user.invite.UserInvitationService.ValidationMethod; 037 038/** 039 * Simple operation to invite a User. 040 * 041 * @since 8.2 042 */ 043@Operation(id = UserInvite.ID, category = Constants.CAT_USERS_GROUPS, label = "Invite a user", 044 description = "Stores a registration request and returns its ID.") 045public class UserInvite { 046 047 public static final String ID = "User.Invite"; 048 049 @Context 050 protected UserManager userManager; 051 052 @Context 053 protected UserInvitationService invitationService; 054 055 @Param(name = "validationMethod", required = false) 056 protected ValidationMethod validationMethod = ValidationMethod.EMAIL; 057 058 @Param(name = "autoAccept", required = false) 059 protected boolean autoAccept = true; 060 061 @Param(name = "info", required = false) 062 protected Map<String, Serializable> info = new HashMap<>(); 063 064 @Param(name = "comment", required = false) 065 protected String comment; 066 067 @OperationMethod 068 public String run(NuxeoPrincipal user) { 069 DocumentModel invitation = invitationService.getUserRegistrationModel(null); 070 071 UserRegistrationConfiguration config = invitationService.getConfiguration(); 072 invitation.setPropertyValue(config.getUserInfoUsernameField(), user.getName()); 073 invitation.setPropertyValue(config.getUserInfoFirstnameField(), user.getFirstName()); 074 invitation.setPropertyValue(config.getUserInfoLastnameField(), user.getLastName()); 075 invitation.setPropertyValue(config.getUserInfoEmailField(), user.getEmail()); 076 invitation.setPropertyValue(config.getUserInfoGroupsField(), user.getGroups().toArray()); 077 invitation.setPropertyValue(config.getUserInfoTenantIdField(), user.getTenantId()); 078 invitation.setPropertyValue(config.getUserInfoCompanyField(), user.getCompany()); 079 invitation.setPropertyValue("registration:comment", comment); 080 081 return invitationService.submitRegistrationRequest(invitation, info, validationMethod, autoAccept); 082 } 083}