1
0

Provider.php 5.4 KB

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