Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions lib/Command/CreateAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

if (empty($this->accountService->findByUserIdAndAddress($userId, $email)) === false) {
$output->writeln("<error>User $userId already has an account for $email</error>");
return 1;
}

$account = new MailAccount();
$account->setUserId($userId);
$account->setName($name);
Expand Down
6 changes: 6 additions & 0 deletions lib/Controller/AccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ public function create(string $accountName,
$this->logger->info('Creating account disabled by admin.');
return MailJsonResponse::error('Could not create account');
}
if (empty($this->accountService->findByUserIdAndAddress($this->userId, $emailAddress)) === false) {
$this->logger->info('Trying to create already existing account.');
return MailJsonResponse::fail([
'error' => 'ACCOUNT_EXISTS',
]);
}
if (!$this->hostValidator->isValid($imapHost)) {
$this->logger->debug('Prevented access to invalid IMAP host', [
'host' => $imapHost,
Expand Down
2 changes: 2 additions & 0 deletions src/components/AccountForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ export default {
} else if (error.data.service === 'SMTP') {
this.feedback = t('mail', 'SMTP server is not reachable')
}
} else if (error.data?.error === 'ACCOUNT_EXISTS') {
this.feedback = t('mail', 'Account already exists')
} else if (error.data?.error === 'AUTHENTICATION_WRONG_PASSWORD') {
if (error.data.service === 'IMAP') {
this.feedback = t('mail', 'IMAP username or password is wrong')
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Controller/AccountsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,30 @@ public function testCreateManualNotAllowed(): void {
self::assertEquals($expectedResponse, $response);
}

public function testCreateManualNoDuplicate(): void {
$email = 'user@domain.tld';
$accountName = 'Mail';
$imapHost = 'localhost';
$imapPort = 993;
$imapSslMode = 'ssl';
$imapUser = 'user@domain.tld';
$imapPassword = 'mypassword';
$smtpHost = 'localhost';
$smtpPort = 465;
$smtpSslMode = 'none';
$smtpUser = 'user@domain.tld';
$smtpPassword = 'mypassword';
$this->accountService->expects(self::once())
->method('findByUserIdAndAddress')
->willReturn(['existing account']);
$this->setupService->expects(self::never())
->method('createNewAccount');

$expectedResponse = \OCA\Mail\Http\JsonResponse::fail(['error' => 'ACCOUNT_EXISTS']);
$response = $this->controller->create($accountName, $email, $imapHost, $imapPort, $imapSslMode, $imapUser, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $imapPassword, $smtpPassword);

self::assertEquals($expectedResponse, $response);
}

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