HookManager.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Citharel <tcit@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  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, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CardDAV\CardDavBackend;
  30. use OCA\DAV\CardDAV\SyncService;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\Util;
  34. use Symfony\Component\EventDispatcher\EventDispatcher;
  35. class HookManager {
  36. /** @var IUserManager */
  37. private $userManager;
  38. /** @var SyncService */
  39. private $syncService;
  40. /** @var IUser[] */
  41. private $usersToDelete = [];
  42. /** @var CalDavBackend */
  43. private $calDav;
  44. /** @var CardDavBackend */
  45. private $cardDav;
  46. /** @var array */
  47. private $calendarsToDelete = [];
  48. /** @var array */
  49. private $addressBooksToDelete = [];
  50. /** @var EventDispatcher */
  51. private $eventDispatcher;
  52. public function __construct(IUserManager $userManager,
  53. SyncService $syncService,
  54. CalDavBackend $calDav,
  55. CardDavBackend $cardDav,
  56. EventDispatcher $eventDispatcher) {
  57. $this->userManager = $userManager;
  58. $this->syncService = $syncService;
  59. $this->calDav = $calDav;
  60. $this->cardDav = $cardDav;
  61. $this->eventDispatcher = $eventDispatcher;
  62. }
  63. public function setup() {
  64. Util::connectHook('OC_User',
  65. 'post_createUser',
  66. $this,
  67. 'postCreateUser');
  68. \OC::$server->getUserManager()->listen('\OC\User', 'assignedUserId', function ($uid) {
  69. $this->postCreateUser(['uid' => $uid]);
  70. });
  71. Util::connectHook('OC_User',
  72. 'pre_deleteUser',
  73. $this,
  74. 'preDeleteUser');
  75. \OC::$server->getUserManager()->listen('\OC\User', 'preUnassignedUserId', [$this, 'preUnassignedUserId']);
  76. Util::connectHook('OC_User',
  77. 'post_deleteUser',
  78. $this,
  79. 'postDeleteUser');
  80. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', function ($uid) {
  81. $this->postDeleteUser(['uid' => $uid]);
  82. });
  83. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', [$this, 'postUnassignedUserId']);
  84. Util::connectHook('OC_User',
  85. 'changeUser',
  86. $this,
  87. 'changeUser');
  88. }
  89. public function postCreateUser($params) {
  90. $user = $this->userManager->get($params['uid']);
  91. if ($user instanceof IUser) {
  92. $this->syncService->updateUser($user);
  93. }
  94. }
  95. public function preDeleteUser($params) {
  96. $uid = $params['uid'];
  97. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  98. $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars('principals/users/' . $uid);
  99. $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks('principals/users/' . $uid);
  100. }
  101. public function preUnassignedUserId($uid) {
  102. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  103. }
  104. public function postDeleteUser($params) {
  105. $uid = $params['uid'];
  106. if (isset($this->usersToDelete[$uid])){
  107. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  108. }
  109. foreach ($this->calendarsToDelete as $calendar) {
  110. $this->calDav->deleteCalendar($calendar['id']);
  111. }
  112. $this->calDav->deleteAllSharesByUser('principals/users/' . $uid);
  113. foreach ($this->addressBooksToDelete as $addressBook) {
  114. $this->cardDav->deleteAddressBook($addressBook['id']);
  115. }
  116. }
  117. public function postUnassignedUserId($uid) {
  118. if (isset($this->usersToDelete[$uid])){
  119. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  120. }
  121. }
  122. public function changeUser($params) {
  123. $user = $params['user'];
  124. $this->syncService->updateUser($user);
  125. }
  126. public function firstLogin(IUser $user = null) {
  127. if (!is_null($user)) {
  128. $principal = 'principals/users/' . $user->getUID();
  129. if ($this->calDav->getCalendarsForUserCount($principal) === 0) {
  130. try {
  131. $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [
  132. '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
  133. ]);
  134. } catch (\Exception $ex) {
  135. \OC::$server->getLogger()->logException($ex);
  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 $ex) {
  144. \OC::$server->getLogger()->logException($ex);
  145. }
  146. }
  147. }
  148. }
  149. }