You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HookManager.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Thomas Citharel <nextcloud@tcit.fr>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV;
  30. use OCA\DAV\CalDAV\CalDavBackend;
  31. use OCA\DAV\CardDAV\CardDavBackend;
  32. use OCA\DAV\CardDAV\SyncService;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\Util;
  36. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  37. class HookManager {
  38. /** @var IUserManager */
  39. private $userManager;
  40. /** @var SyncService */
  41. private $syncService;
  42. /** @var IUser[] */
  43. private $usersToDelete = [];
  44. /** @var CalDavBackend */
  45. private $calDav;
  46. /** @var CardDavBackend */
  47. private $cardDav;
  48. /** @var array */
  49. private $calendarsToDelete = [];
  50. /** @var array */
  51. private $addressBooksToDelete = [];
  52. /** @var EventDispatcherInterface */
  53. private $eventDispatcher;
  54. public function __construct(IUserManager $userManager,
  55. SyncService $syncService,
  56. CalDavBackend $calDav,
  57. CardDavBackend $cardDav,
  58. EventDispatcherInterface $eventDispatcher) {
  59. $this->userManager = $userManager;
  60. $this->syncService = $syncService;
  61. $this->calDav = $calDav;
  62. $this->cardDav = $cardDav;
  63. $this->eventDispatcher = $eventDispatcher;
  64. }
  65. public function setup() {
  66. Util::connectHook('OC_User',
  67. 'post_createUser',
  68. $this,
  69. 'postCreateUser');
  70. \OC::$server->getUserManager()->listen('\OC\User', 'assignedUserId', function ($uid) {
  71. $this->postCreateUser(['uid' => $uid]);
  72. });
  73. Util::connectHook('OC_User',
  74. 'pre_deleteUser',
  75. $this,
  76. 'preDeleteUser');
  77. \OC::$server->getUserManager()->listen('\OC\User', 'preUnassignedUserId', [$this, 'preUnassignedUserId']);
  78. Util::connectHook('OC_User',
  79. 'post_deleteUser',
  80. $this,
  81. 'postDeleteUser');
  82. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', function ($uid) {
  83. $this->postDeleteUser(['uid' => $uid]);
  84. });
  85. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', [$this, 'postUnassignedUserId']);
  86. Util::connectHook('OC_User',
  87. 'changeUser',
  88. $this,
  89. 'changeUser');
  90. }
  91. public function postCreateUser($params) {
  92. $user = $this->userManager->get($params['uid']);
  93. if ($user instanceof IUser) {
  94. $this->syncService->updateUser($user);
  95. }
  96. }
  97. public function preDeleteUser($params) {
  98. $uid = $params['uid'];
  99. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  100. $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid);
  101. $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid);
  102. }
  103. public function preUnassignedUserId($uid) {
  104. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  105. }
  106. public function postDeleteUser($params) {
  107. $uid = $params['uid'];
  108. if (isset($this->usersToDelete[$uid])) {
  109. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  110. }
  111. foreach ($this->calendarsToDelete as $calendar) {
  112. $this->calDav->deleteCalendar(
  113. $calendar['id'],
  114. true // Make sure the data doesn't go into the trashbin, a new user with the same UID would later see it otherwise
  115. );
  116. }
  117. $this->calDav->deleteAllSharesByUser('principals/users/' . $uid);
  118. foreach ($this->addressBooksToDelete as $addressBook) {
  119. $this->cardDav->deleteAddressBook($addressBook['id']);
  120. }
  121. }
  122. public function postUnassignedUserId($uid) {
  123. if (isset($this->usersToDelete[$uid])) {
  124. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  125. }
  126. }
  127. public function changeUser($params) {
  128. $user = $params['user'];
  129. $this->syncService->updateUser($user);
  130. }
  131. public function firstLogin(IUser $user = null) {
  132. if (!is_null($user)) {
  133. $principal = 'principals/users/' . $user->getUID();
  134. if ($this->calDav->getCalendarsForUserCount($principal) === 0) {
  135. try {
  136. $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [
  137. '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
  138. ]);
  139. } catch (\Exception $ex) {
  140. \OC::$server->getLogger()->logException($ex);
  141. }
  142. }
  143. if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) {
  144. try {
  145. $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [
  146. '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME,
  147. ]);
  148. } catch (\Exception $ex) {
  149. \OC::$server->getLogger()->logException($ex);
  150. }
  151. }
  152. }
  153. }
  154. }