1
0

HookManager.php 5.2 KB

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