1
0

HookManager.php 5.0 KB

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