Provider.php 7.3 KB

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