Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2022-07-04 21:29:53 +0300
committerChristoph Wurst <christoph@winzerhof-wurst.at>2022-07-07 16:24:05 +0300
commit799ee0028d553d46af475bc0499cb1db44864e61 (patch)
tree007f17e8bbd35a822709a658695210891b3353b2 /lib/Controller
parent77544d3fe16dec880092e143d2b65eeafdc9fbc6 (diff)
Split auto config and account creation
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib/Controller')
-rw-r--r--lib/Controller/AccountsController.php62
-rw-r--r--lib/Controller/AutoConfigController.php89
2 files changed, 110 insertions, 41 deletions
diff --git a/lib/Controller/AccountsController.php b/lib/Controller/AccountsController.php
index 745fcb6b3..3a20d7d81 100644
--- a/lib/Controller/AccountsController.php
+++ b/lib/Controller/AccountsController.php
@@ -29,7 +29,6 @@ declare(strict_types=1);
namespace OCA\Mail\Controller;
-use Exception;
use Horde_Imap_Client;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailTransmission;
@@ -141,7 +140,6 @@ class AccountsController extends Controller {
* @param int $id
* @param string $accountName
* @param string $emailAddress
- * @param string $password
* @param string $imapHost
* @param int $imapPort
* @param string $imapSslMode
@@ -152,16 +150,13 @@ class AccountsController extends Controller {
* @param string $smtpSslMode
* @param string $smtpUser
* @param string $smtpPassword
- * @param bool $autoDetect
*
* @return JSONResponse
* @throws ClientException
*/
public function update(int $id,
- bool $autoDetect,
string $accountName,
string $emailAddress,
- string $password = null,
string $imapHost = null,
int $imapPort = null,
string $imapSslMode = null,
@@ -179,28 +174,26 @@ class AccountsController extends Controller {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
- $account = null;
- $errorMessage = null;
try {
- if ($autoDetect) {
- $account = $this->setup->createNewAutoConfiguredAccount($accountName, $emailAddress, $password);
- } else {
- $account = $this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId, $id);
- }
- } catch (Exception $ex) {
- $errorMessage = $ex->getMessage();
- }
+ return \OCA\Mail\Http\JsonResponse::success(
+ $this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId, $id)
+ );
+ } catch (CouldNotConnectException $e) {
+ $data = [
+ 'error' => $e->getReason(),
+ 'service' => $e->getService(),
+ 'host' => $e->getHost(),
+ 'port' => $e->getPort(),
+ ];
- if (is_null($account)) {
- if ($autoDetect) {
- throw new ClientException($this->l10n->t('Auto detect failed. Please use manual mode.'));
- } else {
- $this->logger->error('Updating account failed: ' . $errorMessage);
- throw new ClientException($this->l10n->t('Updating account failed: ') . $errorMessage);
- }
+ $this->logger->info('Creating account failed: ' . $e->getMessage(), $data);
+ return \OCA\Mail\Http\JsonResponse::fail($data);
+ } catch (ServiceException $e) {
+ $this->logger->error('Creating account failed: ' . $e->getMessage(), [
+ 'exception' => $e,
+ ]);
+ return \OCA\Mail\Http\JsonResponse::error('Could not create account');
}
-
- return new JSONResponse($account);
}
/**
@@ -299,7 +292,6 @@ class AccountsController extends Controller {
*
* @param string $accountName
* @param string $emailAddress
- * @param string $password
* @param string $imapHost
* @param int $imapPort
* @param string $imapSslMode
@@ -310,17 +302,14 @@ class AccountsController extends Controller {
* @param string $smtpSslMode
* @param string $smtpUser
* @param string $smtpPassword
- * @param bool $autoDetect
*
* @return JSONResponse
*/
- public function create(string $accountName, string $emailAddress, string $password = null, string $imapHost = null, int $imapPort = null, string $imapSslMode = null, string $imapUser = null, string $imapPassword = null, string $smtpHost = null, int $smtpPort = null, string $smtpSslMode = null, string $smtpUser = null, string $smtpPassword = null, bool $autoDetect = true): JSONResponse {
+ public function create(string $accountName, string $emailAddress, string $imapHost = null, int $imapPort = null, string $imapSslMode = null, string $imapUser = null, string $imapPassword = null, string $smtpHost = null, int $smtpPort = null, string $smtpSslMode = null, string $smtpUser = null, string $smtpPassword = null): JSONResponse {
try {
- if ($autoDetect) {
- $account = $this->setup->createNewAutoConfiguredAccount($accountName, $emailAddress, $password);
- } else {
- $account = $this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId);
- }
+ return \OCA\Mail\Http\JsonResponse::success(
+ $this->setup->createNewAccount($accountName, $emailAddress, $imapHost, $imapPort, $imapSslMode, $imapUser, $imapPassword, $smtpHost, $smtpPort, $smtpSslMode, $smtpUser, $smtpPassword, $this->currentUserId), Http::STATUS_CREATED
+ );
} catch (CouldNotConnectException $e) {
$data = [
'error' => $e->getReason(),
@@ -337,15 +326,6 @@ class AccountsController extends Controller {
]);
return \OCA\Mail\Http\JsonResponse::error('Could not create account');
}
-
- if (is_null($account)) {
- return \OCA\Mail\Http\JsonResponse::fail([
- 'error' => 'AUTOCONFIG_FAILED',
- 'message' => $this->l10n->t('Auto detect failed. Please use manual mode.'),
- ]);
- }
-
- return \OCA\Mail\Http\JsonResponse::success($account, Http::STATUS_CREATED);
}
/**
diff --git a/lib/Controller/AutoConfigController.php b/lib/Controller/AutoConfigController.php
new file mode 100644
index 000000000..796f7a42a
--- /dev/null
+++ b/lib/Controller/AutoConfigController.php
@@ -0,0 +1,89 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace OCA\Mail\Controller;
+
+use Horde_Mail_Rfc822_Address;
+use OCA\Mail\AppInfo\Application;
+use OCA\Mail\Http\JsonResponse;
+use OCA\Mail\Service\AutoConfig\ConnectivityTester;
+use OCA\Mail\Service\AutoConfig\IspDb;
+use OCA\Mail\Service\AutoConfig\MxRecord;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\IRequest;
+use function in_array;
+
+class AutoConfigController extends Controller {
+ private IspDb $ispDb;
+ private MxRecord $mxRecord;
+ private ConnectivityTester $connectivityTester;
+
+ public function __construct(IRequest $request,
+ IspDb $ispDb,
+ MxRecord $mxRecord,
+ ConnectivityTester $connectivityTester) {
+ parent::__construct(Application::APP_ID, $request);
+ $this->ispDb = $ispDb;
+ $this->mxRecord = $mxRecord;
+ $this->connectivityTester = $connectivityTester;
+ }
+
+ /**
+ * @param string $email
+ *
+ * @NoAdminRequired
+ * @TrapError
+ *
+ * @return JsonResponse
+ */
+ public function queryIspdb(string $email): JsonResponse {
+ $rfc822Address = new Horde_Mail_Rfc822_Address($email);
+ if (!$rfc822Address->valid) {
+ return JsonResponse::fail('Invalid email address', Http::STATUS_UNPROCESSABLE_ENTITY);
+ }
+ $config = $this->ispDb->query($rfc822Address->host, $rfc822Address);
+ return JsonResponse::success($config);
+ }
+
+ public function queryMx(string $email): JsonResponse {
+ $rfc822Address = new Horde_Mail_Rfc822_Address($email);
+ if (!$rfc822Address->valid) {
+ return JsonResponse::fail('Invalid email address', Http::STATUS_UNPROCESSABLE_ENTITY);
+ }
+ return JsonResponse::success(
+ $this->mxRecord->query($rfc822Address->host),
+ );
+ }
+
+ public function testConnectivity(string $host, int $port): JsonResponse {
+ if (!in_array($port, [143, 993, 465, 587])) {
+ return JsonResponse::fail('Port not allowed');
+ }
+ return JsonResponse::success(
+ $this->connectivityTester->canConnect($host, $port),
+ );
+ }
+}