Completed
Pull Request — master (#6358)
by Morris
16:02
created

Hooks::onChangeEmail()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 67
Code Lines 46

Duplication

Lines 22
Ratio 32.84 %

Importance

Changes 0
Metric Value
cc 7
eloc 46
nc 10
nop 2
dl 22
loc 67
rs 7.0237
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('settings.PasswordChanged', [
121
				'displayname' => $user->getDisplayName(),
122
				'emailAddress' => $user->getEMailAddress(),
123
				'instanceUrl' => $instanceUrl,
124
			]);
125
			$template->addHeader();
126
			$template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false);
127
			$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
128
			$template->addFooter();
129
130
131
			$message = $this->mailer->createMessage();
132
			$message->setTo([$user->getEMailAddress() => $user->getDisplayName()]);
133
			$message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
134
			$message->setBody($template->renderText(), 'text/plain');
135
			$message->setHtmlBody($template->renderHtml());
136
137
			$this->mailer->send($message);
138
		}
139
	}
140
141
	/**
142
	 * @param IUser $user
143
	 * @param string|null $oldMailAddress
144
	 * @throws \InvalidArgumentException
145
	 * @throws \BadMethodCallException
146
	 */
147
	public function onChangeEmail(IUser $user, $oldMailAddress) {
148
149
		if ($oldMailAddress === $user->getEMailAddress() ||
150
			$user->getLastLogin() === 0) {
151
			// Email didn't really change or user didn't login,
152
			// so don't create activities and emails.
153
			return;
154
		}
155
156
		$event = $this->activityManager->generateEvent();
157
		$event->setApp('settings')
158
			->setType('personal_settings')
159
			->setAffectedUser($user->getUID());
160
161
		$instanceUrl = $this->urlGenerator->getAbsoluteURL('/');
162
163
		$actor = $this->userSession->getUser();
164 View Code Duplication
		if ($actor instanceof IUser) {
165
			if ($actor->getUID() !== $user->getUID()) {
166
				$this->l = $this->languageFactory->get(
167
					'settings',
168
					$this->config->getUserValue(
169
						$user->getUID(), 'core', 'lang',
170
						$this->config->getSystemValue('default_language', 'en')
171
					)
172
				);
173
174
				$text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]);
175
				$event->setAuthor($actor->getUID())
176
					->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]);
177
			} else {
178
				$text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]);
179
				$event->setAuthor($actor->getUID())
180
					->setSubject(Provider::EMAIL_CHANGED_SELF);
181
			}
182
		} else {
183
			$text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]);
184
			$event->setSubject(Provider::EMAIL_CHANGED);
185
		}
186
		$this->activityManager->publish($event);
187
188
189
		if ($oldMailAddress !== null) {
190
			$template = $this->mailer->createEMailTemplate('settings.EmailChanged', [
191
				'displayname' => $user->getDisplayName(),
192
				'newEMailAddress' => $user->getEMailAddress(),
193
				'oldEMailAddress' => $oldMailAddress,
194
				'instanceUrl' => $instanceUrl,
195
			]);
196
			$template->addHeader();
197
			$template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false);
198
			$template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.'));
199
			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...
200
				$template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()]));
201
			}
202
			$template->addFooter();
203
204
205
			$message = $this->mailer->createMessage();
206
			$message->setTo([$oldMailAddress => $user->getDisplayName()]);
207
			$message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl]));
208
			$message->setBody($template->renderText(), 'text/plain');
209
			$message->setHtmlBody($template->renderHtml());
210
211
			$this->mailer->send($message);
212
		}
213
	}
214
}
215