1
0

Provider.php 5.6 KB

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