Completed
Pull Request — master (#1559)
by Christoph
09:17
created

createOrUpdateDefaultAccount()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 15
cp 0
rs 8.8571
cc 5
eloc 14
nc 5
nop 0
crap 30
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * ownCloud - Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Mail\Service\DefaultAccount;
23
24
use OCA\Mail\Db\MailAccount;
25
use OCA\Mail\Db\MailAccountMapper;
26
use OCA\Mail\Service\Logger;
27
use OCP\IConfig;
28
use OCP\ISession;
29
use OCP\IUser;
30
use OCP\IUserSession;
31
use OCP\Security\ICrypto;
32
33
class DefaultAccountManager {
34
35
	/** @var IConfig */
36
	private $config;
37
38
	/** @var ISession */
39
	private $session;
40
41
	/** @var Logger */
42
	private $logger;
43
44
	/** @var MailAccountMapper */
45
	private $mapper;
46
47
	/** @var IUserSession */
48
	private $userSession;
49
50
	/** @var ICrypto */
51
	private $crypto;
52
53
	/**
54
	 * @param IConfig $config
55
	 * @param ISession $session
56
	 * @param Logger $logger
57
	 * @param MailAccountMapper $mapper
58
	 * @param IUserSession $userSession
59
	 * @param ICrypto $crypto
60
	 */
61
	public function __construct(IConfig $config, ISession $session, Logger $logger,
62
		MailAccountMapper $mapper, IUserSession $userSession, ICrypto $crypto) {
63
		$this->config = $config;
64
		$this->session = $session;
65
		$this->logger = $logger;
66
		$this->mapper = $mapper;
67
		$this->userSession = $userSession;
68
		$this->crypto = $crypto;
69
	}
70
71
	/**
72
	 * @return Config|null
73
	 */
74
	private function getConfig() {
75
		$config = $this->config->getSystemValue('app.mail.accounts.default', null);
76
		if (is_null($config)) {
77
			$this->logger->debug('no default config found');
78
			return null;
79
		} else {
80
			$this->logger->debug('default config to create a default account found');
81
			// TODO: check if config is complete
82
			return new Config($config);
83
		}
84
	}
85
86
	/**
87
	 * Save the login password to the session to create a default account in a
88
	 * sub-sequent request
89
	 *
90
	 * @param string $password
91
	 */
92
	public function saveLoginPassword($password) {
93
		$this->session->set('mail_default_account_password', $password);
94
	}
95
96
	public function createOrUpdateDefaultAccount() {
97
		$config = $this->getConfig();
98
		if (is_null($config)) {
99
			return;
100
		}
101
102
		$user = $this->userSession->getUser();
103
		if (is_null($user)) {
104
			return;
105
		}
106
107
		$accounts = $this->mapper->findByEmail($config->buildEmail($user), $user->getUID());
108
		if (empty($accounts)) {
109
			$this->createDefaultAccount($user, $config);
110
		} else if (count($accounts) === 1) {
111
			$this->updateDefaultAccount($accounts[0], $user, $config);
0 ignored issues
show
Unused Code introduced by
The call to DefaultAccountManager::updateDefaultAccount() has too many arguments starting with $user.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
112
		} else {
113
			$this->logger->debug('unexpected state: more than one account with the default email found for user ' . $user->getUID());
114
		}
115
	}
116
117
	private function createDefaultAccount(IUser $user, Config $config) {
118
		if (!$this->session->exists('mail_default_account_password')) {
119
			return;
120
		}
121
		$this->logger->info('creating default account for user ' . $user->getUID());
122
		$password = $this->crypto->encrypt($this->session->get('mail_default_account_password'));
123
124
		$account = new MailAccount();
125
		$account->setUserId($user->getUID());
126
		$account->setEmail($config->buildEmail($user));
127
		$account->setName($user->getDisplayName());
128
129
		$account->setInboundUser($config->buildImapUser($user));
130
		$account->setInboundHost($config->getImapHost());
131
		$account->setInboundPort($config->getImapPort());
132
		$account->setInboundSslMode($config->getImapSslMode());
133
		$account->setInboundPassword($password);
134
135
		$account->setOutboundUser($config->buildSmtpUser($user));
136
		$account->setOutboundHost($config->getSmtpHost());
137
		$account->setOutboundPort($config->getSmtpPort());
138
		$account->setOutboundSslMode($config->getSmtpSslMode());
139
		$account->setOutboundPassword($password);
140
141
		$this->mapper->insert($account);
142
	}
143
144
	/**
145
	 * @todo update imap/smtp config if it changed
146
	 *
147
	 * @param MailAccount $account
148
	 * @param IUser $user
0 ignored issues
show
Bug introduced by
There is no parameter named $user. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
149
	 * @param Config $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
150
	 */
151
	private function updateDefaultAccount(MailAccount $account) {
152
		$password = $this->crypto->encrypt($this->session->get('mail_default_account_password'));
153
		$needsUpdate = false;
154
155
		if ($account->getInboundPassword() !== $password) {
156
			$account->setInboundPassword($password);
157
			$needsUpdate = true;
158
		}
159
		if ($account->getOutboundPassword() !== $password) {
160
			$account->setOutboundPassword($password);
161
			$needsUpdate = true;
162
		}
163
164
		if ($needsUpdate) {
165
			$this->mapper->update($account);
166
		}
167
	}
168
169
}
170