diff --git a/server/src/com/cloud/api/commands/LockUserCmd.java b/server/src/com/cloud/api/commands/LockUserCmd.java index fde82869a47..94d5fc63fe1 100644 --- a/server/src/com/cloud/api/commands/LockUserCmd.java +++ b/server/src/com/cloud/api/commands/LockUserCmd.java @@ -17,29 +17,18 @@ */ 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.user.Account; -import com.cloud.user.User; -import com.cloud.utils.Pair; - +import com.cloud.api.BaseCmd.Manager; + +@Implementation(method="lockUser", manager=Manager.ManagementServer) public class LockUserCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(LockUserCmd.class.getName()); private static final String s_name = "lockuserresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -63,36 +52,39 @@ public class LockUserCmd extends BaseCmd { public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Account adminAccount = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Long id = (Long)params.get(BaseCmd.Properties.ID.getName()); - - // Check if user with id exists in the system - User user = getManagementServer().findUserById(id); - if (user == null) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find user by id"); - } else if (user.getRemoved() != null) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find user by id"); - } - - // 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 : " + id + " is a system user, locking is not allowed"); - } - - if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), account.getDomainId())) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to lock user " + id + ", permission denied."); - } - - boolean success = getManagementServer().lockUser(id.longValue()); - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); - return returnValues; - } +// @Override +// public List> execute(Map params) { +// Account adminAccount = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// Long id = (Long)params.get(BaseCmd.Properties.ID.getName()); +// +// // Check if user with id exists in the system +// User user = getManagementServer().findUserById(id); +// if (user == null) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find user by id"); +// } else if (user.getRemoved() != null) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find user by id"); +// } +// +// // 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 : " + id + " is a system user, locking is not allowed"); +// } +// +// if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), account.getDomainId())) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to lock user " + id + ", permission denied."); +// } +// +// boolean success = getManagementServer().lockUser(id.longValue()); +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(success).toString())); +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 633b80d5e07..35cff7631f8 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -29,6 +29,7 @@ import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd; import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.GetCloudIdentifierCmd; +import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.RebootSystemVmCmd; import com.cloud.api.commands.RegisterCmd; import com.cloud.api.commands.RemovePortForwardingServiceCmd; @@ -252,7 +253,7 @@ public interface ManagementServer { * @param userId * @return true if enable was successful, false otherwise */ - boolean lockUser(long userId); + boolean lockUser(LockUserCmd cmd); /** * registerPreallocatedLun registers a preallocated lun in our database. diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 4442b72ef39..3792826a535 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -71,6 +71,7 @@ import com.cloud.api.commands.DeployVMCmd; import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.GetCloudIdentifierCmd; +import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.PrepareForMaintenanceCmd; import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd; import com.cloud.api.commands.RebootSystemVmCmd; @@ -1082,18 +1083,38 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public boolean lockUser(long userId) { + public boolean lockUser(LockUserCmd cmd) { boolean success = false; + + Account adminAccount = (Account)UserContext.current().getAccountObject(); + Long id = cmd.getId(); + + // Check if user with id exists in the system + User user = _userDao.findById(id); + if (user == null) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find user by id"); + } else if (user.getRemoved() != null) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find user by id"); + } + + // 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 : " + id + " is a system user, locking is not allowed"); + } + + if ((adminAccount != null) && !_domainDao.isChildDomain(adminAccount.getDomainId(), account.getDomainId())) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Failed to lock user " + id + ", permission denied."); + } // make sure the account is enabled too - UserVO user = _userDao.findById(userId); if (user != null) { // if the user is either locked already or disabled already, don't change state...only lock currently enabled users if (user.getState().equals(Account.ACCOUNT_STATE_LOCKED)) { // already locked...no-op return true; } else if (user.getState().equals(Account.ACCOUNT_STATE_ENABLED)) { - success = doSetUserStatus(userId, Account.ACCOUNT_STATE_LOCKED); + success = doSetUserStatus(user.getId(), Account.ACCOUNT_STATE_LOCKED); boolean lockAccount = true; List allUsersByAccount = _userDao.listByAccount(user.getAccountId()); @@ -1109,11 +1130,11 @@ public class ManagementServerImpl implements ManagementServer { } } else { if (s_logger.isInfoEnabled()) { - s_logger.info("Attempting to lock a non-enabled user, current state is " + user.getState() + " (userId: " + userId + "), locking failed."); + s_logger.info("Attempting to lock a non-enabled user, current state is " + user.getState() + " (userId: " + user.getId() + "), locking failed."); } } } else { - s_logger.warn("Unable to find user with id: " + userId); + s_logger.warn("Unable to find user with id: " + UserContext.current().getUserId()); } return success; }