From 001fc0f7ae3b8976c38830e699d4b9acdca2741c Mon Sep 17 00:00:00 2001 From: abhishek Date: Tue, 17 Aug 2010 12:12:07 -0700 Subject: [PATCH] Refactoring update user command --- .../com/cloud/api/commands/UpdateUserCmd.java | 97 +++---------------- .../com/cloud/api/commands/UpdateVMCmd.java | 8 -- .../com/cloud/server/ManagementServer.java | 4 +- .../cloud/server/ManagementServerImpl.java | 75 ++++++++++++-- 4 files changed, 80 insertions(+), 104 deletions(-) diff --git a/server/src/com/cloud/api/commands/UpdateUserCmd.java b/server/src/com/cloud/api/commands/UpdateUserCmd.java index 1dff585fc19..f95669de99a 100644 --- a/server/src/com/cloud/api/commands/UpdateUserCmd.java +++ b/server/src/com/cloud/api/commands/UpdateUserCmd.java @@ -25,30 +25,20 @@ import java.util.Map; import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; +import com.cloud.api.BaseCmd.Manager; import com.cloud.exception.InvalidParameterValueException; import com.cloud.user.Account; import com.cloud.user.User; import com.cloud.utils.Pair; - + +@Implementation(method="updateUser", manager=Manager.UserVmManager) public class UpdateUserCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(UpdateUserCmd.class.getName()); private static final String s_name = "updateuserresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.API_KEY, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.EMAIL, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.FIRSTNAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.LASTNAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PASSWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.SECRET_KEY, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.TIMEZONE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USERNAME, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -128,75 +118,12 @@ public class UpdateUserCmd extends BaseCmd { public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long userId = (Long)params.get(BaseCmd.Properties.ID.getName()); - String username = (String)params.get(BaseCmd.Properties.USERNAME.getName()); - String password = (String)params.get(BaseCmd.Properties.PASSWORD.getName()); - String firstname = (String)params.get(BaseCmd.Properties.FIRSTNAME.getName()); - String lastname = (String)params.get(BaseCmd.Properties.LASTNAME.getName()); - String email = (String)params.get(BaseCmd.Properties.EMAIL.getName()); - String timezone = (String)params.get(BaseCmd.Properties.TIMEZONE.getName()); - String apiKey = (String)params.get(BaseCmd.Properties.API_KEY.getName()); - String secretKey = (String)params.get(BaseCmd.Properties.SECRET_KEY.getName()); - //check if 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 by id"); - } - - if((apiKey == null && secretKey != null) || (apiKey != null && secretKey == null)) - { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please provide an api key/secret key pair"); - } - - // If the account is an admin type, 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 system account, update is not allowed"); - } - - if (firstname == null) { - firstname = user.getFirstname(); - } - if (lastname == null) { - lastname = user.getLastname(); - } - if (username == null) { - username = user.getUsername(); - } - if (password == null) { - password = user.getPassword(); - } - if (email == null) { - email = user.getEmail(); - } - if (timezone == null) { - timezone = user.getTimezone(); - } - if (apiKey == null) { - apiKey = user.getApiKey(); - } - if (secretKey == null) { - secretKey = user.getSecretKey(); - } - List> returnValues = new ArrayList>(); - boolean success = false; - try { - success = getManagementServer().updateUser(user.getId(), username, password, firstname, lastname, email, timezone, apiKey, secretKey); - } catch (InvalidParameterValueException e) - { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage()); - } - if (success) { - returnValues.add(new Pair (BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); - } else { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "failed to update user"); - } - return returnValues; - } + + @Override + public String getResponse() { + // TODO Auto-generated method stub + + //response returned is true or false, based on which you can throw an error + return null; + } } \ No newline at end of file diff --git a/server/src/com/cloud/api/commands/UpdateVMCmd.java b/server/src/com/cloud/api/commands/UpdateVMCmd.java index 410c5829ea4..fba369ab9c7 100644 --- a/server/src/com/cloud/api/commands/UpdateVMCmd.java +++ b/server/src/com/cloud/api/commands/UpdateVMCmd.java @@ -18,27 +18,19 @@ 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.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; import com.cloud.api.BaseCmd.Manager; import com.cloud.api.response.UpgradeVmResponse; -import com.cloud.user.Account; -import com.cloud.utils.Pair; import com.cloud.vm.UserVmVO; @Implementation(method="updateVirtualMachine", manager=Manager.UserVmManager) public class UpdateVMCmd extends BaseCmd{ public static final Logger s_logger = Logger.getLogger(UpdateVMCmd.class.getName()); private static final String s_name = "updatevirtualmachineresponse"; - private static final List> s_properties = new ArrayList>(); ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index f13c55323fb..dc6990f3cdd 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -32,6 +32,7 @@ import com.cloud.api.commands.GetCloudIdentifierCmd; import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateTemplateCmd; +import com.cloud.api.commands.UpdateUserCmd; import com.cloud.async.AsyncJobResult; import com.cloud.async.AsyncJobVO; import com.cloud.capacity.CapacityVO; @@ -298,7 +299,7 @@ public interface ManagementServer { * @return true if update was successful, false otherwise * @throws InvalidParameterValueException */ - boolean updateUser(long userId, String username, String password, String firstname, String lastname, String email, String timezone, String apiKey, String secretKey) throws InvalidParameterValueException; +// boolean updateUser(long userId, String username, String password, String firstname, String lastname, String email, String timezone, String apiKey, String secretKey) throws InvalidParameterValueException; /** * Locate a user by their apiKey @@ -2139,4 +2140,5 @@ public interface ManagementServer { // boolean addConfig(String instance, String component, String category, String name, String value, String description); boolean validateCustomVolumeSizeRange(long size) throws InvalidParameterValueException; + boolean updateUser(UpdateUserCmd cmd) throws InvalidParameterValueException; } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 48cf549166b..408b769204f 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -84,6 +84,7 @@ import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateDomainCmd; import com.cloud.api.commands.UpdateTemplateCmd; +import com.cloud.api.commands.UpdateUserCmd; import com.cloud.api.commands.UpgradeVMCmd; import com.cloud.async.AsyncInstanceCreateStatus; import com.cloud.async.AsyncJobExecutor; @@ -1273,14 +1274,68 @@ public class ManagementServerImpl implements ManagementServer { @Override - public boolean updateUser(long userId, String username, String password, String firstname, String lastname, String email, String timezone, String apiKey, String secretKey) throws InvalidParameterValueException{ - UserVO user = _userDao.findById(userId); + public boolean updateUser(UpdateUserCmd cmd) throws InvalidParameterValueException + { + Long id = cmd.getId(); + String apiKey = cmd.getApiKey(); + String firstName = cmd.getFirstname(); + String email = cmd.getEmail(); + String lastName = cmd.getLastname(); + String password = cmd.getPassword(); + String secretKey = cmd.getSecretKey(); + String timeZone = cmd.getTimezone(); + String userName = cmd.getUsername(); + + //Input validation + UserVO user = _userDao.getUser(id); + + if (user == null) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find user by id"); + } + + if((apiKey == null && secretKey != null) || (apiKey != null && secretKey == null)) + { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please provide an api key/secret key pair"); + } + + // If the account is an admin type, return an error. We do not allow this + Account account = (Account)UserContext.current().getAccountObject(); + + if (account != null && (account.getId() == Account.ACCOUNT_ID_SYSTEM)) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "user id : " + id + " is system account, update is not allowed"); + } + + if (firstName == null) { + firstName = user.getFirstname(); + } + if (lastName == null) { + lastName = user.getLastname(); + } + if (userName == null) { + userName = user.getUsername(); + } + if (password == null) { + password = user.getPassword(); + } + if (email == null) { + email = user.getEmail(); + } + if (timeZone == null) { + timeZone = user.getTimezone(); + } + if (apiKey == null) { + apiKey = user.getApiKey(); + } + if (secretKey == null) { + secretKey = user.getSecretKey(); + } + Long accountId = user.getAccountId(); if (s_logger.isDebugEnabled()) { - s_logger.debug("updating user with id: " + userId); + s_logger.debug("updating user with id: " + id); } - UserAccount userAccount = _userAccountDao.findById(userId); + UserAccount userAccount = _userAccountDao.findById(id); try { //check if the apiKey and secretKey are globally unique @@ -1292,8 +1347,8 @@ public class ManagementServerImpl implements ManagementServer { { User usr = apiKeyOwner.first(); - if(usr.getId() != userId) - throw new InvalidParameterValueException("The api key:"+apiKey+" exists in the system for user id:"+userId+" ,please provide a unique key"); + if(usr.getId() != id) + throw new InvalidParameterValueException("The api key:"+apiKey+" exists in the system for user id:"+id+" ,please provide a unique key"); else { //allow the updation to take place @@ -1303,12 +1358,12 @@ public class ManagementServerImpl implements ManagementServer { } - _userDao.update(userId, username, password, firstname, lastname, email, accountId, timezone, apiKey, secretKey); - EventUtils.saveEvent(new Long(1), Long.valueOf(1), EventVO.LEVEL_INFO, EventTypes.EVENT_USER_UPDATE, "User, " + username + " for accountId = " - + accountId + " domainId = " + userAccount.getDomainId() + " and timezone = "+timezone + " was updated."); + _userDao.update(id, userName, password, firstName, lastName, email, accountId, timeZone, apiKey, secretKey); + EventUtils.saveEvent(new Long(1), Long.valueOf(1), EventVO.LEVEL_INFO, EventTypes.EVENT_USER_UPDATE, "User, " + userName + " for accountId = " + + accountId + " domainId = " + userAccount.getDomainId() + " and timezone = "+timeZone + " was updated."); } catch (Throwable th) { s_logger.error("error updating user", th); - EventUtils.saveEvent(Long.valueOf(1), Long.valueOf(1), EventVO.LEVEL_ERROR, EventTypes.EVENT_USER_UPDATE, "Error updating user, " + username + EventUtils.saveEvent(Long.valueOf(1), Long.valueOf(1), EventVO.LEVEL_ERROR, EventTypes.EVENT_USER_UPDATE, "Error updating user, " + userName + " for accountId = " + accountId + " and domainId = " + userAccount.getDomainId()); return false; }