HookManager.php 6.1 KB

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