Skip to content

Commit 1d1bbed

Browse files
committed
fix: while creating a new account, check for his existance
Signed-off-by: Roberto Guido <info@madbob.org>
1 parent 5ed4a0a commit 1d1bbed

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

lib/Controller/AccountsController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,12 @@ public function create(string $accountName,
338338
$this->logger->info('Creating account disabled by admin.');
339339
return MailJsonResponse::error('Could not create account');
340340
}
341+
if (empty($this->accountService->findByUserIdAndAddress($this->userId, $emailAddress)) === false) {
342+
$this->logger->info('Trying to create already existing account.');
343+
return MailJsonResponse::fail([
344+
'error' => 'ACCOUNT_EXISTS',
345+
]);
346+
}
341347
if (!$this->hostValidator->isValid($imapHost)) {
342348
$this->logger->debug('Prevented access to invalid IMAP host', [
343349
'host' => $imapHost,

src/components/AccountForm.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ export default {
692692
} else if (error.data.service === 'SMTP') {
693693
this.feedback = t('mail', 'SMTP server is not reachable')
694694
}
695+
} else if (error.data?.error === 'ACCOUNT_EXISTS') {
696+
this.feedback = t('mail', 'Account already exists')
695697
} else if (error.data?.error === 'AUTHENTICATION_WRONG_PASSWORD') {
696698
if (error.data.service === 'IMAP') {
697699
this.feedback = t('mail', 'IMAP username or password is wrong')

tests/Unit/Controller/AccountsControllerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,30 @@ public function testCreateManualNotAllowed(): void {
299299
self::assertEquals($expectedResponse, $response);
300300
}
301301

302+
public function testCreateManualNoDuplicate(): void {
303+
$email = 'user@domain.tld';
304+
$accountName = 'Mail';
305+
$imapHost = 'localhost';
306+
$imapPort = 993;
307+
$imapSslMode = 'ssl';
308+
$imapUser = 'user@domain.tld';
309+
$imapPassword = 'mypassword';
310+
$smtpHost = 'localhost';
311+
$smtpPort = 465;
312+
$smtpSslMode = 'none';
313+
$smtpUser = 'user@domain.tld';
314+
$smtpPassword = 'mypassword';
315+
$this->accountService->expects(self::once())
316+
->method('findByUserIdAndAddress')
317+
->willReturn(['existing account']);
318+
$this->setupService->expects(self::never())
319+
->method('createNewAccount');
320+
321+
$expectedResponse = \OCA\Mail\Http\JsonResponse::fail(['error' => 'ACCOUNT_EXISTS']);
322+
$response = $this->controller->create($accountName, $email, $imapHost, $imapPort, $imapSslMode, $imapUser, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $imapPassword, $smtpPassword);
323+
324+
self::assertEquals($expectedResponse, $response);
325+
}
302326

303327
public function testCreateManualFailure(): void {
304328
$email = 'user@domain.tld';

0 commit comments

Comments
 (0)