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>2016-09-02 18:02:47 +0300
committerChristoph Wurst <christoph@winzerhof-wurst.at>2017-02-22 23:25:28 +0300
commit4a8114709092a27b7e511bdb2f3d2903ab3df201 (patch)
treeb247bbdeec98bc762a1e916c05f8bf6f22f25b66
parent03573e2bb00995372210fda9ddbfef578f2a6a2b (diff)
create a dynamic default account
-rw-r--r--lib/AppInfo/Application.php25
-rw-r--r--lib/Db/MailAccountMapper.php10
-rw-r--r--lib/Service/AccountService.php33
-rw-r--r--lib/service/defaultaccount/config.php124
-rw-r--r--lib/service/defaultaccount/defaultaccountmanager.php125
5 files changed, 311 insertions, 6 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php
index 637f59cd9..0b56e37ba 100644
--- a/lib/AppInfo/Application.php
+++ b/lib/AppInfo/Application.php
@@ -22,6 +22,7 @@
namespace OCA\Mail\AppInfo;
+use OC;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Service\MailManager;
use OCP\AppFramework\App;
@@ -32,6 +33,11 @@ class Application extends App {
public function __construct(array $urlParams = []) {
parent::__construct('mail', $urlParams);
+ $this->initializeAppContainer();
+ $this->registerHooks();
+ }
+
+ public function initializeAppContainer() {
$container = $this->getContainer();
$transport = $container->getServer()->getConfig()->getSystemValue('app.mail.transport', 'smtp');
@@ -48,8 +54,25 @@ class Application extends App {
return $container->getServer()->getUserFolder($user);
});
$container->registerParameter("testSmtp", $testSmtp);
- $container->registerParameter("referrer", isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null);
+ $container->registerParameter("referrer", isset($_SERVER['HTTP_REFERER']) ? : null);
$container->registerParameter("hostname", Util::getServerHostName());
}
+ public function registerHooks() {
+ Util::connectHook('OC_User', 'post_login', $this, 'handleLoginHook');
+ }
+
+ public function handleLoginHook($params) {
+ if (!isset($params['password'])) {
+ return;
+ }
+ $password = $params['password'];
+
+ $container = $this->getContainer();
+ /* @var $defaultAccountManager \OCA\Mail\Service\DefaultAccount\DefaultAccountManager */
+ $defaultAccountManager = $container->query('\OCA\Mail\Service\DefaultAccount\DefaultAccountManager');
+
+ $defaultAccountManager->saveLoginPassword($password);
+ }
+
}
diff --git a/lib/Db/MailAccountMapper.php b/lib/Db/MailAccountMapper.php
index 379b09d32..f154db140 100644
--- a/lib/Db/MailAccountMapper.php
+++ b/lib/Db/MailAccountMapper.php
@@ -62,6 +62,16 @@ class MailAccountMapper extends Mapper {
return $this->findEntities($sql, $params);
}
+ public function findByEmail($email, $userId) {
+ $sql = 'SELECT * FROM ' . $this->getTableName() . ' WHERE user_id = ? and email = ?';
+ $params = [
+ $userId,
+ $email,
+ ];
+
+ return $this->findEntities($sql, $params);
+ }
+
/**
* Saves an User Account into the database
* @param MailAccount $account
diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php
index e2900b63e..b99090a01 100644
--- a/lib/Service/AccountService.php
+++ b/lib/Service/AccountService.php
@@ -19,6 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
+
namespace OCA\Mail\Service;
use Exception;
@@ -26,6 +27,7 @@ use OCA\Mail\Account;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Exception\ServiceException;
+use OCA\Mail\Service\DefaultAccount\DefaultAccountManager;
use OCP\IL10N;
class AccountService {
@@ -43,12 +45,17 @@ class AccountService {
/** @var IL10N */
private $l10n;
+ /** @var DefaultAccountManager */
+ private $defaultAccountManager;
+
/**
* @param MailAccountMapper $mapper
*/
- public function __construct(MailAccountMapper $mapper, IL10N $l10n) {
+ public function __construct(MailAccountMapper $mapper, IL10N $l10n,
+ DefaultAccountManager $defaultAccountManager) {
$this->mapper = $mapper;
$this->l10n = $l10n;
+ $this->defaultAccountManager = $defaultAccountManager;
}
/**
@@ -57,10 +64,15 @@ class AccountService {
*/
public function findByUserId($currentUserId) {
if ($this->accounts === null) {
- $accounts = $this->mapper->findByUserId($currentUserId);
$accounts = array_map(function($a) {
return new Account($a);
- }, $accounts);
+ }, $this->mapper->findByUserId($currentUserId));
+
+ $defaultAccount = $this->defaultAccountManager->getDefaultAccount();
+ if (!is_null($defaultAccount)) {
+ $accounts[] = new Account($defaultAccount);
+ }
+
$this->accounts = $accounts;
}
@@ -82,9 +94,16 @@ class AccountService {
throw new Exception("Invalid account id <$accountId>");
}
- if ((int)$accountId === UnifiedAccount::ID) {
+ if ((int) $accountId === UnifiedAccount::ID) {
return $this->buildUnifiedAccount($currentUserId);
}
+ if ((int) $accountId === DefaultAccountManager::ACCOUNT_ID) {
+ $defaultAccount = $this->defaultAccountManager->getDefaultAccount();
+ if (is_null($defaultAccount)) {
+ throw new Exception('Default account config missing');
+ }
+ return new Account($defaultAccount);
+ }
return new Account($this->mapper->find($currentUserId, $accountId));
}
@@ -109,7 +128,10 @@ class AccountService {
* @param int $accountId
*/
public function delete($currentUserId, $accountId) {
- if ((int)$accountId === UnifiedAccount::ID) {
+ if ((int) $accountId === UnifiedAccount::ID) {
+ return;
+ }
+ if ((int) $accountId === DefaultAccountManager::ACCOUNT_ID) {
return;
}
$mailAccount = $this->mapper->find($currentUserId, $accountId);
@@ -127,4 +149,5 @@ class AccountService {
private function buildUnifiedAccount($userId) {
return new UnifiedAccount($this, $userId, $this->l10n);
}
+
}
diff --git a/lib/service/defaultaccount/config.php b/lib/service/defaultaccount/config.php
new file mode 100644
index 000000000..b50c6dcd1
--- /dev/null
+++ b/lib/service/defaultaccount/config.php
@@ -0,0 +1,124 @@
+<?php
+
+/**
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * Mail
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Mail\Service\DefaultAccount;
+
+use OCP\IUser;
+
+class Config {
+
+ private $data;
+
+ /**
+ * @param array $data
+ */
+ public function __construct($data) {
+ $this->data = $data;
+ }
+
+ /**
+ * @param IUser $user
+ * @return string
+ */
+ public function buildEmail(IUser $user) {
+ return $this->buildUserEmail($this->data['email'], $user);
+ }
+
+ /**
+ * @return string
+ */
+ public function getImapHost() {
+ return $this->data['imapHost'];
+ }
+
+ /**
+ * @return string|int
+ */
+ public function getImapPort() {
+ return $this->data['imapPort'];
+ }
+
+ /**
+ * @return string
+ */
+ public function buildImapUser(IUser $user) {
+ if (isset($this->data['imapUser'])) {
+ return $this->buildUserEmail($this->data['imapUser'], $user);
+ }
+ return $this->buildEmail($user);
+ }
+
+ /**
+ * @return string
+ */
+ public function getImapSslMode() {
+ return $this->data['imapSslMode'];
+ }
+
+ /**
+ * @return string
+ */
+ public function getSmtpHost() {
+ return $this->data['smtpHost'];
+ }
+
+ /**
+ * @return string|int
+ */
+ public function getSmtpPort() {
+ return $this->data['smtpPort'];
+ }
+
+ /**
+ * @param IUser $user
+ * @return string
+ */
+ public function buildSmtpUser(IUser $user) {
+ if (isset($this->data['smtpUser'])) {
+ return $this->buildUserEmail($this->data['smtpUser'], $user);
+ }
+ return $this->buildEmail($user);
+ }
+
+ /**
+ * @return string
+ */
+ public function getSmtpSslMode() {
+ return $this->data['smtpSslMode'];
+ }
+
+ /**
+ * Replace %USERID% and %EMAIL% to allow special configurations
+ *
+ * @param string $original
+ * @param IUser $user
+ * @return string
+ */
+ private function buildUserEmail($original, IUser $user) {
+ $original = str_replace('%USERID%', $user->getUID(), $original);
+ if (!is_null($user->getEMailAddress())) {
+ $original = str_replace('%EMAIL%', $user->getEMailAddress(), $original);
+ }
+
+ return $original;
+ }
+
+}
diff --git a/lib/service/defaultaccount/defaultaccountmanager.php b/lib/service/defaultaccount/defaultaccountmanager.php
new file mode 100644
index 000000000..0ae17dee6
--- /dev/null
+++ b/lib/service/defaultaccount/defaultaccountmanager.php
@@ -0,0 +1,125 @@
+<?php
+
+/**
+ * @author Christoph Wurst <christoph@winzerhof-wurst.at>
+ *
+ * Mail
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Mail\Service\DefaultAccount;
+
+use OCA\Mail\Db\MailAccount;
+use OCA\Mail\Service\Logger;
+use OCP\IConfig;
+use OCP\ISession;
+use OCP\IUserSession;
+use OCP\Security\ICrypto;
+
+class DefaultAccountManager {
+
+ const ACCOUNT_ID = -2;
+
+ /** @var IConfig */
+ private $config;
+
+ /** @var ISession */
+ private $session;
+
+ /** @var Logger */
+ private $logger;
+
+ /** @var IUserSession */
+ private $userSession;
+
+ /** @var ICrypto */
+ private $crypto;
+
+ /**
+ * @param IConfig $config
+ * @param ISession $session
+ * @param Logger $logger
+ * @param IUserSession $userSession
+ * @param ICrypto $crypto
+ */
+ public function __construct(IConfig $config, ISession $session, Logger $logger,
+ IUserSession $userSession, ICrypto $crypto) {
+ $this->config = $config;
+ $this->session = $session;
+ $this->logger = $logger;
+ $this->userSession = $userSession;
+ $this->crypto = $crypto;
+ }
+
+ /**
+ * @return Config|null
+ */
+ private function getConfig() {
+ $config = $this->config->getSystemValue('app.mail.accounts.default', null);
+ if (is_null($config)) {
+ $this->logger->debug('no default config found');
+ return null;
+ } else {
+ $this->logger->debug('default config to create a default account found');
+ // TODO: check if config is complete
+ return new Config($config);
+ }
+ }
+
+ /**
+ * Save the login password to the session to create a default account in a
+ * sub-sequent request
+ *
+ * @param string $password
+ */
+ public function saveLoginPassword($password) {
+ $this->session->set('mail_default_account_password', $password);
+ }
+
+ /**
+ * @return MailAccount|null
+ */
+ public function getDefaultAccount() {
+ $config = $this->getConfig();
+ if (is_null($config)) {
+ return null;
+ }
+
+ $user = $this->userSession->getUser();
+ $this->logger->info('building default account for user ' . $user->getUID());
+ $password = $this->crypto->encrypt($this->session->get('mail_default_account_password'));
+
+ $account = new MailAccount();
+ $account->setId(self::ACCOUNT_ID);
+ $account->setUserId($user->getUID());
+ $account->setEmail($config->buildEmail($user));
+ $account->setName($user->getDisplayName());
+
+ $account->setInboundUser($config->buildImapUser($user));
+ $account->setInboundHost($config->getImapHost());
+ $account->setInboundPort($config->getImapPort());
+ $account->setInboundSslMode($config->getImapSslMode());
+ $account->setInboundPassword($password);
+
+ $account->setOutboundUser($config->buildSmtpUser($user));
+ $account->setOutboundHost($config->getSmtpHost());
+ $account->setOutboundPort($config->getSmtpPort());
+ $account->setOutboundSslMode($config->getSmtpSslMode());
+ $account->setOutboundPassword($password);
+
+ return $account;
+ }
+
+}