HookManager.php 4.0 KB

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