HookManager.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\CardDAV\CardDavBackend;
  10. use OCA\DAV\CardDAV\SyncService;
  11. use OCP\Defaults;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. use OCP\Util;
  15. use Psr\Log\LoggerInterface;
  16. class HookManager {
  17. /** @var IUser[] */
  18. private $usersToDelete = [];
  19. /** @var array */
  20. private $calendarsToDelete = [];
  21. /** @var array */
  22. private $subscriptionsToDelete = [];
  23. /** @var array */
  24. private $addressBooksToDelete = [];
  25. public function __construct(
  26. private IUserManager $userManager,
  27. private SyncService $syncService,
  28. private CalDavBackend $calDav,
  29. private CardDavBackend $cardDav,
  30. private Defaults $themingDefaults,
  31. ) {
  32. }
  33. public function setup() {
  34. Util::connectHook('OC_User',
  35. 'post_createUser',
  36. $this,
  37. 'postCreateUser');
  38. \OC::$server->getUserManager()->listen('\OC\User', 'assignedUserId', function ($uid): void {
  39. $this->postCreateUser(['uid' => $uid]);
  40. });
  41. Util::connectHook('OC_User',
  42. 'pre_deleteUser',
  43. $this,
  44. 'preDeleteUser');
  45. \OC::$server->getUserManager()->listen('\OC\User', 'preUnassignedUserId', [$this, 'preUnassignedUserId']);
  46. Util::connectHook('OC_User',
  47. 'post_deleteUser',
  48. $this,
  49. 'postDeleteUser');
  50. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', function ($uid): void {
  51. $this->postDeleteUser(['uid' => $uid]);
  52. });
  53. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', [$this, 'postUnassignedUserId']);
  54. Util::connectHook('OC_User', 'changeUser', $this, 'changeUser');
  55. }
  56. public function postCreateUser($params) {
  57. $user = $this->userManager->get($params['uid']);
  58. if ($user instanceof IUser) {
  59. $this->syncService->updateUser($user);
  60. }
  61. }
  62. public function preDeleteUser($params) {
  63. $uid = $params['uid'];
  64. $userPrincipalUri = 'principals/users/' . $uid;
  65. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  66. $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars($userPrincipalUri);
  67. $this->subscriptionsToDelete = $this->calDav->getSubscriptionsForUser($userPrincipalUri);
  68. $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks($userPrincipalUri);
  69. }
  70. public function preUnassignedUserId($uid) {
  71. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  72. }
  73. public function postDeleteUser($params) {
  74. $uid = $params['uid'];
  75. if (isset($this->usersToDelete[$uid])) {
  76. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  77. }
  78. foreach ($this->calendarsToDelete as $calendar) {
  79. $this->calDav->deleteCalendar(
  80. $calendar['id'],
  81. true // Make sure the data doesn't go into the trashbin, a new user with the same UID would later see it otherwise
  82. );
  83. }
  84. foreach ($this->subscriptionsToDelete as $subscription) {
  85. $this->calDav->deleteSubscription(
  86. $subscription['id'],
  87. );
  88. }
  89. $this->calDav->deleteAllSharesByUser('principals/users/' . $uid);
  90. foreach ($this->addressBooksToDelete as $addressBook) {
  91. $this->cardDav->deleteAddressBook($addressBook['id']);
  92. }
  93. }
  94. public function postUnassignedUserId($uid) {
  95. if (isset($this->usersToDelete[$uid])) {
  96. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  97. }
  98. }
  99. public function changeUser($params) {
  100. $user = $params['user'];
  101. $feature = $params['feature'];
  102. // This case is already covered by the account manager firing up a signal
  103. // later on
  104. if ($feature !== 'eMailAddress' && $feature !== 'displayName') {
  105. $this->syncService->updateUser($user);
  106. }
  107. }
  108. /**
  109. * @return void
  110. */
  111. public function firstLogin(?IUser $user = null) {
  112. if (!is_null($user)) {
  113. $principal = 'principals/users/' . $user->getUID();
  114. if ($this->calDav->getCalendarsForUserCount($principal) === 0) {
  115. try {
  116. $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [
  117. '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
  118. '{http://apple.com/ns/ical/}calendar-color' => $this->themingDefaults->getColorPrimary(),
  119. 'components' => 'VEVENT'
  120. ]);
  121. } catch (\Exception $e) {
  122. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
  123. }
  124. }
  125. if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) {
  126. try {
  127. $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [
  128. '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME,
  129. ]);
  130. } catch (\Exception $e) {
  131. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
  132. }
  133. }
  134. }
  135. }
  136. }