Provider.php 5.3 KB

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