|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.api.command.admin.user; |
| 18 | + |
| 19 | +import com.cloud.user.Account; |
| 20 | +import com.cloud.user.User; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import org.apache.cloudstack.acl.RoleType; |
| 23 | +import org.apache.cloudstack.api.APICommand; |
| 24 | +import org.apache.cloudstack.api.ApiConstants; |
| 25 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 26 | +import org.apache.cloudstack.api.BaseCmd; |
| 27 | +import org.apache.cloudstack.api.Parameter; |
| 28 | +import org.apache.cloudstack.api.ServerApiException; |
| 29 | +import org.apache.cloudstack.api.response.AccountResponse; |
| 30 | +import org.apache.cloudstack.api.response.SuccessResponse; |
| 31 | +import org.apache.cloudstack.api.response.UserResponse; |
| 32 | +import org.apache.cloudstack.context.CallContext; |
| 33 | +import org.apache.cloudstack.region.RegionService; |
| 34 | +import org.apache.commons.lang3.ObjectUtils; |
| 35 | +import org.apache.log4j.Logger; |
| 36 | + |
| 37 | +import javax.inject.Inject; |
| 38 | + |
| 39 | +@APICommand(name = "moveUser", |
| 40 | + description = "Moves a user to another account", |
| 41 | + responseObject = SuccessResponse.class, |
| 42 | + requestHasSensitiveInfo = false, |
| 43 | + responseHasSensitiveInfo = false, |
| 44 | + since = "4.11", |
| 45 | + authorized = {RoleType.Admin}) |
| 46 | +public class MoveUserCmd extends BaseCmd { |
| 47 | + public static final Logger s_logger = Logger.getLogger(UpdateUserCmd.class.getName()); |
| 48 | + |
| 49 | + public static final String APINAME = "moveUser"; |
| 50 | + |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + //////////////// API parameters ///////////////////// |
| 53 | + ///////////////////////////////////////////////////// |
| 54 | + @Parameter(name = ApiConstants.ID, |
| 55 | + type = CommandType.UUID, |
| 56 | + entityType = UserResponse.class, |
| 57 | + required = true, |
| 58 | + description = "id of the user to be deleted") |
| 59 | + private Long id; |
| 60 | + |
| 61 | + @Parameter(name = ApiConstants.ACCOUNT, |
| 62 | + type = CommandType.STRING, |
| 63 | + description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name.") |
| 64 | + private String accountName; |
| 65 | + |
| 66 | + @Parameter(name = ApiConstants.ACCOUNT_ID, |
| 67 | + type = CommandType.UUID, |
| 68 | + entityType = AccountResponse.class, |
| 69 | + description = "Creates the user under the specified domain. Has to be accompanied with the account parameter") |
| 70 | + private Long accountId; |
| 71 | + |
| 72 | + @Inject |
| 73 | + RegionService _regionService; |
| 74 | + |
| 75 | + ///////////////////////////////////////////////////// |
| 76 | + /////////////////// Accessors /////////////////////// |
| 77 | + ///////////////////////////////////////////////////// |
| 78 | + |
| 79 | + public Long getId() { |
| 80 | + return id; |
| 81 | + } |
| 82 | + |
| 83 | + public String getAccountName() { |
| 84 | + return accountName; |
| 85 | + } |
| 86 | + |
| 87 | + public Long getAccountId() { |
| 88 | + return accountId; |
| 89 | + } |
| 90 | + |
| 91 | + ///////////////////////////////////////////////////// |
| 92 | + /////////////// API Implementation/////////////////// |
| 93 | + ///////////////////////////////////////////////////// |
| 94 | + |
| 95 | + @Override |
| 96 | + public String getCommandName() { |
| 97 | + return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public long getEntityOwnerId() { |
| 102 | + User user = _entityMgr.findById(User.class, getId()); |
| 103 | + if (user != null) { |
| 104 | + return user.getAccountId(); |
| 105 | + } |
| 106 | + |
| 107 | + return Account.ACCOUNT_ID_SYSTEM; // no account info given, parent this command to SYSTEM so ERROR events are tracked |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void execute() { |
| 112 | + Preconditions.checkNotNull(getId(),"I have to have an user to move!"); |
| 113 | + Preconditions.checkState(ObjectUtils.anyNotNull(getAccountId(),getAccountName()),"provide either an account name or an account id!"); |
| 114 | + |
| 115 | + CallContext.current().setEventDetails("UserId: " + getId()); |
| 116 | + boolean result = |
| 117 | + _regionService.moveUser(this); |
| 118 | + if (result) { |
| 119 | + SuccessResponse response = new SuccessResponse(getCommandName()); |
| 120 | + this.setResponseObject(response); |
| 121 | + } else { |
| 122 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move the user to a new account"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments