Provider.php 7.4 KB

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