Provider.php 6.9 KB

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