@@ -42,321 +42,321 @@ |
||
42 | 42 | use OCP\Notification\IManager; |
43 | 43 | |
44 | 44 | class Sync extends TimedJob { |
45 | - const MAX_INTERVAL = 12 * 60 * 60; // 12h |
|
46 | - const MIN_INTERVAL = 30 * 60; // 30min |
|
47 | - /** @var Helper */ |
|
48 | - protected $ldapHelper; |
|
49 | - /** @var LDAP */ |
|
50 | - protected $ldap; |
|
51 | - /** @var Manager */ |
|
52 | - protected $userManager; |
|
53 | - /** @var UserMapping */ |
|
54 | - protected $mapper; |
|
55 | - /** @var IConfig */ |
|
56 | - protected $config; |
|
57 | - /** @var IAvatarManager */ |
|
58 | - protected $avatarManager; |
|
59 | - /** @var IDBConnection */ |
|
60 | - protected $dbc; |
|
61 | - /** @var IUserManager */ |
|
62 | - protected $ncUserManager; |
|
63 | - /** @var IManager */ |
|
64 | - protected $notificationManager; |
|
65 | - |
|
66 | - public function __construct() { |
|
67 | - $this->setInterval( |
|
68 | - \OC::$server->getConfig()->getAppValue( |
|
69 | - 'user_ldap', |
|
70 | - 'background_sync_interval', |
|
71 | - self::MIN_INTERVAL |
|
72 | - ) |
|
73 | - ); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * updates the interval |
|
78 | - * |
|
79 | - * the idea is to adjust the interval depending on the amount of known users |
|
80 | - * and the attempt to update each user one day. At most it would run every |
|
81 | - * 30 minutes, and at least every 12 hours. |
|
82 | - */ |
|
83 | - public function updateInterval() { |
|
84 | - $minPagingSize = $this->getMinPagingSize(); |
|
85 | - $mappedUsers = $this->mapper->count(); |
|
86 | - |
|
87 | - $runsPerDay = ($minPagingSize === 0 || $mappedUsers === 0) ? self::MAX_INTERVAL |
|
88 | - : $mappedUsers / $minPagingSize; |
|
89 | - $interval = floor(24 * 60 * 60 / $runsPerDay); |
|
90 | - $interval = min(max($interval, self::MIN_INTERVAL), self::MAX_INTERVAL); |
|
91 | - |
|
92 | - $this->config->setAppValue('user_ldap', 'background_sync_interval', $interval); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * returns the smallest configured paging size |
|
97 | - * @return int |
|
98 | - */ |
|
99 | - protected function getMinPagingSize() { |
|
100 | - $configKeys = $this->config->getAppKeys('user_ldap'); |
|
101 | - $configKeys = array_filter($configKeys, function($key) { |
|
102 | - return strpos($key, 'ldap_paging_size') !== false; |
|
103 | - }); |
|
104 | - $minPagingSize = null; |
|
105 | - foreach ($configKeys as $configKey) { |
|
106 | - $pagingSize = $this->config->getAppValue('user_ldap', $configKey, $minPagingSize); |
|
107 | - $minPagingSize = $minPagingSize === null ? $pagingSize : min($minPagingSize, $pagingSize); |
|
108 | - } |
|
109 | - return (int)$minPagingSize; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param array $argument |
|
114 | - */ |
|
115 | - protected function run($argument) { |
|
116 | - $this->setArgument($argument); |
|
117 | - |
|
118 | - $isBackgroundJobModeAjax = $this->config |
|
119 | - ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
120 | - if($isBackgroundJobModeAjax) { |
|
121 | - return; |
|
122 | - } |
|
123 | - |
|
124 | - $cycleData = $this->getCycle(); |
|
125 | - if($cycleData === null) { |
|
126 | - $cycleData = $this->determineNextCycle(); |
|
127 | - if($cycleData === null) { |
|
128 | - $this->updateInterval(); |
|
129 | - return; |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - if(!$this->qualifiesToRun($cycleData)) { |
|
134 | - $this->updateInterval(); |
|
135 | - return; |
|
136 | - } |
|
137 | - |
|
138 | - try { |
|
139 | - $expectMoreResults = $this->runCycle($cycleData); |
|
140 | - if ($expectMoreResults) { |
|
141 | - $this->increaseOffset($cycleData); |
|
142 | - } else { |
|
143 | - $this->determineNextCycle(); |
|
144 | - } |
|
145 | - $this->updateInterval(); |
|
146 | - } catch (ServerNotAvailableException $e) { |
|
147 | - $this->determineNextCycle(); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param array $cycleData |
|
153 | - * @return bool whether more results are expected from the same configuration |
|
154 | - */ |
|
155 | - public function runCycle($cycleData) { |
|
156 | - $connection = new Connection($this->ldap, $cycleData['prefix']); |
|
157 | - $access = new Access($connection, $this->ldap, $this->userManager, $this->ldapHelper, $this->config); |
|
158 | - $access->setUserMapper($this->mapper); |
|
159 | - |
|
160 | - $filter = $access->combineFilterWithAnd(array( |
|
161 | - $access->connection->ldapUserFilter, |
|
162 | - $access->connection->ldapUserDisplayName . '=*', |
|
163 | - $access->getFilterPartForUserSearch('') |
|
164 | - )); |
|
165 | - $results = $access->fetchListOfUsers( |
|
166 | - $filter, |
|
167 | - $access->userManager->getAttributes(), |
|
168 | - $connection->ldapPagingSize, |
|
169 | - $cycleData['offset'], |
|
170 | - true |
|
171 | - ); |
|
172 | - |
|
173 | - if($connection->ldapPagingSize === 0) { |
|
174 | - return true; |
|
175 | - } |
|
176 | - return count($results) !== $connection->ldapPagingSize; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * returns the info about the current cycle that should be run, if any, |
|
181 | - * otherwise null |
|
182 | - * |
|
183 | - * @return array|null |
|
184 | - */ |
|
185 | - public function getCycle() { |
|
186 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
187 | - if(count($prefixes) === 0) { |
|
188 | - return null; |
|
189 | - } |
|
190 | - |
|
191 | - $cycleData = [ |
|
192 | - 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null), |
|
193 | - 'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0), |
|
194 | - ]; |
|
195 | - |
|
196 | - if( |
|
197 | - $cycleData['prefix'] !== null |
|
198 | - && in_array($cycleData['prefix'], $prefixes) |
|
199 | - ) { |
|
200 | - return $cycleData; |
|
201 | - } |
|
202 | - |
|
203 | - return null; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Save the provided cycle information in the DB |
|
208 | - * |
|
209 | - * @param array $cycleData |
|
210 | - */ |
|
211 | - public function setCycle(array $cycleData) { |
|
212 | - $this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']); |
|
213 | - $this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * returns data about the next cycle that should run, if any, otherwise |
|
218 | - * null. It also always goes for the next LDAP configuration! |
|
219 | - * |
|
220 | - * @param array|null $cycleData the old cycle |
|
221 | - * @return array|null |
|
222 | - */ |
|
223 | - public function determineNextCycle(array $cycleData = null) { |
|
224 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
225 | - if(count($prefixes) === 0) { |
|
226 | - return null; |
|
227 | - } |
|
228 | - |
|
229 | - // get the next prefix in line and remember it |
|
230 | - $oldPrefix = $cycleData === null ? null : $cycleData['prefix']; |
|
231 | - $prefix = $this->getNextPrefix($oldPrefix); |
|
232 | - if($prefix === null) { |
|
233 | - return null; |
|
234 | - } |
|
235 | - $cycleData['prefix'] = $prefix; |
|
236 | - $cycleData['offset'] = 0; |
|
237 | - $this->setCycle(['prefix' => $prefix, 'offset' => 0]); |
|
238 | - |
|
239 | - return $cycleData; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Checks whether the provided cycle should be run. Currently only the |
|
244 | - * last configuration change goes into account (at least one hour). |
|
245 | - * |
|
246 | - * @param $cycleData |
|
247 | - * @return bool |
|
248 | - */ |
|
249 | - protected function qualifiesToRun($cycleData) { |
|
250 | - $lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0); |
|
251 | - if((time() - $lastChange) > 60 * 30) { |
|
252 | - return true; |
|
253 | - } |
|
254 | - return false; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * increases the offset of the current cycle for the next run |
|
259 | - * |
|
260 | - * @param $cycleData |
|
261 | - */ |
|
262 | - protected function increaseOffset($cycleData) { |
|
263 | - $ldapConfig = new Configuration($cycleData['prefix']); |
|
264 | - $cycleData['offset'] += (int)$ldapConfig->ldapPagingSize; |
|
265 | - $this->setCycle($cycleData); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * determines the next configuration prefix based on the last one (if any) |
|
270 | - * |
|
271 | - * @param string|null $lastPrefix |
|
272 | - * @return string|null |
|
273 | - */ |
|
274 | - protected function getNextPrefix($lastPrefix) { |
|
275 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
276 | - $noOfPrefixes = count($prefixes); |
|
277 | - if($noOfPrefixes === 0) { |
|
278 | - return null; |
|
279 | - } |
|
280 | - $i = $lastPrefix === null ? false : array_search($lastPrefix, $prefixes, true); |
|
281 | - if($i === false) { |
|
282 | - $i = -1; |
|
283 | - } else { |
|
284 | - $i++; |
|
285 | - } |
|
286 | - |
|
287 | - if(!isset($prefixes[$i])) { |
|
288 | - $i = 0; |
|
289 | - } |
|
290 | - return $prefixes[$i]; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * "fixes" DI |
|
295 | - * |
|
296 | - * @param array $argument |
|
297 | - */ |
|
298 | - public function setArgument($argument) { |
|
299 | - if(isset($argument['config'])) { |
|
300 | - $this->config = $argument['config']; |
|
301 | - } else { |
|
302 | - $this->config = \OC::$server->getConfig(); |
|
303 | - } |
|
304 | - |
|
305 | - if(isset($argument['helper'])) { |
|
306 | - $this->ldapHelper = $argument['helper']; |
|
307 | - } else { |
|
308 | - $this->ldapHelper = new Helper($this->config); |
|
309 | - } |
|
310 | - |
|
311 | - if(isset($argument['ldapWrapper'])) { |
|
312 | - $this->ldap = $argument['ldapWrapper']; |
|
313 | - } else { |
|
314 | - $this->ldap = new LDAP(); |
|
315 | - } |
|
316 | - |
|
317 | - if(isset($argument['avatarManager'])) { |
|
318 | - $this->avatarManager = $argument['avatarManager']; |
|
319 | - } else { |
|
320 | - $this->avatarManager = \OC::$server->getAvatarManager(); |
|
321 | - } |
|
322 | - |
|
323 | - if(isset($argument['dbc'])) { |
|
324 | - $this->dbc = $argument['dbc']; |
|
325 | - } else { |
|
326 | - $this->dbc = \OC::$server->getDatabaseConnection(); |
|
327 | - } |
|
328 | - |
|
329 | - if(isset($argument['ncUserManager'])) { |
|
330 | - $this->ncUserManager = $argument['ncUserManager']; |
|
331 | - } else { |
|
332 | - $this->ncUserManager = \OC::$server->getUserManager(); |
|
333 | - } |
|
334 | - |
|
335 | - if(isset($argument['notificationManager'])) { |
|
336 | - $this->notificationManager = $argument['notificationManager']; |
|
337 | - } else { |
|
338 | - $this->notificationManager = \OC::$server->getNotificationManager(); |
|
339 | - } |
|
340 | - |
|
341 | - if(isset($argument['userManager'])) { |
|
342 | - $this->userManager = $argument['userManager']; |
|
343 | - } else { |
|
344 | - $this->userManager = new Manager( |
|
345 | - $this->config, |
|
346 | - new FilesystemHelper(), |
|
347 | - new LogWrapper(), |
|
348 | - $this->avatarManager, |
|
349 | - new Image(), |
|
350 | - $this->dbc, |
|
351 | - $this->ncUserManager, |
|
352 | - $this->notificationManager |
|
353 | - ); |
|
354 | - } |
|
355 | - |
|
356 | - if(isset($argument['mapper'])) { |
|
357 | - $this->mapper = $argument['mapper']; |
|
358 | - } else { |
|
359 | - $this->mapper = new UserMapping($this->dbc); |
|
360 | - } |
|
361 | - } |
|
45 | + const MAX_INTERVAL = 12 * 60 * 60; // 12h |
|
46 | + const MIN_INTERVAL = 30 * 60; // 30min |
|
47 | + /** @var Helper */ |
|
48 | + protected $ldapHelper; |
|
49 | + /** @var LDAP */ |
|
50 | + protected $ldap; |
|
51 | + /** @var Manager */ |
|
52 | + protected $userManager; |
|
53 | + /** @var UserMapping */ |
|
54 | + protected $mapper; |
|
55 | + /** @var IConfig */ |
|
56 | + protected $config; |
|
57 | + /** @var IAvatarManager */ |
|
58 | + protected $avatarManager; |
|
59 | + /** @var IDBConnection */ |
|
60 | + protected $dbc; |
|
61 | + /** @var IUserManager */ |
|
62 | + protected $ncUserManager; |
|
63 | + /** @var IManager */ |
|
64 | + protected $notificationManager; |
|
65 | + |
|
66 | + public function __construct() { |
|
67 | + $this->setInterval( |
|
68 | + \OC::$server->getConfig()->getAppValue( |
|
69 | + 'user_ldap', |
|
70 | + 'background_sync_interval', |
|
71 | + self::MIN_INTERVAL |
|
72 | + ) |
|
73 | + ); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * updates the interval |
|
78 | + * |
|
79 | + * the idea is to adjust the interval depending on the amount of known users |
|
80 | + * and the attempt to update each user one day. At most it would run every |
|
81 | + * 30 minutes, and at least every 12 hours. |
|
82 | + */ |
|
83 | + public function updateInterval() { |
|
84 | + $minPagingSize = $this->getMinPagingSize(); |
|
85 | + $mappedUsers = $this->mapper->count(); |
|
86 | + |
|
87 | + $runsPerDay = ($minPagingSize === 0 || $mappedUsers === 0) ? self::MAX_INTERVAL |
|
88 | + : $mappedUsers / $minPagingSize; |
|
89 | + $interval = floor(24 * 60 * 60 / $runsPerDay); |
|
90 | + $interval = min(max($interval, self::MIN_INTERVAL), self::MAX_INTERVAL); |
|
91 | + |
|
92 | + $this->config->setAppValue('user_ldap', 'background_sync_interval', $interval); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * returns the smallest configured paging size |
|
97 | + * @return int |
|
98 | + */ |
|
99 | + protected function getMinPagingSize() { |
|
100 | + $configKeys = $this->config->getAppKeys('user_ldap'); |
|
101 | + $configKeys = array_filter($configKeys, function($key) { |
|
102 | + return strpos($key, 'ldap_paging_size') !== false; |
|
103 | + }); |
|
104 | + $minPagingSize = null; |
|
105 | + foreach ($configKeys as $configKey) { |
|
106 | + $pagingSize = $this->config->getAppValue('user_ldap', $configKey, $minPagingSize); |
|
107 | + $minPagingSize = $minPagingSize === null ? $pagingSize : min($minPagingSize, $pagingSize); |
|
108 | + } |
|
109 | + return (int)$minPagingSize; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param array $argument |
|
114 | + */ |
|
115 | + protected function run($argument) { |
|
116 | + $this->setArgument($argument); |
|
117 | + |
|
118 | + $isBackgroundJobModeAjax = $this->config |
|
119 | + ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
120 | + if($isBackgroundJobModeAjax) { |
|
121 | + return; |
|
122 | + } |
|
123 | + |
|
124 | + $cycleData = $this->getCycle(); |
|
125 | + if($cycleData === null) { |
|
126 | + $cycleData = $this->determineNextCycle(); |
|
127 | + if($cycleData === null) { |
|
128 | + $this->updateInterval(); |
|
129 | + return; |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + if(!$this->qualifiesToRun($cycleData)) { |
|
134 | + $this->updateInterval(); |
|
135 | + return; |
|
136 | + } |
|
137 | + |
|
138 | + try { |
|
139 | + $expectMoreResults = $this->runCycle($cycleData); |
|
140 | + if ($expectMoreResults) { |
|
141 | + $this->increaseOffset($cycleData); |
|
142 | + } else { |
|
143 | + $this->determineNextCycle(); |
|
144 | + } |
|
145 | + $this->updateInterval(); |
|
146 | + } catch (ServerNotAvailableException $e) { |
|
147 | + $this->determineNextCycle(); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param array $cycleData |
|
153 | + * @return bool whether more results are expected from the same configuration |
|
154 | + */ |
|
155 | + public function runCycle($cycleData) { |
|
156 | + $connection = new Connection($this->ldap, $cycleData['prefix']); |
|
157 | + $access = new Access($connection, $this->ldap, $this->userManager, $this->ldapHelper, $this->config); |
|
158 | + $access->setUserMapper($this->mapper); |
|
159 | + |
|
160 | + $filter = $access->combineFilterWithAnd(array( |
|
161 | + $access->connection->ldapUserFilter, |
|
162 | + $access->connection->ldapUserDisplayName . '=*', |
|
163 | + $access->getFilterPartForUserSearch('') |
|
164 | + )); |
|
165 | + $results = $access->fetchListOfUsers( |
|
166 | + $filter, |
|
167 | + $access->userManager->getAttributes(), |
|
168 | + $connection->ldapPagingSize, |
|
169 | + $cycleData['offset'], |
|
170 | + true |
|
171 | + ); |
|
172 | + |
|
173 | + if($connection->ldapPagingSize === 0) { |
|
174 | + return true; |
|
175 | + } |
|
176 | + return count($results) !== $connection->ldapPagingSize; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * returns the info about the current cycle that should be run, if any, |
|
181 | + * otherwise null |
|
182 | + * |
|
183 | + * @return array|null |
|
184 | + */ |
|
185 | + public function getCycle() { |
|
186 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
187 | + if(count($prefixes) === 0) { |
|
188 | + return null; |
|
189 | + } |
|
190 | + |
|
191 | + $cycleData = [ |
|
192 | + 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null), |
|
193 | + 'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0), |
|
194 | + ]; |
|
195 | + |
|
196 | + if( |
|
197 | + $cycleData['prefix'] !== null |
|
198 | + && in_array($cycleData['prefix'], $prefixes) |
|
199 | + ) { |
|
200 | + return $cycleData; |
|
201 | + } |
|
202 | + |
|
203 | + return null; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Save the provided cycle information in the DB |
|
208 | + * |
|
209 | + * @param array $cycleData |
|
210 | + */ |
|
211 | + public function setCycle(array $cycleData) { |
|
212 | + $this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']); |
|
213 | + $this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * returns data about the next cycle that should run, if any, otherwise |
|
218 | + * null. It also always goes for the next LDAP configuration! |
|
219 | + * |
|
220 | + * @param array|null $cycleData the old cycle |
|
221 | + * @return array|null |
|
222 | + */ |
|
223 | + public function determineNextCycle(array $cycleData = null) { |
|
224 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
225 | + if(count($prefixes) === 0) { |
|
226 | + return null; |
|
227 | + } |
|
228 | + |
|
229 | + // get the next prefix in line and remember it |
|
230 | + $oldPrefix = $cycleData === null ? null : $cycleData['prefix']; |
|
231 | + $prefix = $this->getNextPrefix($oldPrefix); |
|
232 | + if($prefix === null) { |
|
233 | + return null; |
|
234 | + } |
|
235 | + $cycleData['prefix'] = $prefix; |
|
236 | + $cycleData['offset'] = 0; |
|
237 | + $this->setCycle(['prefix' => $prefix, 'offset' => 0]); |
|
238 | + |
|
239 | + return $cycleData; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Checks whether the provided cycle should be run. Currently only the |
|
244 | + * last configuration change goes into account (at least one hour). |
|
245 | + * |
|
246 | + * @param $cycleData |
|
247 | + * @return bool |
|
248 | + */ |
|
249 | + protected function qualifiesToRun($cycleData) { |
|
250 | + $lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0); |
|
251 | + if((time() - $lastChange) > 60 * 30) { |
|
252 | + return true; |
|
253 | + } |
|
254 | + return false; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * increases the offset of the current cycle for the next run |
|
259 | + * |
|
260 | + * @param $cycleData |
|
261 | + */ |
|
262 | + protected function increaseOffset($cycleData) { |
|
263 | + $ldapConfig = new Configuration($cycleData['prefix']); |
|
264 | + $cycleData['offset'] += (int)$ldapConfig->ldapPagingSize; |
|
265 | + $this->setCycle($cycleData); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * determines the next configuration prefix based on the last one (if any) |
|
270 | + * |
|
271 | + * @param string|null $lastPrefix |
|
272 | + * @return string|null |
|
273 | + */ |
|
274 | + protected function getNextPrefix($lastPrefix) { |
|
275 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
|
276 | + $noOfPrefixes = count($prefixes); |
|
277 | + if($noOfPrefixes === 0) { |
|
278 | + return null; |
|
279 | + } |
|
280 | + $i = $lastPrefix === null ? false : array_search($lastPrefix, $prefixes, true); |
|
281 | + if($i === false) { |
|
282 | + $i = -1; |
|
283 | + } else { |
|
284 | + $i++; |
|
285 | + } |
|
286 | + |
|
287 | + if(!isset($prefixes[$i])) { |
|
288 | + $i = 0; |
|
289 | + } |
|
290 | + return $prefixes[$i]; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * "fixes" DI |
|
295 | + * |
|
296 | + * @param array $argument |
|
297 | + */ |
|
298 | + public function setArgument($argument) { |
|
299 | + if(isset($argument['config'])) { |
|
300 | + $this->config = $argument['config']; |
|
301 | + } else { |
|
302 | + $this->config = \OC::$server->getConfig(); |
|
303 | + } |
|
304 | + |
|
305 | + if(isset($argument['helper'])) { |
|
306 | + $this->ldapHelper = $argument['helper']; |
|
307 | + } else { |
|
308 | + $this->ldapHelper = new Helper($this->config); |
|
309 | + } |
|
310 | + |
|
311 | + if(isset($argument['ldapWrapper'])) { |
|
312 | + $this->ldap = $argument['ldapWrapper']; |
|
313 | + } else { |
|
314 | + $this->ldap = new LDAP(); |
|
315 | + } |
|
316 | + |
|
317 | + if(isset($argument['avatarManager'])) { |
|
318 | + $this->avatarManager = $argument['avatarManager']; |
|
319 | + } else { |
|
320 | + $this->avatarManager = \OC::$server->getAvatarManager(); |
|
321 | + } |
|
322 | + |
|
323 | + if(isset($argument['dbc'])) { |
|
324 | + $this->dbc = $argument['dbc']; |
|
325 | + } else { |
|
326 | + $this->dbc = \OC::$server->getDatabaseConnection(); |
|
327 | + } |
|
328 | + |
|
329 | + if(isset($argument['ncUserManager'])) { |
|
330 | + $this->ncUserManager = $argument['ncUserManager']; |
|
331 | + } else { |
|
332 | + $this->ncUserManager = \OC::$server->getUserManager(); |
|
333 | + } |
|
334 | + |
|
335 | + if(isset($argument['notificationManager'])) { |
|
336 | + $this->notificationManager = $argument['notificationManager']; |
|
337 | + } else { |
|
338 | + $this->notificationManager = \OC::$server->getNotificationManager(); |
|
339 | + } |
|
340 | + |
|
341 | + if(isset($argument['userManager'])) { |
|
342 | + $this->userManager = $argument['userManager']; |
|
343 | + } else { |
|
344 | + $this->userManager = new Manager( |
|
345 | + $this->config, |
|
346 | + new FilesystemHelper(), |
|
347 | + new LogWrapper(), |
|
348 | + $this->avatarManager, |
|
349 | + new Image(), |
|
350 | + $this->dbc, |
|
351 | + $this->ncUserManager, |
|
352 | + $this->notificationManager |
|
353 | + ); |
|
354 | + } |
|
355 | + |
|
356 | + if(isset($argument['mapper'])) { |
|
357 | + $this->mapper = $argument['mapper']; |
|
358 | + } else { |
|
359 | + $this->mapper = new UserMapping($this->dbc); |
|
360 | + } |
|
361 | + } |
|
362 | 362 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | $pagingSize = $this->config->getAppValue('user_ldap', $configKey, $minPagingSize); |
107 | 107 | $minPagingSize = $minPagingSize === null ? $pagingSize : min($minPagingSize, $pagingSize); |
108 | 108 | } |
109 | - return (int)$minPagingSize; |
|
109 | + return (int) $minPagingSize; |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | /** |
@@ -117,20 +117,20 @@ discard block |
||
117 | 117 | |
118 | 118 | $isBackgroundJobModeAjax = $this->config |
119 | 119 | ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
120 | - if($isBackgroundJobModeAjax) { |
|
120 | + if ($isBackgroundJobModeAjax) { |
|
121 | 121 | return; |
122 | 122 | } |
123 | 123 | |
124 | 124 | $cycleData = $this->getCycle(); |
125 | - if($cycleData === null) { |
|
125 | + if ($cycleData === null) { |
|
126 | 126 | $cycleData = $this->determineNextCycle(); |
127 | - if($cycleData === null) { |
|
127 | + if ($cycleData === null) { |
|
128 | 128 | $this->updateInterval(); |
129 | 129 | return; |
130 | 130 | } |
131 | 131 | } |
132 | 132 | |
133 | - if(!$this->qualifiesToRun($cycleData)) { |
|
133 | + if (!$this->qualifiesToRun($cycleData)) { |
|
134 | 134 | $this->updateInterval(); |
135 | 135 | return; |
136 | 136 | } |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | |
160 | 160 | $filter = $access->combineFilterWithAnd(array( |
161 | 161 | $access->connection->ldapUserFilter, |
162 | - $access->connection->ldapUserDisplayName . '=*', |
|
162 | + $access->connection->ldapUserDisplayName.'=*', |
|
163 | 163 | $access->getFilterPartForUserSearch('') |
164 | 164 | )); |
165 | 165 | $results = $access->fetchListOfUsers( |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | true |
171 | 171 | ); |
172 | 172 | |
173 | - if($connection->ldapPagingSize === 0) { |
|
173 | + if ($connection->ldapPagingSize === 0) { |
|
174 | 174 | return true; |
175 | 175 | } |
176 | 176 | return count($results) !== $connection->ldapPagingSize; |
@@ -184,16 +184,16 @@ discard block |
||
184 | 184 | */ |
185 | 185 | public function getCycle() { |
186 | 186 | $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
187 | - if(count($prefixes) === 0) { |
|
187 | + if (count($prefixes) === 0) { |
|
188 | 188 | return null; |
189 | 189 | } |
190 | 190 | |
191 | 191 | $cycleData = [ |
192 | 192 | 'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null), |
193 | - 'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0), |
|
193 | + 'offset' => (int) $this->config->getAppValue('user_ldap', 'background_sync_offset', 0), |
|
194 | 194 | ]; |
195 | 195 | |
196 | - if( |
|
196 | + if ( |
|
197 | 197 | $cycleData['prefix'] !== null |
198 | 198 | && in_array($cycleData['prefix'], $prefixes) |
199 | 199 | ) { |
@@ -222,14 +222,14 @@ discard block |
||
222 | 222 | */ |
223 | 223 | public function determineNextCycle(array $cycleData = null) { |
224 | 224 | $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
225 | - if(count($prefixes) === 0) { |
|
225 | + if (count($prefixes) === 0) { |
|
226 | 226 | return null; |
227 | 227 | } |
228 | 228 | |
229 | 229 | // get the next prefix in line and remember it |
230 | 230 | $oldPrefix = $cycleData === null ? null : $cycleData['prefix']; |
231 | 231 | $prefix = $this->getNextPrefix($oldPrefix); |
232 | - if($prefix === null) { |
|
232 | + if ($prefix === null) { |
|
233 | 233 | return null; |
234 | 234 | } |
235 | 235 | $cycleData['prefix'] = $prefix; |
@@ -247,8 +247,8 @@ discard block |
||
247 | 247 | * @return bool |
248 | 248 | */ |
249 | 249 | protected function qualifiesToRun($cycleData) { |
250 | - $lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0); |
|
251 | - if((time() - $lastChange) > 60 * 30) { |
|
250 | + $lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'].'_lastChange', 0); |
|
251 | + if ((time() - $lastChange) > 60 * 30) { |
|
252 | 252 | return true; |
253 | 253 | } |
254 | 254 | return false; |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | */ |
262 | 262 | protected function increaseOffset($cycleData) { |
263 | 263 | $ldapConfig = new Configuration($cycleData['prefix']); |
264 | - $cycleData['offset'] += (int)$ldapConfig->ldapPagingSize; |
|
264 | + $cycleData['offset'] += (int) $ldapConfig->ldapPagingSize; |
|
265 | 265 | $this->setCycle($cycleData); |
266 | 266 | } |
267 | 267 | |
@@ -274,17 +274,17 @@ discard block |
||
274 | 274 | protected function getNextPrefix($lastPrefix) { |
275 | 275 | $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true); |
276 | 276 | $noOfPrefixes = count($prefixes); |
277 | - if($noOfPrefixes === 0) { |
|
277 | + if ($noOfPrefixes === 0) { |
|
278 | 278 | return null; |
279 | 279 | } |
280 | 280 | $i = $lastPrefix === null ? false : array_search($lastPrefix, $prefixes, true); |
281 | - if($i === false) { |
|
281 | + if ($i === false) { |
|
282 | 282 | $i = -1; |
283 | 283 | } else { |
284 | 284 | $i++; |
285 | 285 | } |
286 | 286 | |
287 | - if(!isset($prefixes[$i])) { |
|
287 | + if (!isset($prefixes[$i])) { |
|
288 | 288 | $i = 0; |
289 | 289 | } |
290 | 290 | return $prefixes[$i]; |
@@ -296,49 +296,49 @@ discard block |
||
296 | 296 | * @param array $argument |
297 | 297 | */ |
298 | 298 | public function setArgument($argument) { |
299 | - if(isset($argument['config'])) { |
|
299 | + if (isset($argument['config'])) { |
|
300 | 300 | $this->config = $argument['config']; |
301 | 301 | } else { |
302 | 302 | $this->config = \OC::$server->getConfig(); |
303 | 303 | } |
304 | 304 | |
305 | - if(isset($argument['helper'])) { |
|
305 | + if (isset($argument['helper'])) { |
|
306 | 306 | $this->ldapHelper = $argument['helper']; |
307 | 307 | } else { |
308 | 308 | $this->ldapHelper = new Helper($this->config); |
309 | 309 | } |
310 | 310 | |
311 | - if(isset($argument['ldapWrapper'])) { |
|
311 | + if (isset($argument['ldapWrapper'])) { |
|
312 | 312 | $this->ldap = $argument['ldapWrapper']; |
313 | 313 | } else { |
314 | 314 | $this->ldap = new LDAP(); |
315 | 315 | } |
316 | 316 | |
317 | - if(isset($argument['avatarManager'])) { |
|
317 | + if (isset($argument['avatarManager'])) { |
|
318 | 318 | $this->avatarManager = $argument['avatarManager']; |
319 | 319 | } else { |
320 | 320 | $this->avatarManager = \OC::$server->getAvatarManager(); |
321 | 321 | } |
322 | 322 | |
323 | - if(isset($argument['dbc'])) { |
|
323 | + if (isset($argument['dbc'])) { |
|
324 | 324 | $this->dbc = $argument['dbc']; |
325 | 325 | } else { |
326 | 326 | $this->dbc = \OC::$server->getDatabaseConnection(); |
327 | 327 | } |
328 | 328 | |
329 | - if(isset($argument['ncUserManager'])) { |
|
329 | + if (isset($argument['ncUserManager'])) { |
|
330 | 330 | $this->ncUserManager = $argument['ncUserManager']; |
331 | 331 | } else { |
332 | 332 | $this->ncUserManager = \OC::$server->getUserManager(); |
333 | 333 | } |
334 | 334 | |
335 | - if(isset($argument['notificationManager'])) { |
|
335 | + if (isset($argument['notificationManager'])) { |
|
336 | 336 | $this->notificationManager = $argument['notificationManager']; |
337 | 337 | } else { |
338 | 338 | $this->notificationManager = \OC::$server->getNotificationManager(); |
339 | 339 | } |
340 | 340 | |
341 | - if(isset($argument['userManager'])) { |
|
341 | + if (isset($argument['userManager'])) { |
|
342 | 342 | $this->userManager = $argument['userManager']; |
343 | 343 | } else { |
344 | 344 | $this->userManager = new Manager( |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | ); |
354 | 354 | } |
355 | 355 | |
356 | - if(isset($argument['mapper'])) { |
|
356 | + if (isset($argument['mapper'])) { |
|
357 | 357 | $this->mapper = $argument['mapper']; |
358 | 358 | } else { |
359 | 359 | $this->mapper = new UserMapping($this->dbc); |
@@ -37,170 +37,170 @@ |
||
37 | 37 | use OCA\User_LDAP\User\Manager; |
38 | 38 | |
39 | 39 | abstract class Proxy { |
40 | - static private $accesses = array(); |
|
41 | - private $ldap = null; |
|
42 | - |
|
43 | - /** @var \OCP\ICache|null */ |
|
44 | - private $cache; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param ILDAPWrapper $ldap |
|
48 | - */ |
|
49 | - public function __construct(ILDAPWrapper $ldap) { |
|
50 | - $this->ldap = $ldap; |
|
51 | - $memcache = \OC::$server->getMemCacheFactory(); |
|
52 | - if($memcache->isAvailable()) { |
|
53 | - $this->cache = $memcache->create(); |
|
54 | - } |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $configPrefix |
|
59 | - */ |
|
60 | - private function addAccess($configPrefix) { |
|
61 | - static $ocConfig; |
|
62 | - static $fs; |
|
63 | - static $log; |
|
64 | - static $avatarM; |
|
65 | - static $userMap; |
|
66 | - static $groupMap; |
|
67 | - static $db; |
|
68 | - static $coreUserManager; |
|
69 | - static $coreNotificationManager; |
|
70 | - if($fs === null) { |
|
71 | - $ocConfig = \OC::$server->getConfig(); |
|
72 | - $fs = new FilesystemHelper(); |
|
73 | - $log = new LogWrapper(); |
|
74 | - $avatarM = \OC::$server->getAvatarManager(); |
|
75 | - $db = \OC::$server->getDatabaseConnection(); |
|
76 | - $userMap = new UserMapping($db); |
|
77 | - $groupMap = new GroupMapping($db); |
|
78 | - $coreUserManager = \OC::$server->getUserManager(); |
|
79 | - $coreNotificationManager = \OC::$server->getNotificationManager(); |
|
80 | - } |
|
81 | - $userManager = |
|
82 | - new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, |
|
83 | - $coreUserManager, $coreNotificationManager); |
|
84 | - $connector = new Connection($this->ldap, $configPrefix); |
|
85 | - $access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig); |
|
86 | - $access->setUserMapper($userMap); |
|
87 | - $access->setGroupMapper($groupMap); |
|
88 | - self::$accesses[$configPrefix] = $access; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $configPrefix |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - protected function getAccess($configPrefix) { |
|
96 | - if(!isset(self::$accesses[$configPrefix])) { |
|
97 | - $this->addAccess($configPrefix); |
|
98 | - } |
|
99 | - return self::$accesses[$configPrefix]; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $uid |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - protected function getUserCacheKey($uid) { |
|
107 | - return 'user-'.$uid.'-lastSeenOn'; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param string $gid |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - protected function getGroupCacheKey($gid) { |
|
115 | - return 'group-'.$gid.'-lastSeenOn'; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param string $id |
|
120 | - * @param string $method |
|
121 | - * @param array $parameters |
|
122 | - * @param bool $passOnWhen |
|
123 | - * @return mixed |
|
124 | - */ |
|
125 | - abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
126 | - |
|
127 | - /** |
|
128 | - * @param string $id |
|
129 | - * @param string $method |
|
130 | - * @param array $parameters |
|
131 | - * @return mixed |
|
132 | - */ |
|
133 | - abstract protected function walkBackends($id, $method, $parameters); |
|
134 | - |
|
135 | - /** |
|
136 | - * @param string $id |
|
137 | - * @return Access |
|
138 | - */ |
|
139 | - abstract public function getLDAPAccess($id); |
|
140 | - |
|
141 | - /** |
|
142 | - * Takes care of the request to the User backend |
|
143 | - * @param string $id |
|
144 | - * @param string $method string, the method of the user backend that shall be called |
|
145 | - * @param array $parameters an array of parameters to be passed |
|
146 | - * @param bool $passOnWhen |
|
147 | - * @return mixed, the result of the specified method |
|
148 | - */ |
|
149 | - protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
|
150 | - $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
151 | - if($result === $passOnWhen) { |
|
152 | - $result = $this->walkBackends($id, $method, $parameters); |
|
153 | - } |
|
154 | - return $result; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param string|null $key |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - private function getCacheKey($key) { |
|
162 | - $prefix = 'LDAP-Proxy-'; |
|
163 | - if($key === null) { |
|
164 | - return $prefix; |
|
165 | - } |
|
166 | - return $prefix.md5($key); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string $key |
|
171 | - * @return mixed|null |
|
172 | - */ |
|
173 | - public function getFromCache($key) { |
|
174 | - if($this->cache === null) { |
|
175 | - return null; |
|
176 | - } |
|
177 | - |
|
178 | - $key = $this->getCacheKey($key); |
|
179 | - $value = $this->cache->get($key); |
|
180 | - if ($value === null) { |
|
181 | - return null; |
|
182 | - } |
|
183 | - |
|
184 | - return json_decode(base64_decode($value)); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @param string $key |
|
189 | - * @param mixed $value |
|
190 | - */ |
|
191 | - public function writeToCache($key, $value) { |
|
192 | - if($this->cache === null) { |
|
193 | - return; |
|
194 | - } |
|
195 | - $key = $this->getCacheKey($key); |
|
196 | - $value = base64_encode(json_encode($value)); |
|
197 | - $this->cache->set($key, $value, 2592000); |
|
198 | - } |
|
199 | - |
|
200 | - public function clearCache() { |
|
201 | - if($this->cache === null) { |
|
202 | - return; |
|
203 | - } |
|
204 | - $this->cache->clear($this->getCacheKey(null)); |
|
205 | - } |
|
40 | + static private $accesses = array(); |
|
41 | + private $ldap = null; |
|
42 | + |
|
43 | + /** @var \OCP\ICache|null */ |
|
44 | + private $cache; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param ILDAPWrapper $ldap |
|
48 | + */ |
|
49 | + public function __construct(ILDAPWrapper $ldap) { |
|
50 | + $this->ldap = $ldap; |
|
51 | + $memcache = \OC::$server->getMemCacheFactory(); |
|
52 | + if($memcache->isAvailable()) { |
|
53 | + $this->cache = $memcache->create(); |
|
54 | + } |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $configPrefix |
|
59 | + */ |
|
60 | + private function addAccess($configPrefix) { |
|
61 | + static $ocConfig; |
|
62 | + static $fs; |
|
63 | + static $log; |
|
64 | + static $avatarM; |
|
65 | + static $userMap; |
|
66 | + static $groupMap; |
|
67 | + static $db; |
|
68 | + static $coreUserManager; |
|
69 | + static $coreNotificationManager; |
|
70 | + if($fs === null) { |
|
71 | + $ocConfig = \OC::$server->getConfig(); |
|
72 | + $fs = new FilesystemHelper(); |
|
73 | + $log = new LogWrapper(); |
|
74 | + $avatarM = \OC::$server->getAvatarManager(); |
|
75 | + $db = \OC::$server->getDatabaseConnection(); |
|
76 | + $userMap = new UserMapping($db); |
|
77 | + $groupMap = new GroupMapping($db); |
|
78 | + $coreUserManager = \OC::$server->getUserManager(); |
|
79 | + $coreNotificationManager = \OC::$server->getNotificationManager(); |
|
80 | + } |
|
81 | + $userManager = |
|
82 | + new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, |
|
83 | + $coreUserManager, $coreNotificationManager); |
|
84 | + $connector = new Connection($this->ldap, $configPrefix); |
|
85 | + $access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig); |
|
86 | + $access->setUserMapper($userMap); |
|
87 | + $access->setGroupMapper($groupMap); |
|
88 | + self::$accesses[$configPrefix] = $access; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $configPrefix |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + protected function getAccess($configPrefix) { |
|
96 | + if(!isset(self::$accesses[$configPrefix])) { |
|
97 | + $this->addAccess($configPrefix); |
|
98 | + } |
|
99 | + return self::$accesses[$configPrefix]; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $uid |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + protected function getUserCacheKey($uid) { |
|
107 | + return 'user-'.$uid.'-lastSeenOn'; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param string $gid |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + protected function getGroupCacheKey($gid) { |
|
115 | + return 'group-'.$gid.'-lastSeenOn'; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param string $id |
|
120 | + * @param string $method |
|
121 | + * @param array $parameters |
|
122 | + * @param bool $passOnWhen |
|
123 | + * @return mixed |
|
124 | + */ |
|
125 | + abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
126 | + |
|
127 | + /** |
|
128 | + * @param string $id |
|
129 | + * @param string $method |
|
130 | + * @param array $parameters |
|
131 | + * @return mixed |
|
132 | + */ |
|
133 | + abstract protected function walkBackends($id, $method, $parameters); |
|
134 | + |
|
135 | + /** |
|
136 | + * @param string $id |
|
137 | + * @return Access |
|
138 | + */ |
|
139 | + abstract public function getLDAPAccess($id); |
|
140 | + |
|
141 | + /** |
|
142 | + * Takes care of the request to the User backend |
|
143 | + * @param string $id |
|
144 | + * @param string $method string, the method of the user backend that shall be called |
|
145 | + * @param array $parameters an array of parameters to be passed |
|
146 | + * @param bool $passOnWhen |
|
147 | + * @return mixed, the result of the specified method |
|
148 | + */ |
|
149 | + protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
|
150 | + $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
151 | + if($result === $passOnWhen) { |
|
152 | + $result = $this->walkBackends($id, $method, $parameters); |
|
153 | + } |
|
154 | + return $result; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param string|null $key |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + private function getCacheKey($key) { |
|
162 | + $prefix = 'LDAP-Proxy-'; |
|
163 | + if($key === null) { |
|
164 | + return $prefix; |
|
165 | + } |
|
166 | + return $prefix.md5($key); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string $key |
|
171 | + * @return mixed|null |
|
172 | + */ |
|
173 | + public function getFromCache($key) { |
|
174 | + if($this->cache === null) { |
|
175 | + return null; |
|
176 | + } |
|
177 | + |
|
178 | + $key = $this->getCacheKey($key); |
|
179 | + $value = $this->cache->get($key); |
|
180 | + if ($value === null) { |
|
181 | + return null; |
|
182 | + } |
|
183 | + |
|
184 | + return json_decode(base64_decode($value)); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @param string $key |
|
189 | + * @param mixed $value |
|
190 | + */ |
|
191 | + public function writeToCache($key, $value) { |
|
192 | + if($this->cache === null) { |
|
193 | + return; |
|
194 | + } |
|
195 | + $key = $this->getCacheKey($key); |
|
196 | + $value = base64_encode(json_encode($value)); |
|
197 | + $this->cache->set($key, $value, 2592000); |
|
198 | + } |
|
199 | + |
|
200 | + public function clearCache() { |
|
201 | + if($this->cache === null) { |
|
202 | + return; |
|
203 | + } |
|
204 | + $this->cache->clear($this->getCacheKey(null)); |
|
205 | + } |
|
206 | 206 | } |
@@ -36,13 +36,13 @@ discard block |
||
36 | 36 | $l = \OC::$server->getL10N('user_ldap'); |
37 | 37 | |
38 | 38 | if(!isset($_POST['action'])) { |
39 | - \OCP\JSON::error(array('message' => $l->t('No action specified'))); |
|
39 | + \OCP\JSON::error(array('message' => $l->t('No action specified'))); |
|
40 | 40 | } |
41 | 41 | $action = (string)$_POST['action']; |
42 | 42 | |
43 | 43 | |
44 | 44 | if(!isset($_POST['ldap_serverconfig_chooser'])) { |
45 | - \OCP\JSON::error(array('message' => $l->t('No configuration specified'))); |
|
45 | + \OCP\JSON::error(array('message' => $l->t('No configuration specified'))); |
|
46 | 46 | } |
47 | 47 | $prefix = (string)$_POST['ldap_serverconfig_chooser']; |
48 | 48 | |
@@ -55,95 +55,95 @@ discard block |
||
55 | 55 | $con->setIgnoreValidation(true); |
56 | 56 | |
57 | 57 | $userManager = new \OCA\User_LDAP\User\Manager( |
58 | - \OC::$server->getConfig(), |
|
59 | - new \OCA\User_LDAP\FilesystemHelper(), |
|
60 | - new \OCA\User_LDAP\LogWrapper(), |
|
61 | - \OC::$server->getAvatarManager(), |
|
62 | - new \OCP\Image(), |
|
63 | - \OC::$server->getDatabaseConnection(), |
|
64 | - \OC::$server->getUserManager(), |
|
65 | - \OC::$server->getNotificationManager()); |
|
58 | + \OC::$server->getConfig(), |
|
59 | + new \OCA\User_LDAP\FilesystemHelper(), |
|
60 | + new \OCA\User_LDAP\LogWrapper(), |
|
61 | + \OC::$server->getAvatarManager(), |
|
62 | + new \OCP\Image(), |
|
63 | + \OC::$server->getDatabaseConnection(), |
|
64 | + \OC::$server->getUserManager(), |
|
65 | + \OC::$server->getNotificationManager()); |
|
66 | 66 | |
67 | 67 | $access = new \OCA\User_LDAP\Access( |
68 | - $con, |
|
69 | - $ldapWrapper, |
|
70 | - $userManager, |
|
71 | - new \OCA\User_LDAP\Helper(\OC::$server->getConfig()), |
|
72 | - \OC::$server->getConfig() |
|
68 | + $con, |
|
69 | + $ldapWrapper, |
|
70 | + $userManager, |
|
71 | + new \OCA\User_LDAP\Helper(\OC::$server->getConfig()), |
|
72 | + \OC::$server->getConfig() |
|
73 | 73 | ); |
74 | 74 | |
75 | 75 | $wizard = new \OCA\User_LDAP\Wizard($configuration, $ldapWrapper, $access); |
76 | 76 | |
77 | 77 | switch($action) { |
78 | - case 'guessPortAndTLS': |
|
79 | - case 'guessBaseDN': |
|
80 | - case 'detectEmailAttribute': |
|
81 | - case 'detectUserDisplayNameAttribute': |
|
82 | - case 'determineGroupMemberAssoc': |
|
83 | - case 'determineUserObjectClasses': |
|
84 | - case 'determineGroupObjectClasses': |
|
85 | - case 'determineGroupsForUsers': |
|
86 | - case 'determineGroupsForGroups': |
|
87 | - case 'determineAttributes': |
|
88 | - case 'getUserListFilter': |
|
89 | - case 'getUserLoginFilter': |
|
90 | - case 'getGroupFilter': |
|
91 | - case 'countUsers': |
|
92 | - case 'countGroups': |
|
93 | - case 'countInBaseDN': |
|
94 | - try { |
|
95 | - $result = $wizard->$action(); |
|
96 | - if($result !== false) { |
|
97 | - OCP\JSON::success($result->getResultArray()); |
|
98 | - exit; |
|
99 | - } |
|
100 | - } catch (\Exception $e) { |
|
101 | - \OCP\JSON::error(array('message' => $e->getMessage(), 'code' => $e->getCode())); |
|
102 | - exit; |
|
103 | - } |
|
104 | - \OCP\JSON::error(); |
|
105 | - exit; |
|
106 | - break; |
|
78 | + case 'guessPortAndTLS': |
|
79 | + case 'guessBaseDN': |
|
80 | + case 'detectEmailAttribute': |
|
81 | + case 'detectUserDisplayNameAttribute': |
|
82 | + case 'determineGroupMemberAssoc': |
|
83 | + case 'determineUserObjectClasses': |
|
84 | + case 'determineGroupObjectClasses': |
|
85 | + case 'determineGroupsForUsers': |
|
86 | + case 'determineGroupsForGroups': |
|
87 | + case 'determineAttributes': |
|
88 | + case 'getUserListFilter': |
|
89 | + case 'getUserLoginFilter': |
|
90 | + case 'getGroupFilter': |
|
91 | + case 'countUsers': |
|
92 | + case 'countGroups': |
|
93 | + case 'countInBaseDN': |
|
94 | + try { |
|
95 | + $result = $wizard->$action(); |
|
96 | + if($result !== false) { |
|
97 | + OCP\JSON::success($result->getResultArray()); |
|
98 | + exit; |
|
99 | + } |
|
100 | + } catch (\Exception $e) { |
|
101 | + \OCP\JSON::error(array('message' => $e->getMessage(), 'code' => $e->getCode())); |
|
102 | + exit; |
|
103 | + } |
|
104 | + \OCP\JSON::error(); |
|
105 | + exit; |
|
106 | + break; |
|
107 | 107 | |
108 | - case 'testLoginName': { |
|
109 | - try { |
|
110 | - $loginName = $_POST['ldap_test_loginname']; |
|
111 | - $result = $wizard->$action($loginName); |
|
112 | - if($result !== false) { |
|
113 | - OCP\JSON::success($result->getResultArray()); |
|
114 | - exit; |
|
115 | - } |
|
116 | - } catch (\Exception $e) { |
|
117 | - \OCP\JSON::error(array('message' => $e->getMessage())); |
|
118 | - exit; |
|
119 | - } |
|
120 | - \OCP\JSON::error(); |
|
121 | - exit; |
|
122 | - break; |
|
123 | - } |
|
108 | + case 'testLoginName': { |
|
109 | + try { |
|
110 | + $loginName = $_POST['ldap_test_loginname']; |
|
111 | + $result = $wizard->$action($loginName); |
|
112 | + if($result !== false) { |
|
113 | + OCP\JSON::success($result->getResultArray()); |
|
114 | + exit; |
|
115 | + } |
|
116 | + } catch (\Exception $e) { |
|
117 | + \OCP\JSON::error(array('message' => $e->getMessage())); |
|
118 | + exit; |
|
119 | + } |
|
120 | + \OCP\JSON::error(); |
|
121 | + exit; |
|
122 | + break; |
|
123 | + } |
|
124 | 124 | |
125 | - case 'save': |
|
126 | - $key = isset($_POST['cfgkey']) ? $_POST['cfgkey'] : false; |
|
127 | - $val = isset($_POST['cfgval']) ? $_POST['cfgval'] : null; |
|
128 | - if($key === false || is_null($val)) { |
|
129 | - \OCP\JSON::error(array('message' => $l->t('No data specified'))); |
|
130 | - exit; |
|
131 | - } |
|
132 | - $cfg = array($key => $val); |
|
133 | - $setParameters = array(); |
|
134 | - $configuration->setConfiguration($cfg, $setParameters); |
|
135 | - if(!in_array($key, $setParameters)) { |
|
136 | - \OCP\JSON::error(array('message' => $l->t($key. |
|
137 | - ' Could not set configuration %s', $setParameters[0]))); |
|
138 | - exit; |
|
139 | - } |
|
140 | - $configuration->saveConfiguration(); |
|
141 | - //clear the cache on save |
|
142 | - $connection = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix); |
|
143 | - $connection->clearCache(); |
|
144 | - OCP\JSON::success(); |
|
145 | - break; |
|
146 | - default: |
|
147 | - \OCP\JSON::error(array('message' => $l->t('Action does not exist'))); |
|
148 | - break; |
|
125 | + case 'save': |
|
126 | + $key = isset($_POST['cfgkey']) ? $_POST['cfgkey'] : false; |
|
127 | + $val = isset($_POST['cfgval']) ? $_POST['cfgval'] : null; |
|
128 | + if($key === false || is_null($val)) { |
|
129 | + \OCP\JSON::error(array('message' => $l->t('No data specified'))); |
|
130 | + exit; |
|
131 | + } |
|
132 | + $cfg = array($key => $val); |
|
133 | + $setParameters = array(); |
|
134 | + $configuration->setConfiguration($cfg, $setParameters); |
|
135 | + if(!in_array($key, $setParameters)) { |
|
136 | + \OCP\JSON::error(array('message' => $l->t($key. |
|
137 | + ' Could not set configuration %s', $setParameters[0]))); |
|
138 | + exit; |
|
139 | + } |
|
140 | + $configuration->saveConfiguration(); |
|
141 | + //clear the cache on save |
|
142 | + $connection = new \OCA\User_LDAP\Connection($ldapWrapper, $prefix); |
|
143 | + $connection->clearCache(); |
|
144 | + OCP\JSON::success(); |
|
145 | + break; |
|
146 | + default: |
|
147 | + \OCP\JSON::error(array('message' => $l->t('Action does not exist'))); |
|
148 | + break; |
|
149 | 149 | } |