1
0

HookManager.php 5.9 KB

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