Passed
Push — master ( 7d7f7a...71065f )
by Joas
16:42 queued 13s
created

Provider::getDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Daniel Kesselberg <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Thomas Citharel <[email protected]>
12
 *
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
namespace OCA\Settings\Activity;
30
31
use OCP\Activity\IEvent;
32
use OCP\Activity\IManager;
33
use OCP\Activity\IProvider;
34
use OCP\IL10N;
35
use OCP\IURLGenerator;
36
use OCP\IUser;
37
use OCP\IUserManager;
38
use OCP\L10N\IFactory;
39
40
class Provider implements IProvider {
41
	public const PASSWORD_CHANGED_BY = 'password_changed_by';
42
	public const PASSWORD_CHANGED_SELF = 'password_changed_self';
43
	public const PASSWORD_RESET = 'password_changed';
44
	public const PASSWORD_RESET_SELF = 'password_reset_self';
45
	public const EMAIL_CHANGED_BY = 'email_changed_by';
46
	public const EMAIL_CHANGED_SELF = 'email_changed_self';
47
	public const EMAIL_CHANGED = 'email_changed';
48
	public const APP_TOKEN_CREATED = 'app_token_created';
49
	public const APP_TOKEN_DELETED = 'app_token_deleted';
50
	public const APP_TOKEN_RENAMED = 'app_token_renamed';
51
	public const APP_TOKEN_FILESYSTEM_GRANTED = 'app_token_filesystem_granted';
52
	public const APP_TOKEN_FILESYSTEM_REVOKED = 'app_token_filesystem_revoked';
53
54
	/** @var IFactory */
55
	protected $languageFactory;
56
57
	/** @var IL10N */
58
	protected $l;
59
60
	/** @var IURLGenerator */
61
	protected $url;
62
63
	/** @var IUserManager */
64
	protected $userManager;
65
66
	/** @var IManager */
67
	private $activityManager;
68
69
	public function __construct(IFactory $languageFactory,
70
								IURLGenerator $url,
71
								IUserManager $userManager,
72
								IManager $activityManager) {
73
		$this->languageFactory = $languageFactory;
74
		$this->url = $url;
75
		$this->userManager = $userManager;
76
		$this->activityManager = $activityManager;
77
	}
78
79
	/**
80
	 * @param string $language
81
	 * @param IEvent $event
82
	 * @param IEvent|null $previousEvent
83
	 * @return IEvent
84
	 * @throws \InvalidArgumentException
85
	 * @since 11.0.0
86
	 */
87
	public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
88
		if ($event->getApp() !== 'settings') {
89
			throw new \InvalidArgumentException('Unknown app');
90
		}
91
92
		$this->l = $this->languageFactory->get('settings', $language);
93
94
		if ($this->activityManager->getRequirePNG()) {
95
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png')));
96
		} else {
97
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg')));
98
		}
99
100
		if ($event->getSubject() === self::PASSWORD_CHANGED_BY) {
101
			$subject = $this->l->t('{actor} changed your password');
102
		} elseif ($event->getSubject() === self::PASSWORD_CHANGED_SELF) {
103
			$subject = $this->l->t('You changed your password');
104
		} elseif ($event->getSubject() === self::PASSWORD_RESET) {
105
			$subject = $this->l->t('Your password was reset by an administrator');
106
		} elseif ($event->getSubject() === self::PASSWORD_RESET_SELF) {
107
			$subject = $this->l->t('Your password was reset');
108
		} elseif ($event->getSubject() === self::EMAIL_CHANGED_BY) {
109
			$subject = $this->l->t('{actor} changed your email address');
110
		} elseif ($event->getSubject() === self::EMAIL_CHANGED_SELF) {
111
			$subject = $this->l->t('You changed your email address');
112
		} elseif ($event->getSubject() === self::EMAIL_CHANGED) {
113
			$subject = $this->l->t('Your email address was changed by an administrator');
114
		} elseif ($event->getSubject() === self::APP_TOKEN_CREATED) {
115
			if ($event->getAffectedUser() === $event->getAuthor()) {
116
				$subject = $this->l->t('You created app password "{token}"');
117
			} else {
118
				$subject = $this->l->t('An administrator created app password "{token}"');
119
			}
120
		} elseif ($event->getSubject() === self::APP_TOKEN_DELETED) {
121
			$subject = $this->l->t('You deleted app password "{token}"');
122
		} elseif ($event->getSubject() === self::APP_TOKEN_RENAMED) {
123
			$subject = $this->l->t('You renamed app password "{token}" to "{newToken}"');
124
		} elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_GRANTED) {
125
			$subject = $this->l->t('You granted filesystem access to app password "{token}"');
126
		} elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_REVOKED) {
127
			$subject = $this->l->t('You revoked filesystem access from app password "{token}"');
128
		} else {
129
			throw new \InvalidArgumentException('Unknown subject');
130
		}
131
132
		$parsedParameters = $this->getParameters($event);
133
		$this->setSubjects($event, $subject, $parsedParameters);
134
135
		return $event;
136
	}
137
138
	/**
139
	 * @param IEvent $event
140
	 * @return array
141
	 * @throws \InvalidArgumentException
142
	 */
143
	protected function getParameters(IEvent $event): array {
144
		$subject = $event->getSubject();
145
		$parameters = $event->getSubjectParameters();
146
147
		switch ($subject) {
148
			case self::PASSWORD_CHANGED_SELF:
149
			case self::PASSWORD_RESET:
150
			case self::PASSWORD_RESET_SELF:
151
			case self::EMAIL_CHANGED_SELF:
152
			case self::EMAIL_CHANGED:
153
				return [];
154
			case self::PASSWORD_CHANGED_BY:
155
			case self::EMAIL_CHANGED_BY:
156
				return [
157
					'actor' => $this->generateUserParameter($parameters[0]),
158
				];
159
			case self::APP_TOKEN_CREATED:
160
			case self::APP_TOKEN_DELETED:
161
			case self::APP_TOKEN_FILESYSTEM_GRANTED:
162
			case self::APP_TOKEN_FILESYSTEM_REVOKED:
163
				return [
164
					'token' => [
165
						'type' => 'highlight',
166
						'id' => $event->getObjectId(),
167
						'name' => $parameters['name'],
168
					]
169
				];
170
			case self::APP_TOKEN_RENAMED:
171
				return [
172
					'token' => [
173
						'type' => 'highlight',
174
						'id' => $event->getObjectId(),
175
						'name' => $parameters['name'],
176
					],
177
					'newToken' => [
178
						'type' => 'highlight',
179
						'id' => $event->getObjectId(),
180
						'name' => $parameters['newName'],
181
					]
182
				];
183
		}
184
185
		throw new \InvalidArgumentException('Unknown subject');
186
	}
187
188
	/**
189
	 * @param IEvent $event
190
	 * @param string $subject
191
	 * @param array $parameters
192
	 * @throws \InvalidArgumentException
193
	 */
194
	protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
195
		$placeholders = $replacements = [];
196
		foreach ($parameters as $placeholder => $parameter) {
197
			$placeholders[] = '{' . $placeholder . '}';
198
			$replacements[] = $parameter['name'];
199
		}
200
201
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
202
			->setRichSubject($subject, $parameters);
203
	}
204
205
	protected function generateUserParameter(string $uid): array {
206
		return [
207
			'type' => 'user',
208
			'id' => $uid,
209
			'name' => $this->userManager->getDisplayName($uid) ?? $uid,
210
		];
211
	}
212
}
213