Provider.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Activity;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IManager;
  10. use OCP\Activity\IProvider;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserManager;
  14. use OCP\L10N\IFactory;
  15. class Provider implements IProvider {
  16. public const PASSWORD_CHANGED_BY = 'password_changed_by';
  17. public const PASSWORD_CHANGED_SELF = 'password_changed_self';
  18. public const PASSWORD_RESET = 'password_changed';
  19. public const PASSWORD_RESET_SELF = 'password_reset_self';
  20. public const EMAIL_CHANGED_BY = 'email_changed_by';
  21. public const EMAIL_CHANGED_SELF = 'email_changed_self';
  22. public const EMAIL_CHANGED = 'email_changed';
  23. public const APP_TOKEN_CREATED = 'app_token_created';
  24. public const APP_TOKEN_DELETED = 'app_token_deleted';
  25. public const APP_TOKEN_RENAMED = 'app_token_renamed';
  26. public const APP_TOKEN_FILESYSTEM_GRANTED = 'app_token_filesystem_granted';
  27. public const APP_TOKEN_FILESYSTEM_REVOKED = 'app_token_filesystem_revoked';
  28. /** @var IFactory */
  29. protected $languageFactory;
  30. /** @var IL10N */
  31. protected $l;
  32. /** @var IURLGenerator */
  33. protected $url;
  34. /** @var IUserManager */
  35. protected $userManager;
  36. /** @var IManager */
  37. private $activityManager;
  38. public function __construct(IFactory $languageFactory,
  39. IURLGenerator $url,
  40. IUserManager $userManager,
  41. IManager $activityManager) {
  42. $this->languageFactory = $languageFactory;
  43. $this->url = $url;
  44. $this->userManager = $userManager;
  45. $this->activityManager = $activityManager;
  46. }
  47. /**
  48. * @param string $language
  49. * @param IEvent $event
  50. * @param IEvent|null $previousEvent
  51. * @return IEvent
  52. * @throws \InvalidArgumentException
  53. * @since 11.0.0
  54. */
  55. public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
  56. if ($event->getApp() !== 'settings') {
  57. throw new \InvalidArgumentException('Unknown app');
  58. }
  59. $this->l = $this->languageFactory->get('settings', $language);
  60. if ($this->activityManager->getRequirePNG()) {
  61. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png')));
  62. } else {
  63. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg')));
  64. }
  65. if ($event->getSubject() === self::PASSWORD_CHANGED_BY) {
  66. $subject = $this->l->t('{actor} changed your password');
  67. } elseif ($event->getSubject() === self::PASSWORD_CHANGED_SELF) {
  68. $subject = $this->l->t('You changed your password');
  69. } elseif ($event->getSubject() === self::PASSWORD_RESET) {
  70. $subject = $this->l->t('Your password was reset by an administrator');
  71. } elseif ($event->getSubject() === self::PASSWORD_RESET_SELF) {
  72. $subject = $this->l->t('Your password was reset');
  73. } elseif ($event->getSubject() === self::EMAIL_CHANGED_BY) {
  74. $subject = $this->l->t('{actor} changed your email address');
  75. } elseif ($event->getSubject() === self::EMAIL_CHANGED_SELF) {
  76. $subject = $this->l->t('You changed your email address');
  77. } elseif ($event->getSubject() === self::EMAIL_CHANGED) {
  78. $subject = $this->l->t('Your email address was changed by an administrator');
  79. } elseif ($event->getSubject() === self::APP_TOKEN_CREATED) {
  80. if ($event->getAffectedUser() === $event->getAuthor()) {
  81. $subject = $this->l->t('You created an app password for a session named "{token}"');
  82. } else {
  83. $subject = $this->l->t('An administrator created an app password for a session named "{token}"');
  84. }
  85. } elseif ($event->getSubject() === self::APP_TOKEN_DELETED) {
  86. $subject = $this->l->t('You deleted app password "{token}"');
  87. } elseif ($event->getSubject() === self::APP_TOKEN_RENAMED) {
  88. $subject = $this->l->t('You renamed app password "{token}" to "{newToken}"');
  89. } elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_GRANTED) {
  90. $subject = $this->l->t('You granted filesystem access to app password "{token}"');
  91. } elseif ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_REVOKED) {
  92. $subject = $this->l->t('You revoked filesystem access from app password "{token}"');
  93. } else {
  94. throw new \InvalidArgumentException('Unknown subject');
  95. }
  96. $parsedParameters = $this->getParameters($event);
  97. $this->setSubjects($event, $subject, $parsedParameters);
  98. return $event;
  99. }
  100. /**
  101. * @param IEvent $event
  102. * @return array
  103. * @throws \InvalidArgumentException
  104. */
  105. protected function getParameters(IEvent $event): array {
  106. $subject = $event->getSubject();
  107. $parameters = $event->getSubjectParameters();
  108. switch ($subject) {
  109. case self::PASSWORD_CHANGED_SELF:
  110. case self::PASSWORD_RESET:
  111. case self::PASSWORD_RESET_SELF:
  112. case self::EMAIL_CHANGED_SELF:
  113. case self::EMAIL_CHANGED:
  114. return [];
  115. case self::PASSWORD_CHANGED_BY:
  116. case self::EMAIL_CHANGED_BY:
  117. return [
  118. 'actor' => $this->generateUserParameter($parameters[0]),
  119. ];
  120. case self::APP_TOKEN_CREATED:
  121. case self::APP_TOKEN_DELETED:
  122. case self::APP_TOKEN_FILESYSTEM_GRANTED:
  123. case self::APP_TOKEN_FILESYSTEM_REVOKED:
  124. return [
  125. 'token' => [
  126. 'type' => 'highlight',
  127. 'id' => $event->getObjectId(),
  128. 'name' => $parameters['name'],
  129. ]
  130. ];
  131. case self::APP_TOKEN_RENAMED:
  132. return [
  133. 'token' => [
  134. 'type' => 'highlight',
  135. 'id' => $event->getObjectId(),
  136. 'name' => $parameters['name'],
  137. ],
  138. 'newToken' => [
  139. 'type' => 'highlight',
  140. 'id' => $event->getObjectId(),
  141. 'name' => $parameters['newName'],
  142. ]
  143. ];
  144. }
  145. throw new \InvalidArgumentException('Unknown subject');
  146. }
  147. /**
  148. * @throws \InvalidArgumentException
  149. */
  150. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  151. $event->setRichSubject($subject, $parameters);
  152. }
  153. protected function generateUserParameter(string $uid): array {
  154. return [
  155. 'type' => 'user',
  156. 'id' => $uid,
  157. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  158. ];
  159. }
  160. }