diff --git a/server/src/main/java/com/cloud/user/AccountManagerImpl.java b/server/src/main/java/com/cloud/user/AccountManagerImpl.java index 9f760e655e5..031ba9e22c4 100644 --- a/server/src/main/java/com/cloud/user/AccountManagerImpl.java +++ b/server/src/main/java/com/cloud/user/AccountManagerImpl.java @@ -1181,6 +1181,18 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M return _userAccountDao.findById(userId); } + private boolean isValidRoleChange(Account account, Role role) { + Long currentAccRoleId = account.getRoleId(); + Role currentRole = roleService.findRole(currentAccRoleId); + + if (role.getRoleType().ordinal() < currentRole.getRoleType().ordinal() && ((account.getType() == Account.Type.NORMAL && role.getRoleType().getAccountType().ordinal() > Account.Type.NORMAL.ordinal()) || + account.getType().ordinal() > Account.Type.NORMAL.ordinal() && role.getRoleType().getAccountType().ordinal() < account.getType().ordinal() && role.getRoleType().getAccountType().ordinal() > 0)) { + throw new PermissionDeniedException(String.format("Unable to update account role to %s as you are " + + "attempting to escalate the account %s to account type %s which has higher privileges", role.getName(), account.getAccountName(), role.getRoleType().getAccountType().name())); + } + return true; + } + /** * if there is any permission under the requested role that is not permitted for the caller, refuse */ @@ -1897,7 +1909,10 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M "in the domain '" + domainId + "'."); } + Role role = roleService.findRole(roleId); + isValidRoleChange(account, role); acctForUpdate.setRoleId(roleId); + acctForUpdate.setType(role.getRoleType().getAccountType()); checkRoleEscalation(getCurrentCallingAccount(), acctForUpdate); }