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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\Defaults;
  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 $subscriptionsToDelete = [];
  53. /** @var array */
  54. private $addressBooksToDelete = [];
  55. /** @var Defaults */
  56. private $themingDefaults;
  57. /** @var EventDispatcherInterface */
  58. private $eventDispatcher;
  59. public function __construct(IUserManager $userManager,
  60. SyncService $syncService,
  61. CalDavBackend $calDav,
  62. CardDavBackend $cardDav,
  63. Defaults $themingDefaults,
  64. EventDispatcherInterface $eventDispatcher) {
  65. $this->userManager = $userManager;
  66. $this->syncService = $syncService;
  67. $this->calDav = $calDav;
  68. $this->cardDav = $cardDav;
  69. $this->themingDefaults = $themingDefaults;
  70. $this->eventDispatcher = $eventDispatcher;
  71. }
  72. public function setup() {
  73. Util::connectHook('OC_User',
  74. 'post_createUser',
  75. $this,
  76. 'postCreateUser');
  77. \OC::$server->getUserManager()->listen('\OC\User', 'assignedUserId', function ($uid) {
  78. $this->postCreateUser(['uid' => $uid]);
  79. });
  80. Util::connectHook('OC_User',
  81. 'pre_deleteUser',
  82. $this,
  83. 'preDeleteUser');
  84. \OC::$server->getUserManager()->listen('\OC\User', 'preUnassignedUserId', [$this, 'preUnassignedUserId']);
  85. Util::connectHook('OC_User',
  86. 'post_deleteUser',
  87. $this,
  88. 'postDeleteUser');
  89. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', function ($uid) {
  90. $this->postDeleteUser(['uid' => $uid]);
  91. });
  92. \OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', [$this, 'postUnassignedUserId']);
  93. Util::connectHook('OC_User',
  94. 'changeUser',
  95. $this,
  96. 'changeUser');
  97. }
  98. public function postCreateUser($params) {
  99. $user = $this->userManager->get($params['uid']);
  100. if ($user instanceof IUser) {
  101. $this->syncService->updateUser($user);
  102. }
  103. }
  104. public function preDeleteUser($params) {
  105. $uid = $params['uid'];
  106. $userPrincipalUri = 'principals/users/' . $uid;
  107. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  108. $this->calendarsToDelete = $this->calDav->getUsersOwnCalendars($userPrincipalUri);
  109. $this->subscriptionsToDelete = $this->calDav->getSubscriptionsForUser($userPrincipalUri);
  110. $this->addressBooksToDelete = $this->cardDav->getUsersOwnAddressBooks($userPrincipalUri);
  111. }
  112. public function preUnassignedUserId($uid) {
  113. $this->usersToDelete[$uid] = $this->userManager->get($uid);
  114. }
  115. public function postDeleteUser($params) {
  116. $uid = $params['uid'];
  117. if (isset($this->usersToDelete[$uid])) {
  118. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  119. }
  120. foreach ($this->calendarsToDelete as $calendar) {
  121. $this->calDav->deleteCalendar(
  122. $calendar['id'],
  123. true // Make sure the data doesn't go into the trashbin, a new user with the same UID would later see it otherwise
  124. );
  125. }
  126. foreach ($this->subscriptionsToDelete as $subscription) {
  127. $this->calDav->deleteSubscription(
  128. $subscription['id'],
  129. );
  130. }
  131. $this->calDav->deleteAllSharesByUser('principals/users/' . $uid);
  132. foreach ($this->addressBooksToDelete as $addressBook) {
  133. $this->cardDav->deleteAddressBook($addressBook['id']);
  134. }
  135. }
  136. public function postUnassignedUserId($uid) {
  137. if (isset($this->usersToDelete[$uid])) {
  138. $this->syncService->deleteUser($this->usersToDelete[$uid]);
  139. }
  140. }
  141. public function changeUser($params) {
  142. $user = $params['user'];
  143. $this->syncService->updateUser($user);
  144. }
  145. public function firstLogin(IUser $user = null) {
  146. if (!is_null($user)) {
  147. $principal = 'principals/users/' . $user->getUID();
  148. if ($this->calDav->getCalendarsForUserCount($principal) === 0) {
  149. try {
  150. $this->calDav->createCalendar($principal, CalDavBackend::PERSONAL_CALENDAR_URI, [
  151. '{DAV:}displayname' => CalDavBackend::PERSONAL_CALENDAR_NAME,
  152. '{http://apple.com/ns/ical/}calendar-color' => $this->themingDefaults->getColorPrimary(),
  153. 'components' => 'VEVENT'
  154. ]);
  155. } catch (\Exception $e) {
  156. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
  157. }
  158. }
  159. if ($this->cardDav->getAddressBooksForUserCount($principal) === 0) {
  160. try {
  161. $this->cardDav->createAddressBook($principal, CardDavBackend::PERSONAL_ADDRESSBOOK_URI, [
  162. '{DAV:}displayname' => CardDavBackend::PERSONAL_ADDRESSBOOK_NAME,
  163. ]);
  164. } catch (\Exception $e) {
  165. \OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]);
  166. }
  167. }
  168. }
  169. }
  170. }