Refactoring disableUser and disableAccount to new API framework (they are both async commands).

This commit is contained in:
Kris McQueen 2010-09-29 13:57:26 -07:00
parent 95a7f20a0a
commit 9e2d1f5e70
5 changed files with 35 additions and 12 deletions

View File

@ -19,7 +19,7 @@ package com.cloud.api.commands;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseAsyncCmd;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
@ -27,7 +27,7 @@ import com.cloud.api.ResponseObject;
import com.cloud.api.response.SuccessResponse;
@Implementation(method="disableAccount", manager=Manager.ManagementServer)
public class DisableAccountCmd extends BaseCmd {
public class DisableAccountCmd extends BaseAsyncCmd {
public static final Logger s_logger = Logger.getLogger(DisableAccountCmd.class.getName());
private static final String s_name = "disableaccountresponse";

View File

@ -19,12 +19,14 @@ package com.cloud.api.commands;
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.ResponseObject;
import com.cloud.api.response.SuccessResponse;
public class DisableUserCmd extends BaseCmd {
@Implementation(method="disableUser")
public class DisableUserCmd extends BaseAsyncCmd {
public static final Logger s_logger = Logger.getLogger(DisableUserCmd.class.getName());
private static final String s_name = "disableuserresponse";

View File

@ -46,7 +46,7 @@ public class DisableUserExecutor extends BaseAsyncJobExecutor {
AsyncJobVO job = getJob();
ManagementServer managementServer = asyncMgr.getExecutorContext().getManagementServer();
Long param = gson.fromJson(job.getCmdInfo(), Long.class);
/*
SyncQueueItemVO syncItem = getSyncSource();
if(syncItem == null) {
initialSchedule(managementServer, param.longValue());
@ -76,6 +76,7 @@ public class DisableUserExecutor extends BaseAsyncJobExecutor {
+ " has ceased, still more to go to disable account for user " + param);
}
}
*/
return true;
}
@ -90,7 +91,7 @@ public class DisableUserExecutor extends BaseAsyncJobExecutor {
"User " + userId + " does not exist");
return;
}
/*
if(managementServer.disableUser(userId)) {
if(needToDisableAccount(user)) {
if(s_logger.isInfoEnabled())
@ -120,6 +121,7 @@ public class DisableUserExecutor extends BaseAsyncJobExecutor {
asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_FAILED, BaseCmd.INTERNAL_ERROR,
"Unable to disable user " + userId);
}
*/
}
private boolean needToDisableAccount(UserVO user) {

View File

@ -34,6 +34,7 @@ import com.cloud.api.commands.DeletePortForwardingServiceCmd;
import com.cloud.api.commands.DeletePreallocatedLunCmd;
import com.cloud.api.commands.DeleteUserCmd;
import com.cloud.api.commands.DeployVMCmd;
import com.cloud.api.commands.DisableUserCmd;
import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd;
import com.cloud.api.commands.ExtractVolumeCmd;
@ -202,10 +203,10 @@ public interface ManagementServer {
/**
* Disables a user by userId
* @param userId
* @param cmd the command wrapping the userId parameter
* @return true if disable was successful, false otherwise
*/
boolean disableUser(long userId);
boolean disableUser(DisableUserCmd cmd);
/**
* Disables an account by accountId

View File

@ -71,6 +71,7 @@ import com.cloud.api.commands.DeletePortForwardingServiceCmd;
import com.cloud.api.commands.DeletePreallocatedLunCmd;
import com.cloud.api.commands.DeleteUserCmd;
import com.cloud.api.commands.DeployVMCmd;
import com.cloud.api.commands.DisableUserCmd;
import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd;
import com.cloud.api.commands.ExtractVolumeCmd;
@ -964,7 +965,8 @@ public class ManagementServerImpl implements ManagementServer {
}
@Override
public boolean disableUser(long userId) {
public boolean disableUser(DisableUserCmd cmd) {
Long userId = cmd.getId();
if (userId <= 2) {
if (s_logger.isInfoEnabled()) {
s_logger.info("disableUser -- invalid user id: " + userId);
@ -972,7 +974,21 @@ public class ManagementServerImpl implements ManagementServer {
return false;
}
return doSetUserStatus(userId, Account.ACCOUNT_STATE_DISABLED);
boolean success = doSetUserStatus(userId, Account.ACCOUNT_STATE_DISABLED);
if (success) {
User user = _userDao.findById(userId);
List<UserVO> allUsersByAccount = _userDao.listByAccount(user.getAccountId());
for (UserVO oneUser : allUsersByAccount) {
if (oneUser.getState().equals(Account.ACCOUNT_STATE_ENABLED)) {
return true;
}
}
// there are no enabled users attached to this user's account, disable the account
return disableAccount(user.getAccountId());
} else {
return false;
}
}
@Override
@ -3189,10 +3205,11 @@ public class ManagementServerImpl implements ManagementServer {
public List<AccountVO> searchForAccounts(ListAccountsCmd cmd) {
Account account = (Account)UserContext.current().getAccountObject();
Long domainId = cmd.getDomainId();
String accountName = cmd.getAccountName();
Long accountId = cmd.getId();
String accountName = null;
if ((account == null) || isAdmin(account.getType())) {
accountName = cmd.getSearchName(); // admin's can specify a name to search for
if (domainId == null) {
// default domainId to the admin's domain
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
@ -3203,6 +3220,7 @@ public class ManagementServerImpl implements ManagementServer {
}
} else {
accountId = account.getId();
accountName = account.getAccountName(); // regular users must be constrained to their own account
}
Filter searchFilter = new Filter(AccountVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());