Completed
Push — master ( f0158e...2060ff )
by Blizzz
50:16 queued 36:57
created

Hooks::onChangeEmail()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 68
Code Lines 47

Duplication

Lines 22
Ratio 32.35 %

Importance

Changes 0
Metric Value
cc 7
eloc 47
nc 10
nop 2
dl 22
loc 68
rs 6.9654
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Settings;
23
24
use OC\Settings\Activity\Provider;
25
use OCP\Activity\IManager as IActivityManager;
26
use OCP\IConfig;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\IUser;
30
use OCP\IUserManager;
31
use OCP\IUserSession;
32
use OCP\L10N\IFactory;
33
use OCP\Mail\IMailer;
34
35
class Hooks {
36
37
	/** @var IActivityManager */
38
	protected $activityManager;
39
	/** @var IUserManager */
40
	protected $userManager;
41
	/** @var IUserSession */
42
	protected $userSession;
43
	/** @var IURLGenerator */
44
	protected $urlGenerator;
45
	/** @var IMailer */
46
	protected $mailer;
47
	/** @var IConfig */
48
	protected $config;
49
	/** @var IFactory */
50
	protected $languageFactory;
51
	/** @var IL10N */
52
	protected $l;
53
54 View Code Duplication
	public function __construct(IActivityManager $activityManager,
55
								IUserManager $userManager,
56
								IUserSession $userSession,
57
								IURLGenerator $urlGenerator,
58
								IMailer $mailer,
59
								IConfig $config,
60
								IFactory $languageFactory,
61
								IL10N $l) {
62
		$this->activityManager = $activityManager;
63
		$this->userManager = $userManager;
64
		$this->userSession = $userSession;
65
		$this->urlGenerator = $urlGenerator;
66
		$this->mailer = $mailer;
67
		$this->config = $config;
68
		$this->languageFactory = $languageFactory;
69
		$this->l = $l;
70
	}
71
72
	/**
73
	 * @param string $uid
74
	 * @throws \InvalidArgumentException
75
	 * @throws \BadMethodCallException
76
	 * @throws \Exception
77
	 */
78
	public function onChangePassword($uid) {
79
		$user = $this->userManager->get($uid);
80
81
		if (!$user instanceof IUser || $user->getLastLogin() === 0) {
82
			// User didn't login, so don't create activities and emails.
83
			return;
84
		}
85
86
		$event = $this->activityManager->generateEvent();
87
		$event->setApp('settings')
88
			->setType('personal_settings')
89
			->setAffectedUser($user->getUID());
90
91
		$instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
92
93
		$actor = $this->userSession->getUser();
94 View Code Duplication
		if ($actor instanceof IUser) {
95
			if ($actor->getUID() !== $user->getUID()) {
96
				$this->l = $this->languageFactory->get(
97
					'settings',
98
					$this->config->getUserValue(
99
						$user->getUID(), 'core', 'lang',
100
						$this->config->getSystemValue('default_language', 'en')
101
					)
102
				);
103
104
				$text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
105
				$event->setAuthor($actor->getUID())
106
					->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]);
107
			} else {
108
				$text = $this->l->t('Your password on %s was changed.', [$instanceUrl]);
109
				$event->setAuthor($actor->getUID())
110
					->setSubject(Provider::PASSWORD_CHANGED_SELF);
111
			}
112
		} else {
113
			$text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]);
114
			$event->setSubject(Provider::PASSWORD_RESET);
115
		}
116
117
		$this->activityManager->publish($event);
118
119
		if ($user->getEMailAddress() !== null) {
120
			$template = $this->mailer->createEMailTemplate();
121
			$template->setMetaData('settings.PasswordChanged', [
122
				'displayname' => $user->getDisplayName(),
123
				'emailAddress' => $user->getEMailAddress(),
124
				'instanceUrl' => $instanceUrl,
125
			]);
126
			$template->addHeader();
127
			$template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
128
			$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
129
			$template->addFooter();
130
131
132
			$message = $this->mailer->createMessage();
133
			$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
134
			$message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
135
			$message->setBody($template->renderText(), 'text/plain');
136
			$message->setHtmlBody($template->renderHtml());
137
138
			$this->mailer->send($message);
139
		}
140
	}
141
142
	/**
143
	 * @param IUser $user
144
	 * @param string|null $oldMailAddress
145
	 * @throws \InvalidArgumentException
146
	 * @throws \BadMethodCallException
147
	 */
148
	public function onChangeEmail(IUser $user, $oldMailAddress) {
149
150
		if ($oldMailAddress === $user->getEMailAddress() ||
151
			$user->getLastLogin() === 0) {
152
			// Email didn't really change or user didn't login,
153
			// so don't create activities and emails.
154
			return;
155
		}
156
157
		$event = $this->activityManager->generateEvent();
158
		$event->setApp('settings')
159
			->setType('personal_settings')
160
			->setAffectedUser($user->getUID());
161
162
		$instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
163
164
		$actor = $this->userSession->getUser();
165 View Code Duplication
		if ($actor instanceof IUser) {
166
			if ($actor->getUID() !== $user->getUID()) {
167
				$this->l = $this->languageFactory->get(
168
					'settings',
169
					$this->config->getUserValue(
170
						$user->getUID(), 'core', 'lang',
171
						$this->config->getSystemValue('default_language', 'en')
172
					)
173
				);
174
175
				$text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
176
				$event->setAuthor($actor->getUID())
177
					->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]);
178
			} else {
179
				$text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
180
				$event->setAuthor($actor->getUID())
181
					->setSubject(Provider::EMAIL_CHANGED_SELF);
182
			}
183
		} else {
184
			$text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
185
			$event->setSubject(Provider::EMAIL_CHANGED);
186
		}
187
		$this->activityManager->publish($event);
188
189
190
		if ($oldMailAddress !== null) {
191
			$template = $this->mailer->createEMailTemplate();
192
			$template->setMetaData('settings.EmailChanged', [
193
				'displayname' => $user->getDisplayName(),
194
				'newEMailAddress' => $user->getEMailAddress(),
195
				'oldEMailAddress' => $oldMailAddress,
196
				'instanceUrl' => $instanceUrl,
197
			]);
198
			$template->addHeader();
199
			$template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
200
			$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
201
			if ($user->getEMailAddress()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->getEMailAddress() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
202
				$template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
203
			}
204
			$template->addFooter();
205
206
207
			$message = $this->mailer->createMessage();
208
			$message->setTo([$oldMailAddress => $user->getDisplayName()]);
209
			$message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
210
			$message->setBody($template->renderText(), 'text/plain');
211
			$message->setHtmlBody($template->renderHtml());
212
213
			$this->mailer->send($message);
214
		}
215
	}
216
}
217