hookmanager.php 3.1 KB

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