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