1
0

Provider.php 6.7 KB

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