Provider.php 5.9 KB

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