Skip to content

Commit 64bbd0e

Browse files
committed
server,ui: prevent role change for default accounts
Fixes #10931 Role for default accounts shouldn't be changed. Appropriate error should be returned by the server and UI should not present option for them. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 5dfeb79 commit 64bbd0e

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,9 @@ public Pair<Long, Account> doInTransaction(TransactionStatus status) {
13981398
- New role should not be of type Admin with domain other than ROOT domain
13991399
*/
14001400
protected void validateRoleChange(Account account, Role role, Account caller) {
1401+
if (account.getRoleId() == role.getId()) {
1402+
return;
1403+
}
14011404
Role currentRole = roleService.findRole(account.getRoleId());
14021405
Role callerRole = roleService.findRole(caller.getRoleId());
14031406
String errorMsg = String.format("Unable to update account role to %s, ", role.getName());
@@ -1413,6 +1416,9 @@ protected void validateRoleChange(Account account, Role role, Account caller) {
14131416
throw new PermissionDeniedException(String.format("%s as either current or new role has higher " +
14141417
"privileges than the caller", errorMsg));
14151418
}
1419+
if (account.isDefault()) {
1420+
throw new PermissionDeniedException(String.format("%s as the account is a default account", errorMsg));
1421+
}
14161422
if (role.getRoleType().equals(RoleType.Admin) && account.getDomainId() != Domain.ROOT_DOMAIN) {
14171423
throw new PermissionDeniedException(String.format("%s as the user does not belong to the ROOT domain",
14181424
errorMsg));

server/src/test/java/com/cloud/user/AccountManagerImplTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,22 @@ public void testValidateRoleAdminCannotEscalateAdminFromNonRootDomain() {
13651365
accountManagerImpl.validateRoleChange(account, newRole, caller);
13661366
}
13671367

1368+
@Test(expected = PermissionDeniedException.class)
1369+
public void testValidateRoleAdminCannotChangeDefaultAdmin() {
1370+
Account account = Mockito.mock(Account.class);
1371+
Mockito.when(account.isDefault()).thenReturn(true);
1372+
Mockito.when(account.getRoleId()).thenReturn(1L);
1373+
Role newRole = Mockito.mock(Role.class);
1374+
Mockito.when(newRole.getRoleType()).thenReturn(RoleType.User);
1375+
Role callerRole = Mockito.mock(Role.class);
1376+
Mockito.when(callerRole.getRoleType()).thenReturn(RoleType.Admin);
1377+
Account caller = Mockito.mock(Account.class);
1378+
Mockito.when(caller.getRoleId()).thenReturn(2L);
1379+
Mockito.when(roleService.findRole(1L)).thenReturn(Mockito.mock(Role.class));
1380+
Mockito.when(roleService.findRole(2L)).thenReturn(callerRole);
1381+
accountManagerImpl.validateRoleChange(account, newRole, caller);
1382+
}
1383+
13681384
@Test
13691385
public void checkIfAccountManagesProjectsTestNotThrowExceptionWhenTheAccountIsNotAProjectAdministrator() {
13701386
long accountId = 1L;

ui/src/views/iam/EditAccount.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
v-model:value="form.networkdomain"
4141
:placeholder="apiParams.networkdomain.description" />
4242
</a-form-item>
43-
<a-form-item ref="roleid" name="roleid">
43+
<a-form-item ref="roleid" name="roleid" v-if="!resource.isdefault">
4444
<template #label>
4545
<tooltip-label :title="$t('label.role')" :tooltip="apiParams.roleid.description"/>
4646
</template>
@@ -145,11 +145,13 @@ export default {
145145
const params = {
146146
newname: values.newname,
147147
networkdomain: values.networkdomain,
148-
roleid: values.roleid,
149148
apikeyaccess: values.apikeyaccess,
150149
account: this.account,
151150
domainid: this.domainId
152151
}
152+
if (values.roleid) {
153+
params.roleid = values.roleid
154+
}
153155
if (this.isValidValueForKey(values, 'networkdomain') && values.networkdomain.length > 0) {
154156
params.networkdomain = values.networkdomain
155157
}

0 commit comments

Comments
 (0)