Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/src/main/java/com/cloud/user/AccountManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,10 @@ protected boolean lockAccount(long accountId) {
public boolean deleteAccount(AccountVO account, long callerUserId, Account caller) {
long accountId = account.getId();

if (account.getState() != State.DISABLED) {
Comment thread
DaanHoogland marked this conversation as resolved.
Outdated
throw new CloudRuntimeException("Account must be disabled before it can be deleted.");
}

// delete the account record
if (!_accountDao.remove(accountId)) {
logger.error("Unable to delete account " + accountId);
Expand Down
17 changes: 16 additions & 1 deletion server/src/test/java/com/cloud/user/AccountManagerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public void disableAccount() throws ConcurrentOperationException, ResourceUnavai
public void deleteUserAccount() {
AccountVO account = new AccountVO();
account.setId(42l);
account.setState(State.DISABLED);
DomainVO domain = new DomainVO();
Mockito.when(_accountDao.findById(42l)).thenReturn(account);
Mockito.doNothing().when(accountManagerImpl).checkAccess(Mockito.any(Account.class), Mockito.isNull(), Mockito.anyBoolean(), Mockito.any(Account.class));
Expand Down Expand Up @@ -195,6 +196,7 @@ public void deleteUserAccount() {
public void deleteUserAccountCleanup() {
AccountVO account = new AccountVO();
account.setId(42l);
account.setState(State.DISABLED);
DomainVO domain = new DomainVO();
Mockito.when(_accountDao.findById(42l)).thenReturn(account);
Mockito.doNothing().when(accountManagerImpl).checkAccess(Mockito.any(Account.class), Mockito.isNull(), Mockito.anyBoolean(), Mockito.any(Account.class));
Expand All @@ -211,6 +213,16 @@ public void deleteUserAccountCleanup() {
Mockito.verify(_accountDao, Mockito.atLeastOnce()).markForCleanup(Mockito.eq(42l));
}

@Test (expected = CloudRuntimeException.class)
public void deleteUserAccountEnabled() {
AccountVO account = new AccountVO();
account.setId(42l);
account.setState(State.ENABLED);
Mockito.when(_accountDao.findById(42l)).thenReturn(account);
Mockito.doNothing().when(accountManagerImpl).checkAccess(Mockito.any(Account.class), Mockito.isNull(), Mockito.anyBoolean(), Mockito.any(Account.class));
accountManagerImpl.deleteUserAccount(42l);
}

@Test (expected = InvalidParameterValueException.class)
public void deleteUserAccountTestIfAccountIdIsEqualToCallerIdShouldThrowException() {
try (MockedStatic<CallContext> callContextMocked = Mockito.mockStatic(CallContext.class)) {
Expand All @@ -234,8 +246,11 @@ public void deleteUserAccountTestIfAccountIdIsNotEqualToCallerAccountIdShouldNot
callContextMocked.when(CallContext::current).thenReturn(callContextMock);
long accountId = 1L;

AccountVO account = new AccountVO();
account.setId(accountId);
account.setState(State.DISABLED);
Mockito.doReturn(accountVoMock).when(callContextMock).getCallingAccount();
Mockito.doReturn(accountVoMock).when(_accountDao).findById(Mockito.anyLong());
Mockito.doReturn(account).when(_accountDao).findById(accountId);
Mockito.doReturn(2L).when(accountVoMock).getId();
Mockito.doReturn(true).when(accountManagerImpl).isDeleteNeeded(Mockito.any(), Mockito.anyLong(), Mockito.any());
Mockito.doReturn(new ArrayList<Long>()).when(_projectAccountDao).listAdministratedProjectIds(Mockito.anyLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected void defineMocksBehavior() throws AgentUnavailableException, Concurren

AccountVO account = new AccountVO();
account.setId(ACCOUNT_ID);
account.setState(Account.State.DISABLED);
when(_accountDao.remove(ACCOUNT_ID)).thenReturn(true);
when(_accountDao.findById(ACCOUNT_ID)).thenReturn(account);

Expand Down
8 changes: 7 additions & 1 deletion ui/public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@
"label.endipv6": "IPv6 end IP",
"label.endpoint": "Endpoint",
"label.endport": "End port",
"label.enter.account.name": "Enter the account name",
"label.enter.code": "Enter 2FA code to verify",
"label.enter.static.pin": "Enter static PIN to verify",
"label.enter.token": "Enter token",
Expand Down Expand Up @@ -2939,7 +2940,11 @@
"message.dedicating.host": "Dedicating host...",
"message.dedicating.pod": "Dedicating pod...",
"message.dedicating.zone": "Dedicating zone...",
"message.delete.account": "Please confirm that you want to delete this Account.",
"message.delete.account.confirm": "Please confirm that you want to delete this account by entering the name of the account below.",
"message.delete.account.failed": "Delete account failed",
"message.delete.account.processing": "Deleting account",
"message.delete.account.success": "Successfully deleted account",
"message.delete.account.warning": "Deleting this account will delete all of the instances, volumes and snapshots associated with the account.",
"message.delete.acl.processing": "Removing ACL rule...",
"message.delete.acl.rule": "Remove ACL rule",
"message.delete.acl.rule.failed": "Failed to remove ACL rule.",
Expand Down Expand Up @@ -3031,6 +3036,7 @@
"message.enabled.vpn.ip.sec": "Your IPSec pre-shared key is",
"message.enabling.security.group.provider": "Enabling security group provider",
"message.enter.valid.nic.ip": "Please enter a valid IP address for NIC",
"message.error.account.delete.name.mismatch": "Name entered doesn't match the account name.",
"message.error.access.key": "Please enter access key.",
"message.error.add.guest.network": "Either IPv4 fields or IPv6 fields need to be filled when adding a guest Network.",
"message.error.add.interface.static.route": "Adding interface Static Route failed",
Expand Down
6 changes: 2 additions & 4 deletions ui/src/config/section/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,12 @@ export default {
api: 'deleteAccount',
icon: 'delete-outlined',
label: 'label.action.delete.account',
message: 'message.delete.account',
dataView: true,
disabled: (record, store) => {
return record.id !== 'undefined' && store.userInfo.accountid === record.id
return (record.id !== 'undefined' && store.userInfo.accountid === record.id) || record.state !== 'disabled'
},
groupAction: true,
popup: true,
groupMap: (selection) => { return selection.map(x => { return { id: x } }) }
component: shallowRef(defineAsyncComponent(() => import('@/views/iam/DeleteAccount.vue')))
}
]
}
142 changes: 142 additions & 0 deletions ui/src/views/iam/DeleteAccount.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

<template>
<a-form
class="form"
:ref="formRef"
:model="form"
:rules="rules"
layout="vertical"
@finish="handleSubmit"
v-ctrl-enter="handleSubmit"
>
<div style="margin-bottom: 10px">
<a-alert type="warning">
<template #message>
<div v-html="$t('message.delete.account.warning')"></div>
</template>
</a-alert>
</div>
<div style="margin-bottom: 10px">
<a-alert>
<template #message>
<div v-html="$t('message.delete.account.confirm')"></div>
</template>
</a-alert>
</div>
<a-form-item name="name" ref="name">
<a-input
v-model:value="form.name"
:placeholder="$t('label.enter.account.name')"
style="width: auto"/>
</a-form-item>
<p v-if="error" class="error">{{ error }}</p>
<div :span="24" class="actions">
<a-button @click="closeModal">{{ $t('label.cancel') }}</a-button>
<a-button type="primary" ref="submit" @click="handleSubmit">{{ $t('label.ok') }}</a-button>
</div>
</a-form>
</template>
<script>

import { ref, reactive } from 'vue'
import { api } from '@/api'

export default {
name: 'DeleteAccount',
props: {
resource: {
type: Object,
required: true
}
},
data () {
return {
error: ''
}
},
created () {
this.initForm()
},
methods: {
initForm () {
this.formRef = ref()
this.form = reactive({
})
this.rules = reactive({
name: [{ required: true, message: this.$t('label.required') }]
})
},
closeModal () {
this.$emit('close-action')
},
handleSubmit (e) {
e.preventDefault()
this.formRef.value.validate().then(async () => {
if (this.form.name !== this.resource.name) {
this.error = `${this.$t('message.error.account.delete.name.mismatch')}`
return
}

api('deleteAccount', {
id: this.resource.id
}).then(response => {
this.$pollJob({
jobId: response.deleteaccountresponse.jobid,
title: this.$t('label.action.delete.account'),
description: this.resource.id,
successMessage: `${this.$t('message.delete.account.success')} - ${this.resource.name}`,
errorMessage: `${this.$t('message.delete.account.failed')} - ${this.resource.name}`,
loadingMessage: `${this.$t('message.delete.account.processing')} - ${this.resource.name}`,
catchMessage: this.$t('error.fetching.async.job.result')
})
this.closeModal()
}).catch(error => {
this.$notifyError(error)
})
}).catch((error) => {
this.formRef.value.scrollToField(error.errorFields[0].name)
})
}
}
}
</script>

<style lang="scss" scoped>
.form {
width: 80vw;

@media (min-width: 500px) {
width: 400px;
}
}
.actions {
display: flex;
justify-content: flex-end;
margin-top: 20px;
button {
&:not(:last-child) {
margin-right: 10px;
}
}
}
.error {
color: red;
margin-top: 10px;
}
</style>