Refactored the delete user cmd

This commit is contained in:
abhishek 2010-08-26 17:58:53 -07:00
parent 3d8a3ef358
commit 126da4baf7
3 changed files with 73 additions and 61 deletions

View File

@ -18,27 +18,17 @@
package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseAsyncCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.utils.Pair;
public class DeleteUserCmd extends BaseCmd {
import com.cloud.api.BaseCmd.Manager;
@Implementation(method="deleteUser", manager=Manager.ManagementServer)
public class DeleteUserCmd extends BaseAsyncCmd {
public static final Logger s_logger = Logger.getLogger(DeleteUserCmd.class.getName());
private static final String s_name = "deleteuserresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -69,29 +59,32 @@ public class DeleteUserCmd extends BaseCmd {
return s_name;
}
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Long userId = (Long)params.get(BaseCmd.Properties.ID.getName());
//Verify that the user exists in the system
User user = getManagementServer().getUser(userId.longValue());
if (user == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find user " + userId);
}
// If the user is a System user, return an error. We do not allow this
Account account = getManagementServer().findAccountById(user.getAccountId());
if ((account != null) && (account.getId() == Account.ACCOUNT_ID_SYSTEM)) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "user id : " + userId + " is a system account, delete is not allowed");
}
long jobId = getManagementServer().deleteUserAsync(userId.longValue());
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
return returnValues;
}
// @Override
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
// Long userId = (Long)params.get(BaseCmd.Properties.ID.getName());
//
// //Verify that the user exists in the system
// User user = getManagementServer().getUser(userId.longValue());
// if (user == null) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find user " + userId);
// }
//
// // If the user is a System user, return an error. We do not allow this
// Account account = getManagementServer().findAccountById(user.getAccountId());
// if ((account != null) && (account.getId() == Account.ACCOUNT_ID_SYSTEM)) {
// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "user id : " + userId + " is a system account, delete is not allowed");
// }
//
// long jobId = getManagementServer().deleteUserAsync(userId.longValue());
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
// return returnValues;
// }
@Override
public String getResponse() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -27,6 +27,7 @@ import com.cloud.api.commands.CreateDomainCmd;
import com.cloud.api.commands.CreatePortForwardingServiceCmd;
import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd;
import com.cloud.api.commands.CreateUserCmd;
import com.cloud.api.commands.DeleteUserCmd;
import com.cloud.api.commands.DisassociateIPAddrCmd;
import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd;
@ -175,8 +176,8 @@ public interface ManagementServer {
* @param userId
* @return true if delete was successful, false otherwise
*/
boolean deleteUser(long userId);
long deleteUserAsync(long userId);
boolean deleteUser(DeleteUserCmd cmd);
// long deleteUserAsync(long userId);
/**
* Disables a user by userId

View File

@ -278,7 +278,7 @@ import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.google.gson.Gson;
public class ManagementServerImpl implements ManagementServer {
public class ManagementServerImpl implements ManagementServer {
public static final Logger s_logger = Logger.getLogger(ManagementServerImpl.class.getName());
private final AccountManager _accountMgr;
@ -839,8 +839,7 @@ public class ManagementServerImpl implements ManagementServer {
}
}
@Override
public boolean deleteUser(long userId) {
private boolean deleteUserInternal(long userId) {
UserAccount userAccount = null;
Long accountId = null;
String username = null;
@ -893,20 +892,20 @@ public class ManagementServerImpl implements ManagementServer {
}
}
@Override
public long deleteUserAsync(long userId) {
Long param = new Long(userId);
Gson gson = GsonHelper.getBuilder().create();
AsyncJobVO job = new AsyncJobVO();
job.setUserId(UserContext.current().getUserId());
job.setAccountId(Account.ACCOUNT_ID_SYSTEM);
job.setCmd("DeleteUser");
job.setCmdInfo(gson.toJson(param));
job.setCmdOriginator(DeleteUserCmd.getStaticName());
return _asyncMgr.submitAsyncJob(job);
}
// @Override
// public long deleteUserAsync(long userId) {
// Long param = new Long(userId);
// Gson gson = GsonHelper.getBuilder().create();
//
// AsyncJobVO job = new AsyncJobVO();
// job.setUserId(UserContext.current().getUserId());
// job.setAccountId(Account.ACCOUNT_ID_SYSTEM);
// job.setCmd("DeleteUser");
// job.setCmdInfo(gson.toJson(param));
// job.setCmdOriginator(DeleteUserCmd.getStaticName());
//
// return _asyncMgr.submitAsyncJob(job);
// }
public boolean deleteAccount(AccountVO account) {
long accountId = account.getId();
@ -6238,7 +6237,7 @@ public class ManagementServerImpl implements ManagementServer {
userSc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
List<UserVO> users = _userDao.search(userSc, null);
for (UserVO user : users) {
success = (success && deleteUser(user.getId()));
success = (success && deleteUserInternal(user.getId()));
}
}
}
@ -8281,5 +8280,24 @@ public class ManagementServerImpl implements ManagementServer {
return lockAccountInternal(account.getId());
}
@Override
public boolean deleteUser(DeleteUserCmd cmd) {
Long userId = cmd.getId();
//Verify that the user exists in the system
User user = getUser(userId.longValue());
if (user == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find user " + userId);
}
// If the user is a System user, return an error. We do not allow this
Account account = _accountDao.findById(user.getAccountId());
if ((account != null) && (account.getId() == Account.ACCOUNT_ID_SYSTEM)) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "user id : " + userId + " is a system account, delete is not allowed");
}
return deleteUserInternal(userId);
}
}