@@ -24,26 +24,26 @@ |
||
24 | 24 | |
25 | 25 | interface IUserLDAP { |
26 | 26 | |
27 | - //Functions used by LDAPProvider |
|
27 | + //Functions used by LDAPProvider |
|
28 | 28 | |
29 | - /** |
|
30 | - * Return access for LDAP interaction. |
|
31 | - * @param string $uid |
|
32 | - * @return Access instance of Access for LDAP interaction |
|
33 | - */ |
|
34 | - public function getLDAPAccess($uid); |
|
29 | + /** |
|
30 | + * Return access for LDAP interaction. |
|
31 | + * @param string $uid |
|
32 | + * @return Access instance of Access for LDAP interaction |
|
33 | + */ |
|
34 | + public function getLDAPAccess($uid); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Return a new LDAP connection for the specified user. |
|
38 | - * @param string $uid |
|
39 | - * @return resource of the LDAP connection |
|
40 | - */ |
|
41 | - public function getNewLDAPConnection($uid); |
|
36 | + /** |
|
37 | + * Return a new LDAP connection for the specified user. |
|
38 | + * @param string $uid |
|
39 | + * @return resource of the LDAP connection |
|
40 | + */ |
|
41 | + public function getNewLDAPConnection($uid); |
|
42 | 42 | |
43 | - /** |
|
44 | - * Return the username for the given LDAP DN, if available. |
|
45 | - * @param string $dn |
|
46 | - * @return string|false with the username |
|
47 | - */ |
|
48 | - public function dn2UserName($dn); |
|
43 | + /** |
|
44 | + * Return the username for the given LDAP DN, if available. |
|
45 | + * @param string $dn |
|
46 | + * @return string|false with the username |
|
47 | + */ |
|
48 | + public function dn2UserName($dn); |
|
49 | 49 | } |
@@ -40,283 +40,283 @@ |
||
40 | 40 | |
41 | 41 | class ConfigAPIController extends OCSController { |
42 | 42 | |
43 | - /** @var Helper */ |
|
44 | - private $ldapHelper; |
|
43 | + /** @var Helper */ |
|
44 | + private $ldapHelper; |
|
45 | 45 | |
46 | - /** @var ILogger */ |
|
47 | - private $logger; |
|
46 | + /** @var ILogger */ |
|
47 | + private $logger; |
|
48 | 48 | |
49 | - public function __construct( |
|
50 | - $appName, |
|
51 | - IRequest $request, |
|
52 | - CapabilitiesManager $capabilitiesManager, |
|
53 | - IUserSession $userSession, |
|
54 | - IUserManager $userManager, |
|
55 | - Throttler $throttler, |
|
56 | - Manager $keyManager, |
|
57 | - Helper $ldapHelper, |
|
58 | - ILogger $logger |
|
59 | - ) { |
|
60 | - parent::__construct( |
|
61 | - $appName, |
|
62 | - $request, |
|
63 | - $capabilitiesManager, |
|
64 | - $userSession, |
|
65 | - $userManager, |
|
66 | - $throttler, |
|
67 | - $keyManager |
|
68 | - ); |
|
49 | + public function __construct( |
|
50 | + $appName, |
|
51 | + IRequest $request, |
|
52 | + CapabilitiesManager $capabilitiesManager, |
|
53 | + IUserSession $userSession, |
|
54 | + IUserManager $userManager, |
|
55 | + Throttler $throttler, |
|
56 | + Manager $keyManager, |
|
57 | + Helper $ldapHelper, |
|
58 | + ILogger $logger |
|
59 | + ) { |
|
60 | + parent::__construct( |
|
61 | + $appName, |
|
62 | + $request, |
|
63 | + $capabilitiesManager, |
|
64 | + $userSession, |
|
65 | + $userManager, |
|
66 | + $throttler, |
|
67 | + $keyManager |
|
68 | + ); |
|
69 | 69 | |
70 | 70 | |
71 | - $this->ldapHelper = $ldapHelper; |
|
72 | - $this->logger = $logger; |
|
73 | - } |
|
71 | + $this->ldapHelper = $ldapHelper; |
|
72 | + $this->logger = $logger; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * creates a new (empty) configuration and returns the resulting prefix |
|
77 | - * |
|
78 | - * Example: curl -X POST -H "OCS-APIREQUEST: true" -u $admin:$password \ |
|
79 | - * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config |
|
80 | - * |
|
81 | - * results in: |
|
82 | - * |
|
83 | - * <?xml version="1.0"?> |
|
84 | - * <ocs> |
|
85 | - * <meta> |
|
86 | - * <status>ok</status> |
|
87 | - * <statuscode>200</statuscode> |
|
88 | - * <message>OK</message> |
|
89 | - * </meta> |
|
90 | - * <data> |
|
91 | - * <configID>s40</configID> |
|
92 | - * </data> |
|
93 | - * </ocs> |
|
94 | - * |
|
95 | - * Failing example: if an exception is thrown (e.g. Database connection lost) |
|
96 | - * the detailed error will be logged. The output will then look like: |
|
97 | - * |
|
98 | - * <?xml version="1.0"?> |
|
99 | - * <ocs> |
|
100 | - * <meta> |
|
101 | - * <status>failure</status> |
|
102 | - * <statuscode>999</statuscode> |
|
103 | - * <message>An issue occurred when creating the new config.</message> |
|
104 | - * </meta> |
|
105 | - * <data/> |
|
106 | - * </ocs> |
|
107 | - * |
|
108 | - * For JSON output provide the format=json parameter |
|
109 | - * |
|
110 | - * @return DataResponse |
|
111 | - * @throws OCSException |
|
112 | - */ |
|
113 | - public function create() { |
|
114 | - try { |
|
115 | - $configPrefix = $this->ldapHelper->getNextServerConfigurationPrefix(); |
|
116 | - $configHolder = new Configuration($configPrefix); |
|
117 | - $configHolder->saveConfiguration(); |
|
118 | - } catch (\Exception $e) { |
|
119 | - $this->logger->logException($e); |
|
120 | - throw new OCSException('An issue occurred when creating the new config.'); |
|
121 | - } |
|
122 | - return new DataResponse(['configID' => $configPrefix]); |
|
123 | - } |
|
75 | + /** |
|
76 | + * creates a new (empty) configuration and returns the resulting prefix |
|
77 | + * |
|
78 | + * Example: curl -X POST -H "OCS-APIREQUEST: true" -u $admin:$password \ |
|
79 | + * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config |
|
80 | + * |
|
81 | + * results in: |
|
82 | + * |
|
83 | + * <?xml version="1.0"?> |
|
84 | + * <ocs> |
|
85 | + * <meta> |
|
86 | + * <status>ok</status> |
|
87 | + * <statuscode>200</statuscode> |
|
88 | + * <message>OK</message> |
|
89 | + * </meta> |
|
90 | + * <data> |
|
91 | + * <configID>s40</configID> |
|
92 | + * </data> |
|
93 | + * </ocs> |
|
94 | + * |
|
95 | + * Failing example: if an exception is thrown (e.g. Database connection lost) |
|
96 | + * the detailed error will be logged. The output will then look like: |
|
97 | + * |
|
98 | + * <?xml version="1.0"?> |
|
99 | + * <ocs> |
|
100 | + * <meta> |
|
101 | + * <status>failure</status> |
|
102 | + * <statuscode>999</statuscode> |
|
103 | + * <message>An issue occurred when creating the new config.</message> |
|
104 | + * </meta> |
|
105 | + * <data/> |
|
106 | + * </ocs> |
|
107 | + * |
|
108 | + * For JSON output provide the format=json parameter |
|
109 | + * |
|
110 | + * @return DataResponse |
|
111 | + * @throws OCSException |
|
112 | + */ |
|
113 | + public function create() { |
|
114 | + try { |
|
115 | + $configPrefix = $this->ldapHelper->getNextServerConfigurationPrefix(); |
|
116 | + $configHolder = new Configuration($configPrefix); |
|
117 | + $configHolder->saveConfiguration(); |
|
118 | + } catch (\Exception $e) { |
|
119 | + $this->logger->logException($e); |
|
120 | + throw new OCSException('An issue occurred when creating the new config.'); |
|
121 | + } |
|
122 | + return new DataResponse(['configID' => $configPrefix]); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Deletes a LDAP configuration, if present. |
|
127 | - * |
|
128 | - * Example: |
|
129 | - * curl -X DELETE -H "OCS-APIREQUEST: true" -u $admin:$password \ |
|
130 | - * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60 |
|
131 | - * |
|
132 | - * <?xml version="1.0"?> |
|
133 | - * <ocs> |
|
134 | - * <meta> |
|
135 | - * <status>ok</status> |
|
136 | - * <statuscode>200</statuscode> |
|
137 | - * <message>OK</message> |
|
138 | - * </meta> |
|
139 | - * <data/> |
|
140 | - * </ocs> |
|
141 | - * |
|
142 | - * @param string $configID |
|
143 | - * @return DataResponse |
|
144 | - * @throws OCSBadRequestException |
|
145 | - * @throws OCSException |
|
146 | - */ |
|
147 | - public function delete($configID) { |
|
148 | - try { |
|
149 | - $this->ensureConfigIDExists($configID); |
|
150 | - if(!$this->ldapHelper->deleteServerConfiguration($configID)) { |
|
151 | - throw new OCSException('Could not delete configuration'); |
|
152 | - } |
|
153 | - } catch(OCSException $e) { |
|
154 | - throw $e; |
|
155 | - } catch(\Exception $e) { |
|
156 | - $this->logger->logException($e); |
|
157 | - throw new OCSException('An issue occurred when deleting the config.'); |
|
158 | - } |
|
125 | + /** |
|
126 | + * Deletes a LDAP configuration, if present. |
|
127 | + * |
|
128 | + * Example: |
|
129 | + * curl -X DELETE -H "OCS-APIREQUEST: true" -u $admin:$password \ |
|
130 | + * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60 |
|
131 | + * |
|
132 | + * <?xml version="1.0"?> |
|
133 | + * <ocs> |
|
134 | + * <meta> |
|
135 | + * <status>ok</status> |
|
136 | + * <statuscode>200</statuscode> |
|
137 | + * <message>OK</message> |
|
138 | + * </meta> |
|
139 | + * <data/> |
|
140 | + * </ocs> |
|
141 | + * |
|
142 | + * @param string $configID |
|
143 | + * @return DataResponse |
|
144 | + * @throws OCSBadRequestException |
|
145 | + * @throws OCSException |
|
146 | + */ |
|
147 | + public function delete($configID) { |
|
148 | + try { |
|
149 | + $this->ensureConfigIDExists($configID); |
|
150 | + if(!$this->ldapHelper->deleteServerConfiguration($configID)) { |
|
151 | + throw new OCSException('Could not delete configuration'); |
|
152 | + } |
|
153 | + } catch(OCSException $e) { |
|
154 | + throw $e; |
|
155 | + } catch(\Exception $e) { |
|
156 | + $this->logger->logException($e); |
|
157 | + throw new OCSException('An issue occurred when deleting the config.'); |
|
158 | + } |
|
159 | 159 | |
160 | - return new DataResponse(); |
|
161 | - } |
|
160 | + return new DataResponse(); |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * modifies a configuration |
|
165 | - * |
|
166 | - * Example: |
|
167 | - * curl -X PUT -d "configData[ldapHost]=ldaps://my.ldap.server&configData[ldapPort]=636" \ |
|
168 | - * -H "OCS-APIREQUEST: true" -u $admin:$password \ |
|
169 | - * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60 |
|
170 | - * |
|
171 | - * <?xml version="1.0"?> |
|
172 | - * <ocs> |
|
173 | - * <meta> |
|
174 | - * <status>ok</status> |
|
175 | - * <statuscode>200</statuscode> |
|
176 | - * <message>OK</message> |
|
177 | - * </meta> |
|
178 | - * <data/> |
|
179 | - * </ocs> |
|
180 | - * |
|
181 | - * @param string $configID |
|
182 | - * @param array $configData |
|
183 | - * @return DataResponse |
|
184 | - * @throws OCSException |
|
185 | - */ |
|
186 | - public function modify($configID, $configData) { |
|
187 | - try { |
|
188 | - $this->ensureConfigIDExists($configID); |
|
163 | + /** |
|
164 | + * modifies a configuration |
|
165 | + * |
|
166 | + * Example: |
|
167 | + * curl -X PUT -d "configData[ldapHost]=ldaps://my.ldap.server&configData[ldapPort]=636" \ |
|
168 | + * -H "OCS-APIREQUEST: true" -u $admin:$password \ |
|
169 | + * https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60 |
|
170 | + * |
|
171 | + * <?xml version="1.0"?> |
|
172 | + * <ocs> |
|
173 | + * <meta> |
|
174 | + * <status>ok</status> |
|
175 | + * <statuscode>200</statuscode> |
|
176 | + * <message>OK</message> |
|
177 | + * </meta> |
|
178 | + * <data/> |
|
179 | + * </ocs> |
|
180 | + * |
|
181 | + * @param string $configID |
|
182 | + * @param array $configData |
|
183 | + * @return DataResponse |
|
184 | + * @throws OCSException |
|
185 | + */ |
|
186 | + public function modify($configID, $configData) { |
|
187 | + try { |
|
188 | + $this->ensureConfigIDExists($configID); |
|
189 | 189 | |
190 | - if(!is_array($configData)) { |
|
191 | - throw new OCSBadRequestException('configData is not properly set'); |
|
192 | - } |
|
190 | + if(!is_array($configData)) { |
|
191 | + throw new OCSBadRequestException('configData is not properly set'); |
|
192 | + } |
|
193 | 193 | |
194 | - $configuration = new Configuration($configID); |
|
195 | - $configKeys = $configuration->getConfigTranslationArray(); |
|
194 | + $configuration = new Configuration($configID); |
|
195 | + $configKeys = $configuration->getConfigTranslationArray(); |
|
196 | 196 | |
197 | - foreach ($configKeys as $i => $key) { |
|
198 | - if(isset($configData[$key])) { |
|
199 | - $configuration->$key = $configData[$key]; |
|
200 | - } |
|
201 | - } |
|
197 | + foreach ($configKeys as $i => $key) { |
|
198 | + if(isset($configData[$key])) { |
|
199 | + $configuration->$key = $configData[$key]; |
|
200 | + } |
|
201 | + } |
|
202 | 202 | |
203 | - $configuration->saveConfiguration(); |
|
204 | - } catch(OCSException $e) { |
|
205 | - throw $e; |
|
206 | - } catch (\Exception $e) { |
|
207 | - $this->logger->logException($e); |
|
208 | - throw new OCSException('An issue occurred when modifying the config.'); |
|
209 | - } |
|
203 | + $configuration->saveConfiguration(); |
|
204 | + } catch(OCSException $e) { |
|
205 | + throw $e; |
|
206 | + } catch (\Exception $e) { |
|
207 | + $this->logger->logException($e); |
|
208 | + throw new OCSException('An issue occurred when modifying the config.'); |
|
209 | + } |
|
210 | 210 | |
211 | - return new DataResponse(); |
|
212 | - } |
|
211 | + return new DataResponse(); |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * retrieves a configuration |
|
216 | - * |
|
217 | - * <?xml version="1.0"?> |
|
218 | - * <ocs> |
|
219 | - * <meta> |
|
220 | - * <status>ok</status> |
|
221 | - * <statuscode>200</statuscode> |
|
222 | - * <message>OK</message> |
|
223 | - * </meta> |
|
224 | - * <data> |
|
225 | - * <ldapHost>ldaps://my.ldap.server</ldapHost> |
|
226 | - * <ldapPort>7770</ldapPort> |
|
227 | - * <ldapBackupHost></ldapBackupHost> |
|
228 | - * <ldapBackupPort></ldapBackupPort> |
|
229 | - * <ldapBase>ou=small,dc=my,dc=ldap,dc=server</ldapBase> |
|
230 | - * <ldapBaseUsers>ou=users,ou=small,dc=my,dc=ldap,dc=server</ldapBaseUsers> |
|
231 | - * <ldapBaseGroups>ou=small,dc=my,dc=ldap,dc=server</ldapBaseGroups> |
|
232 | - * <ldapAgentName>cn=root,dc=my,dc=ldap,dc=server</ldapAgentName> |
|
233 | - * <ldapAgentPassword>clearTextWithShowPassword=1</ldapAgentPassword> |
|
234 | - * <ldapTLS>1</ldapTLS> |
|
235 | - * <turnOffCertCheck>0</turnOffCertCheck> |
|
236 | - * <ldapIgnoreNamingRules/> |
|
237 | - * <ldapUserDisplayName>displayname</ldapUserDisplayName> |
|
238 | - * <ldapUserDisplayName2>uid</ldapUserDisplayName2> |
|
239 | - * <ldapUserFilterObjectclass>inetOrgPerson</ldapUserFilterObjectclass> |
|
240 | - * <ldapUserFilterGroups></ldapUserFilterGroups> |
|
241 | - * <ldapUserFilter>(&(objectclass=nextcloudUser)(nextcloudEnabled=TRUE))</ldapUserFilter> |
|
242 | - * <ldapUserFilterMode>1</ldapUserFilterMode> |
|
243 | - * <ldapGroupFilter>(&(|(objectclass=nextcloudGroup)))</ldapGroupFilter> |
|
244 | - * <ldapGroupFilterMode>0</ldapGroupFilterMode> |
|
245 | - * <ldapGroupFilterObjectclass>nextcloudGroup</ldapGroupFilterObjectclass> |
|
246 | - * <ldapGroupFilterGroups></ldapGroupFilterGroups> |
|
247 | - * <ldapGroupDisplayName>cn</ldapGroupDisplayName> |
|
248 | - * <ldapGroupMemberAssocAttr>memberUid</ldapGroupMemberAssocAttr> |
|
249 | - * <ldapLoginFilter>(&(|(objectclass=inetOrgPerson))(uid=%uid))</ldapLoginFilter> |
|
250 | - * <ldapLoginFilterMode>0</ldapLoginFilterMode> |
|
251 | - * <ldapLoginFilterEmail>0</ldapLoginFilterEmail> |
|
252 | - * <ldapLoginFilterUsername>1</ldapLoginFilterUsername> |
|
253 | - * <ldapLoginFilterAttributes></ldapLoginFilterAttributes> |
|
254 | - * <ldapQuotaAttribute></ldapQuotaAttribute> |
|
255 | - * <ldapQuotaDefault></ldapQuotaDefault> |
|
256 | - * <ldapEmailAttribute>mail</ldapEmailAttribute> |
|
257 | - * <ldapCacheTTL>20</ldapCacheTTL> |
|
258 | - * <ldapUuidUserAttribute>auto</ldapUuidUserAttribute> |
|
259 | - * <ldapUuidGroupAttribute>auto</ldapUuidGroupAttribute> |
|
260 | - * <ldapOverrideMainServer></ldapOverrideMainServer> |
|
261 | - * <ldapConfigurationActive>1</ldapConfigurationActive> |
|
262 | - * <ldapAttributesForUserSearch>uid;sn;givenname</ldapAttributesForUserSearch> |
|
263 | - * <ldapAttributesForGroupSearch></ldapAttributesForGroupSearch> |
|
264 | - * <ldapExperiencedAdmin>0</ldapExperiencedAdmin> |
|
265 | - * <homeFolderNamingRule></homeFolderNamingRule> |
|
266 | - * <hasPagedResultSupport></hasPagedResultSupport> |
|
267 | - * <hasMemberOfFilterSupport></hasMemberOfFilterSupport> |
|
268 | - * <useMemberOfToDetectMembership>1</useMemberOfToDetectMembership> |
|
269 | - * <ldapExpertUsernameAttr>uid</ldapExpertUsernameAttr> |
|
270 | - * <ldapExpertUUIDUserAttr>uid</ldapExpertUUIDUserAttr> |
|
271 | - * <ldapExpertUUIDGroupAttr></ldapExpertUUIDGroupAttr> |
|
272 | - * <lastJpegPhotoLookup>0</lastJpegPhotoLookup> |
|
273 | - * <ldapNestedGroups>0</ldapNestedGroups> |
|
274 | - * <ldapPagingSize>500</ldapPagingSize> |
|
275 | - * <turnOnPasswordChange>1</turnOnPasswordChange> |
|
276 | - * <ldapDynamicGroupMemberURL></ldapDynamicGroupMemberURL> |
|
277 | - * </data> |
|
278 | - * </ocs> |
|
279 | - * |
|
280 | - * @param string $configID |
|
281 | - * @param bool|string $showPassword |
|
282 | - * @return DataResponse |
|
283 | - * @throws OCSException |
|
284 | - */ |
|
285 | - public function show($configID, $showPassword = false) { |
|
286 | - try { |
|
287 | - $this->ensureConfigIDExists($configID); |
|
214 | + /** |
|
215 | + * retrieves a configuration |
|
216 | + * |
|
217 | + * <?xml version="1.0"?> |
|
218 | + * <ocs> |
|
219 | + * <meta> |
|
220 | + * <status>ok</status> |
|
221 | + * <statuscode>200</statuscode> |
|
222 | + * <message>OK</message> |
|
223 | + * </meta> |
|
224 | + * <data> |
|
225 | + * <ldapHost>ldaps://my.ldap.server</ldapHost> |
|
226 | + * <ldapPort>7770</ldapPort> |
|
227 | + * <ldapBackupHost></ldapBackupHost> |
|
228 | + * <ldapBackupPort></ldapBackupPort> |
|
229 | + * <ldapBase>ou=small,dc=my,dc=ldap,dc=server</ldapBase> |
|
230 | + * <ldapBaseUsers>ou=users,ou=small,dc=my,dc=ldap,dc=server</ldapBaseUsers> |
|
231 | + * <ldapBaseGroups>ou=small,dc=my,dc=ldap,dc=server</ldapBaseGroups> |
|
232 | + * <ldapAgentName>cn=root,dc=my,dc=ldap,dc=server</ldapAgentName> |
|
233 | + * <ldapAgentPassword>clearTextWithShowPassword=1</ldapAgentPassword> |
|
234 | + * <ldapTLS>1</ldapTLS> |
|
235 | + * <turnOffCertCheck>0</turnOffCertCheck> |
|
236 | + * <ldapIgnoreNamingRules/> |
|
237 | + * <ldapUserDisplayName>displayname</ldapUserDisplayName> |
|
238 | + * <ldapUserDisplayName2>uid</ldapUserDisplayName2> |
|
239 | + * <ldapUserFilterObjectclass>inetOrgPerson</ldapUserFilterObjectclass> |
|
240 | + * <ldapUserFilterGroups></ldapUserFilterGroups> |
|
241 | + * <ldapUserFilter>(&(objectclass=nextcloudUser)(nextcloudEnabled=TRUE))</ldapUserFilter> |
|
242 | + * <ldapUserFilterMode>1</ldapUserFilterMode> |
|
243 | + * <ldapGroupFilter>(&(|(objectclass=nextcloudGroup)))</ldapGroupFilter> |
|
244 | + * <ldapGroupFilterMode>0</ldapGroupFilterMode> |
|
245 | + * <ldapGroupFilterObjectclass>nextcloudGroup</ldapGroupFilterObjectclass> |
|
246 | + * <ldapGroupFilterGroups></ldapGroupFilterGroups> |
|
247 | + * <ldapGroupDisplayName>cn</ldapGroupDisplayName> |
|
248 | + * <ldapGroupMemberAssocAttr>memberUid</ldapGroupMemberAssocAttr> |
|
249 | + * <ldapLoginFilter>(&(|(objectclass=inetOrgPerson))(uid=%uid))</ldapLoginFilter> |
|
250 | + * <ldapLoginFilterMode>0</ldapLoginFilterMode> |
|
251 | + * <ldapLoginFilterEmail>0</ldapLoginFilterEmail> |
|
252 | + * <ldapLoginFilterUsername>1</ldapLoginFilterUsername> |
|
253 | + * <ldapLoginFilterAttributes></ldapLoginFilterAttributes> |
|
254 | + * <ldapQuotaAttribute></ldapQuotaAttribute> |
|
255 | + * <ldapQuotaDefault></ldapQuotaDefault> |
|
256 | + * <ldapEmailAttribute>mail</ldapEmailAttribute> |
|
257 | + * <ldapCacheTTL>20</ldapCacheTTL> |
|
258 | + * <ldapUuidUserAttribute>auto</ldapUuidUserAttribute> |
|
259 | + * <ldapUuidGroupAttribute>auto</ldapUuidGroupAttribute> |
|
260 | + * <ldapOverrideMainServer></ldapOverrideMainServer> |
|
261 | + * <ldapConfigurationActive>1</ldapConfigurationActive> |
|
262 | + * <ldapAttributesForUserSearch>uid;sn;givenname</ldapAttributesForUserSearch> |
|
263 | + * <ldapAttributesForGroupSearch></ldapAttributesForGroupSearch> |
|
264 | + * <ldapExperiencedAdmin>0</ldapExperiencedAdmin> |
|
265 | + * <homeFolderNamingRule></homeFolderNamingRule> |
|
266 | + * <hasPagedResultSupport></hasPagedResultSupport> |
|
267 | + * <hasMemberOfFilterSupport></hasMemberOfFilterSupport> |
|
268 | + * <useMemberOfToDetectMembership>1</useMemberOfToDetectMembership> |
|
269 | + * <ldapExpertUsernameAttr>uid</ldapExpertUsernameAttr> |
|
270 | + * <ldapExpertUUIDUserAttr>uid</ldapExpertUUIDUserAttr> |
|
271 | + * <ldapExpertUUIDGroupAttr></ldapExpertUUIDGroupAttr> |
|
272 | + * <lastJpegPhotoLookup>0</lastJpegPhotoLookup> |
|
273 | + * <ldapNestedGroups>0</ldapNestedGroups> |
|
274 | + * <ldapPagingSize>500</ldapPagingSize> |
|
275 | + * <turnOnPasswordChange>1</turnOnPasswordChange> |
|
276 | + * <ldapDynamicGroupMemberURL></ldapDynamicGroupMemberURL> |
|
277 | + * </data> |
|
278 | + * </ocs> |
|
279 | + * |
|
280 | + * @param string $configID |
|
281 | + * @param bool|string $showPassword |
|
282 | + * @return DataResponse |
|
283 | + * @throws OCSException |
|
284 | + */ |
|
285 | + public function show($configID, $showPassword = false) { |
|
286 | + try { |
|
287 | + $this->ensureConfigIDExists($configID); |
|
288 | 288 | |
289 | - $config = new Configuration($configID); |
|
290 | - $data = $config->getConfiguration(); |
|
291 | - if(!boolval(intval($showPassword))) { |
|
292 | - $data['ldapAgentPassword'] = '***'; |
|
293 | - } |
|
294 | - foreach ($data as $key => $value) { |
|
295 | - if(is_array($value)) { |
|
296 | - $value = implode(';', $value); |
|
297 | - $data[$key] = $value; |
|
298 | - } |
|
299 | - } |
|
300 | - } catch(OCSException $e) { |
|
301 | - throw $e; |
|
302 | - } catch (\Exception $e) { |
|
303 | - $this->logger->logException($e); |
|
304 | - throw new OCSException('An issue occurred when modifying the config.'); |
|
305 | - } |
|
289 | + $config = new Configuration($configID); |
|
290 | + $data = $config->getConfiguration(); |
|
291 | + if(!boolval(intval($showPassword))) { |
|
292 | + $data['ldapAgentPassword'] = '***'; |
|
293 | + } |
|
294 | + foreach ($data as $key => $value) { |
|
295 | + if(is_array($value)) { |
|
296 | + $value = implode(';', $value); |
|
297 | + $data[$key] = $value; |
|
298 | + } |
|
299 | + } |
|
300 | + } catch(OCSException $e) { |
|
301 | + throw $e; |
|
302 | + } catch (\Exception $e) { |
|
303 | + $this->logger->logException($e); |
|
304 | + throw new OCSException('An issue occurred when modifying the config.'); |
|
305 | + } |
|
306 | 306 | |
307 | - return new DataResponse($data); |
|
308 | - } |
|
307 | + return new DataResponse($data); |
|
308 | + } |
|
309 | 309 | |
310 | - /** |
|
311 | - * if the given config ID is not available, an exception is thrown |
|
312 | - * |
|
313 | - * @param string $configID |
|
314 | - * @throws OCSNotFoundException |
|
315 | - */ |
|
316 | - private function ensureConfigIDExists($configID) { |
|
317 | - $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(); |
|
318 | - if(!in_array($configID, $prefixes, true)) { |
|
319 | - throw new OCSNotFoundException('Config ID not found'); |
|
320 | - } |
|
321 | - } |
|
310 | + /** |
|
311 | + * if the given config ID is not available, an exception is thrown |
|
312 | + * |
|
313 | + * @param string $configID |
|
314 | + * @throws OCSNotFoundException |
|
315 | + */ |
|
316 | + private function ensureConfigIDExists($configID) { |
|
317 | + $prefixes = $this->ldapHelper->getServerConfigurationPrefixes(); |
|
318 | + if(!in_array($configID, $prefixes, true)) { |
|
319 | + throw new OCSNotFoundException('Config ID not found'); |
|
320 | + } |
|
321 | + } |
|
322 | 322 | } |
@@ -41,182 +41,182 @@ |
||
41 | 41 | use OCA\User_LDAP\User\Manager; |
42 | 42 | |
43 | 43 | class UpdateGroups extends \OC\BackgroundJob\TimedJob { |
44 | - static private $groupsFromDB; |
|
45 | - |
|
46 | - static private $groupBE; |
|
47 | - |
|
48 | - public function __construct(){ |
|
49 | - $this->interval = self::getRefreshInterval(); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param mixed $argument |
|
54 | - */ |
|
55 | - public function run($argument){ |
|
56 | - self::updateGroups(); |
|
57 | - } |
|
58 | - |
|
59 | - static public function updateGroups() { |
|
60 | - \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG); |
|
61 | - |
|
62 | - $knownGroups = array_keys(self::getKnownGroups()); |
|
63 | - $actualGroups = self::getGroupBE()->getGroups(); |
|
64 | - |
|
65 | - if(empty($actualGroups) && empty($knownGroups)) { |
|
66 | - \OCP\Util::writeLog('user_ldap', |
|
67 | - 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.', |
|
68 | - \OCP\Util::INFO); |
|
69 | - return; |
|
70 | - } |
|
71 | - |
|
72 | - self::handleKnownGroups(array_intersect($actualGroups, $knownGroups)); |
|
73 | - self::handleCreatedGroups(array_diff($actualGroups, $knownGroups)); |
|
74 | - self::handleRemovedGroups(array_diff($knownGroups, $actualGroups)); |
|
75 | - |
|
76 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return int |
|
81 | - */ |
|
82 | - static private function getRefreshInterval() { |
|
83 | - //defaults to every hour |
|
84 | - return \OCP\Config::getAppValue('user_ldap', 'bgjRefreshInterval', 3600); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param string[] $groups |
|
89 | - */ |
|
90 | - static private function handleKnownGroups($groups) { |
|
91 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG); |
|
92 | - $query = \OCP\DB::prepare(' |
|
44 | + static private $groupsFromDB; |
|
45 | + |
|
46 | + static private $groupBE; |
|
47 | + |
|
48 | + public function __construct(){ |
|
49 | + $this->interval = self::getRefreshInterval(); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param mixed $argument |
|
54 | + */ |
|
55 | + public function run($argument){ |
|
56 | + self::updateGroups(); |
|
57 | + } |
|
58 | + |
|
59 | + static public function updateGroups() { |
|
60 | + \OCP\Util::writeLog('user_ldap', 'Run background job "updateGroups"', \OCP\Util::DEBUG); |
|
61 | + |
|
62 | + $knownGroups = array_keys(self::getKnownGroups()); |
|
63 | + $actualGroups = self::getGroupBE()->getGroups(); |
|
64 | + |
|
65 | + if(empty($actualGroups) && empty($knownGroups)) { |
|
66 | + \OCP\Util::writeLog('user_ldap', |
|
67 | + 'bgJ "updateGroups" – groups do not seem to be configured properly, aborting.', |
|
68 | + \OCP\Util::INFO); |
|
69 | + return; |
|
70 | + } |
|
71 | + |
|
72 | + self::handleKnownGroups(array_intersect($actualGroups, $knownGroups)); |
|
73 | + self::handleCreatedGroups(array_diff($actualGroups, $knownGroups)); |
|
74 | + self::handleRemovedGroups(array_diff($knownGroups, $actualGroups)); |
|
75 | + |
|
76 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Finished.', \OCP\Util::DEBUG); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return int |
|
81 | + */ |
|
82 | + static private function getRefreshInterval() { |
|
83 | + //defaults to every hour |
|
84 | + return \OCP\Config::getAppValue('user_ldap', 'bgjRefreshInterval', 3600); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param string[] $groups |
|
89 | + */ |
|
90 | + static private function handleKnownGroups($groups) { |
|
91 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – Dealing with known Groups.', \OCP\Util::DEBUG); |
|
92 | + $query = \OCP\DB::prepare(' |
|
93 | 93 | UPDATE `*PREFIX*ldap_group_members` |
94 | 94 | SET `owncloudusers` = ? |
95 | 95 | WHERE `owncloudname` = ? |
96 | 96 | '); |
97 | - foreach($groups as $group) { |
|
98 | - //we assume, that self::$groupsFromDB has been retrieved already |
|
99 | - $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']); |
|
100 | - $actualUsers = self::getGroupBE()->usersInGroup($group); |
|
101 | - $hasChanged = false; |
|
102 | - foreach(array_diff($knownUsers, $actualUsers) as $removedUser) { |
|
103 | - \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group)); |
|
104 | - \OCP\Util::writeLog('user_ldap', |
|
105 | - 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".', |
|
106 | - \OCP\Util::INFO); |
|
107 | - $hasChanged = true; |
|
108 | - } |
|
109 | - foreach(array_diff($actualUsers, $knownUsers) as $addedUser) { |
|
110 | - \OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group)); |
|
111 | - \OCP\Util::writeLog('user_ldap', |
|
112 | - 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".', |
|
113 | - \OCP\Util::INFO); |
|
114 | - $hasChanged = true; |
|
115 | - } |
|
116 | - if($hasChanged) { |
|
117 | - $query->execute(array(serialize($actualUsers), $group)); |
|
118 | - } |
|
119 | - } |
|
120 | - \OCP\Util::writeLog('user_ldap', |
|
121 | - 'bgJ "updateGroups" – FINISHED dealing with known Groups.', |
|
122 | - \OCP\Util::DEBUG); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string[] $createdGroups |
|
127 | - */ |
|
128 | - static private function handleCreatedGroups($createdGroups) { |
|
129 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG); |
|
130 | - $query = \OCP\DB::prepare(' |
|
97 | + foreach($groups as $group) { |
|
98 | + //we assume, that self::$groupsFromDB has been retrieved already |
|
99 | + $knownUsers = unserialize(self::$groupsFromDB[$group]['owncloudusers']); |
|
100 | + $actualUsers = self::getGroupBE()->usersInGroup($group); |
|
101 | + $hasChanged = false; |
|
102 | + foreach(array_diff($knownUsers, $actualUsers) as $removedUser) { |
|
103 | + \OCP\Util::emitHook('OC_User', 'post_removeFromGroup', array('uid' => $removedUser, 'gid' => $group)); |
|
104 | + \OCP\Util::writeLog('user_ldap', |
|
105 | + 'bgJ "updateGroups" – "'.$removedUser.'" removed from "'.$group.'".', |
|
106 | + \OCP\Util::INFO); |
|
107 | + $hasChanged = true; |
|
108 | + } |
|
109 | + foreach(array_diff($actualUsers, $knownUsers) as $addedUser) { |
|
110 | + \OCP\Util::emitHook('OC_User', 'post_addToGroup', array('uid' => $addedUser, 'gid' => $group)); |
|
111 | + \OCP\Util::writeLog('user_ldap', |
|
112 | + 'bgJ "updateGroups" – "'.$addedUser.'" added to "'.$group.'".', |
|
113 | + \OCP\Util::INFO); |
|
114 | + $hasChanged = true; |
|
115 | + } |
|
116 | + if($hasChanged) { |
|
117 | + $query->execute(array(serialize($actualUsers), $group)); |
|
118 | + } |
|
119 | + } |
|
120 | + \OCP\Util::writeLog('user_ldap', |
|
121 | + 'bgJ "updateGroups" – FINISHED dealing with known Groups.', |
|
122 | + \OCP\Util::DEBUG); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string[] $createdGroups |
|
127 | + */ |
|
128 | + static private function handleCreatedGroups($createdGroups) { |
|
129 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with created Groups.', \OCP\Util::DEBUG); |
|
130 | + $query = \OCP\DB::prepare(' |
|
131 | 131 | INSERT |
132 | 132 | INTO `*PREFIX*ldap_group_members` (`owncloudname`, `owncloudusers`) |
133 | 133 | VALUES (?, ?) |
134 | 134 | '); |
135 | - foreach($createdGroups as $createdGroup) { |
|
136 | - \OCP\Util::writeLog('user_ldap', |
|
137 | - 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.', |
|
138 | - \OCP\Util::INFO); |
|
139 | - $users = serialize(self::getGroupBE()->usersInGroup($createdGroup)); |
|
140 | - $query->execute(array($createdGroup, $users)); |
|
141 | - } |
|
142 | - \OCP\Util::writeLog('user_ldap', |
|
143 | - 'bgJ "updateGroups" – FINISHED dealing with created Groups.', |
|
144 | - \OCP\Util::DEBUG); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param string[] $removedGroups |
|
149 | - */ |
|
150 | - static private function handleRemovedGroups($removedGroups) { |
|
151 | - \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG); |
|
152 | - $query = \OCP\DB::prepare(' |
|
135 | + foreach($createdGroups as $createdGroup) { |
|
136 | + \OCP\Util::writeLog('user_ldap', |
|
137 | + 'bgJ "updateGroups" – new group "'.$createdGroup.'" found.', |
|
138 | + \OCP\Util::INFO); |
|
139 | + $users = serialize(self::getGroupBE()->usersInGroup($createdGroup)); |
|
140 | + $query->execute(array($createdGroup, $users)); |
|
141 | + } |
|
142 | + \OCP\Util::writeLog('user_ldap', |
|
143 | + 'bgJ "updateGroups" – FINISHED dealing with created Groups.', |
|
144 | + \OCP\Util::DEBUG); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param string[] $removedGroups |
|
149 | + */ |
|
150 | + static private function handleRemovedGroups($removedGroups) { |
|
151 | + \OCP\Util::writeLog('user_ldap', 'bgJ "updateGroups" – dealing with removed groups.', \OCP\Util::DEBUG); |
|
152 | + $query = \OCP\DB::prepare(' |
|
153 | 153 | DELETE |
154 | 154 | FROM `*PREFIX*ldap_group_members` |
155 | 155 | WHERE `owncloudname` = ? |
156 | 156 | '); |
157 | - foreach($removedGroups as $removedGroup) { |
|
158 | - \OCP\Util::writeLog('user_ldap', |
|
159 | - 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.', |
|
160 | - \OCP\Util::INFO); |
|
161 | - $query->execute(array($removedGroup)); |
|
162 | - } |
|
163 | - \OCP\Util::writeLog('user_ldap', |
|
164 | - 'bgJ "updateGroups" – FINISHED dealing with removed groups.', |
|
165 | - \OCP\Util::DEBUG); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy |
|
170 | - */ |
|
171 | - static private function getGroupBE() { |
|
172 | - if(!is_null(self::$groupBE)) { |
|
173 | - return self::$groupBE; |
|
174 | - } |
|
175 | - $helper = new Helper(\OC::$server->getConfig()); |
|
176 | - $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
177 | - $ldapWrapper = new LDAP(); |
|
178 | - if(count($configPrefixes) === 1) { |
|
179 | - //avoid the proxy when there is only one LDAP server configured |
|
180 | - $dbc = \OC::$server->getDatabaseConnection(); |
|
181 | - $userManager = new Manager( |
|
182 | - \OC::$server->getConfig(), |
|
183 | - new FilesystemHelper(), |
|
184 | - new LogWrapper(), |
|
185 | - \OC::$server->getAvatarManager(), |
|
186 | - new \OCP\Image(), |
|
187 | - $dbc, |
|
188 | - \OC::$server->getUserManager()); |
|
189 | - $connector = new Connection($ldapWrapper, $configPrefixes[0]); |
|
190 | - $ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper); |
|
191 | - $groupMapper = new GroupMapping($dbc); |
|
192 | - $userMapper = new UserMapping($dbc); |
|
193 | - $ldapAccess->setGroupMapper($groupMapper); |
|
194 | - $ldapAccess->setUserMapper($userMapper); |
|
195 | - self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess); |
|
196 | - } else { |
|
197 | - self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper); |
|
198 | - } |
|
199 | - |
|
200 | - return self::$groupBE; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @return array |
|
205 | - */ |
|
206 | - static private function getKnownGroups() { |
|
207 | - if(is_array(self::$groupsFromDB)) { |
|
208 | - return self::$groupsFromDB; |
|
209 | - } |
|
210 | - $query = \OCP\DB::prepare(' |
|
157 | + foreach($removedGroups as $removedGroup) { |
|
158 | + \OCP\Util::writeLog('user_ldap', |
|
159 | + 'bgJ "updateGroups" – group "'.$removedGroup.'" was removed.', |
|
160 | + \OCP\Util::INFO); |
|
161 | + $query->execute(array($removedGroup)); |
|
162 | + } |
|
163 | + \OCP\Util::writeLog('user_ldap', |
|
164 | + 'bgJ "updateGroups" – FINISHED dealing with removed groups.', |
|
165 | + \OCP\Util::DEBUG); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @return \OCA\User_LDAP\Group_LDAP|\OCA\User_LDAP\Group_Proxy |
|
170 | + */ |
|
171 | + static private function getGroupBE() { |
|
172 | + if(!is_null(self::$groupBE)) { |
|
173 | + return self::$groupBE; |
|
174 | + } |
|
175 | + $helper = new Helper(\OC::$server->getConfig()); |
|
176 | + $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
177 | + $ldapWrapper = new LDAP(); |
|
178 | + if(count($configPrefixes) === 1) { |
|
179 | + //avoid the proxy when there is only one LDAP server configured |
|
180 | + $dbc = \OC::$server->getDatabaseConnection(); |
|
181 | + $userManager = new Manager( |
|
182 | + \OC::$server->getConfig(), |
|
183 | + new FilesystemHelper(), |
|
184 | + new LogWrapper(), |
|
185 | + \OC::$server->getAvatarManager(), |
|
186 | + new \OCP\Image(), |
|
187 | + $dbc, |
|
188 | + \OC::$server->getUserManager()); |
|
189 | + $connector = new Connection($ldapWrapper, $configPrefixes[0]); |
|
190 | + $ldapAccess = new Access($connector, $ldapWrapper, $userManager, $helper); |
|
191 | + $groupMapper = new GroupMapping($dbc); |
|
192 | + $userMapper = new UserMapping($dbc); |
|
193 | + $ldapAccess->setGroupMapper($groupMapper); |
|
194 | + $ldapAccess->setUserMapper($userMapper); |
|
195 | + self::$groupBE = new \OCA\User_LDAP\Group_LDAP($ldapAccess); |
|
196 | + } else { |
|
197 | + self::$groupBE = new \OCA\User_LDAP\Group_Proxy($configPrefixes, $ldapWrapper); |
|
198 | + } |
|
199 | + |
|
200 | + return self::$groupBE; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @return array |
|
205 | + */ |
|
206 | + static private function getKnownGroups() { |
|
207 | + if(is_array(self::$groupsFromDB)) { |
|
208 | + return self::$groupsFromDB; |
|
209 | + } |
|
210 | + $query = \OCP\DB::prepare(' |
|
211 | 211 | SELECT `owncloudname`, `owncloudusers` |
212 | 212 | FROM `*PREFIX*ldap_group_members` |
213 | 213 | '); |
214 | - $result = $query->execute()->fetchAll(); |
|
215 | - self::$groupsFromDB = array(); |
|
216 | - foreach($result as $dataset) { |
|
217 | - self::$groupsFromDB[$dataset['owncloudname']] = $dataset; |
|
218 | - } |
|
219 | - |
|
220 | - return self::$groupsFromDB; |
|
221 | - } |
|
214 | + $result = $query->execute()->fetchAll(); |
|
215 | + self::$groupsFromDB = array(); |
|
216 | + foreach($result as $dataset) { |
|
217 | + self::$groupsFromDB[$dataset['owncloudname']] = $dataset; |
|
218 | + } |
|
219 | + |
|
220 | + return self::$groupsFromDB; |
|
221 | + } |
|
222 | 222 | } |
@@ -40,194 +40,194 @@ |
||
40 | 40 | * @package OCA\User_LDAP\Jobs; |
41 | 41 | */ |
42 | 42 | class CleanUp extends TimedJob { |
43 | - /** @var int $limit amount of users that should be checked per run */ |
|
44 | - protected $limit = 50; |
|
45 | - |
|
46 | - /** @var int $defaultIntervalMin default interval in minutes */ |
|
47 | - protected $defaultIntervalMin = 51; |
|
48 | - |
|
49 | - /** @var User_LDAP|User_Proxy $userBackend */ |
|
50 | - protected $userBackend; |
|
51 | - |
|
52 | - /** @var \OCP\IConfig $ocConfig */ |
|
53 | - protected $ocConfig; |
|
54 | - |
|
55 | - /** @var \OCP\IDBConnection $db */ |
|
56 | - protected $db; |
|
57 | - |
|
58 | - /** @var Helper $ldapHelper */ |
|
59 | - protected $ldapHelper; |
|
60 | - |
|
61 | - /** @var \OCA\User_LDAP\Mapping\UserMapping */ |
|
62 | - protected $mapping; |
|
63 | - |
|
64 | - /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ |
|
65 | - protected $dui; |
|
66 | - |
|
67 | - public function __construct() { |
|
68 | - $minutes = \OC::$server->getConfig()->getSystemValue( |
|
69 | - 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); |
|
70 | - $this->setInterval(intval($minutes) * 60); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * assigns the instances passed to run() to the class properties |
|
75 | - * @param array $arguments |
|
76 | - */ |
|
77 | - public function setArguments($arguments) { |
|
78 | - //Dependency Injection is not possible, because the constructor will |
|
79 | - //only get values that are serialized to JSON. I.e. whatever we would |
|
80 | - //pass in app.php we do add here, except something else is passed e.g. |
|
81 | - //in tests. |
|
82 | - |
|
83 | - if(isset($arguments['helper'])) { |
|
84 | - $this->ldapHelper = $arguments['helper']; |
|
85 | - } else { |
|
86 | - $this->ldapHelper = new Helper(\OC::$server->getConfig()); |
|
87 | - } |
|
88 | - |
|
89 | - if(isset($arguments['ocConfig'])) { |
|
90 | - $this->ocConfig = $arguments['ocConfig']; |
|
91 | - } else { |
|
92 | - $this->ocConfig = \OC::$server->getConfig(); |
|
93 | - } |
|
94 | - |
|
95 | - if(isset($arguments['userBackend'])) { |
|
96 | - $this->userBackend = $arguments['userBackend']; |
|
97 | - } else { |
|
98 | - $this->userBackend = new User_Proxy( |
|
99 | - $this->ldapHelper->getServerConfigurationPrefixes(true), |
|
100 | - new LDAP(), |
|
101 | - $this->ocConfig |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - if(isset($arguments['db'])) { |
|
106 | - $this->db = $arguments['db']; |
|
107 | - } else { |
|
108 | - $this->db = \OC::$server->getDatabaseConnection(); |
|
109 | - } |
|
110 | - |
|
111 | - if(isset($arguments['mapping'])) { |
|
112 | - $this->mapping = $arguments['mapping']; |
|
113 | - } else { |
|
114 | - $this->mapping = new UserMapping($this->db); |
|
115 | - } |
|
116 | - |
|
117 | - if(isset($arguments['deletedUsersIndex'])) { |
|
118 | - $this->dui = $arguments['deletedUsersIndex']; |
|
119 | - } else { |
|
120 | - $this->dui = new DeletedUsersIndex( |
|
121 | - $this->ocConfig, $this->db, $this->mapping); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * makes the background job do its work |
|
127 | - * @param array $argument |
|
128 | - */ |
|
129 | - public function run($argument) { |
|
130 | - $this->setArguments($argument); |
|
131 | - |
|
132 | - if(!$this->isCleanUpAllowed()) { |
|
133 | - return; |
|
134 | - } |
|
135 | - $users = $this->mapping->getList($this->getOffset(), $this->limit); |
|
136 | - if(!is_array($users)) { |
|
137 | - //something wrong? Let's start from the beginning next time and |
|
138 | - //abort |
|
139 | - $this->setOffset(true); |
|
140 | - return; |
|
141 | - } |
|
142 | - $resetOffset = $this->isOffsetResetNecessary(count($users)); |
|
143 | - $this->checkUsers($users); |
|
144 | - $this->setOffset($resetOffset); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * checks whether next run should start at 0 again |
|
149 | - * @param int $resultCount |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - public function isOffsetResetNecessary($resultCount) { |
|
153 | - return ($resultCount < $this->limit) ? true : false; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * checks whether cleaning up LDAP users is allowed |
|
158 | - * @return bool |
|
159 | - */ |
|
160 | - public function isCleanUpAllowed() { |
|
161 | - try { |
|
162 | - if($this->ldapHelper->haveDisabledConfigurations()) { |
|
163 | - return false; |
|
164 | - } |
|
165 | - } catch (\Exception $e) { |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - $enabled = $this->isCleanUpEnabled(); |
|
170 | - |
|
171 | - return $enabled; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * checks whether clean up is enabled by configuration |
|
176 | - * @return bool |
|
177 | - */ |
|
178 | - private function isCleanUpEnabled() { |
|
179 | - return (bool)$this->ocConfig->getSystemValue( |
|
180 | - 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * checks users whether they are still existing |
|
185 | - * @param array $users result from getMappedUsers() |
|
186 | - */ |
|
187 | - private function checkUsers(array $users) { |
|
188 | - foreach($users as $user) { |
|
189 | - $this->checkUser($user); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * checks whether a user is still existing in LDAP |
|
195 | - * @param string[] $user |
|
196 | - */ |
|
197 | - private function checkUser(array $user) { |
|
198 | - if($this->userBackend->userExistsOnLDAP($user['name'])) { |
|
199 | - //still available, all good |
|
200 | - |
|
201 | - return; |
|
202 | - } |
|
203 | - |
|
204 | - $this->dui->markUser($user['name']); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * gets the offset to fetch users from the mappings table |
|
209 | - * @return int |
|
210 | - */ |
|
211 | - private function getOffset() { |
|
212 | - return intval($this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0)); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * sets the new offset for the next run |
|
217 | - * @param bool $reset whether the offset should be set to 0 |
|
218 | - */ |
|
219 | - public function setOffset($reset = false) { |
|
220 | - $newOffset = $reset ? 0 : |
|
221 | - $this->getOffset() + $this->limit; |
|
222 | - $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * returns the chunk size (limit in DB speak) |
|
227 | - * @return int |
|
228 | - */ |
|
229 | - public function getChunkSize() { |
|
230 | - return $this->limit; |
|
231 | - } |
|
43 | + /** @var int $limit amount of users that should be checked per run */ |
|
44 | + protected $limit = 50; |
|
45 | + |
|
46 | + /** @var int $defaultIntervalMin default interval in minutes */ |
|
47 | + protected $defaultIntervalMin = 51; |
|
48 | + |
|
49 | + /** @var User_LDAP|User_Proxy $userBackend */ |
|
50 | + protected $userBackend; |
|
51 | + |
|
52 | + /** @var \OCP\IConfig $ocConfig */ |
|
53 | + protected $ocConfig; |
|
54 | + |
|
55 | + /** @var \OCP\IDBConnection $db */ |
|
56 | + protected $db; |
|
57 | + |
|
58 | + /** @var Helper $ldapHelper */ |
|
59 | + protected $ldapHelper; |
|
60 | + |
|
61 | + /** @var \OCA\User_LDAP\Mapping\UserMapping */ |
|
62 | + protected $mapping; |
|
63 | + |
|
64 | + /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ |
|
65 | + protected $dui; |
|
66 | + |
|
67 | + public function __construct() { |
|
68 | + $minutes = \OC::$server->getConfig()->getSystemValue( |
|
69 | + 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); |
|
70 | + $this->setInterval(intval($minutes) * 60); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * assigns the instances passed to run() to the class properties |
|
75 | + * @param array $arguments |
|
76 | + */ |
|
77 | + public function setArguments($arguments) { |
|
78 | + //Dependency Injection is not possible, because the constructor will |
|
79 | + //only get values that are serialized to JSON. I.e. whatever we would |
|
80 | + //pass in app.php we do add here, except something else is passed e.g. |
|
81 | + //in tests. |
|
82 | + |
|
83 | + if(isset($arguments['helper'])) { |
|
84 | + $this->ldapHelper = $arguments['helper']; |
|
85 | + } else { |
|
86 | + $this->ldapHelper = new Helper(\OC::$server->getConfig()); |
|
87 | + } |
|
88 | + |
|
89 | + if(isset($arguments['ocConfig'])) { |
|
90 | + $this->ocConfig = $arguments['ocConfig']; |
|
91 | + } else { |
|
92 | + $this->ocConfig = \OC::$server->getConfig(); |
|
93 | + } |
|
94 | + |
|
95 | + if(isset($arguments['userBackend'])) { |
|
96 | + $this->userBackend = $arguments['userBackend']; |
|
97 | + } else { |
|
98 | + $this->userBackend = new User_Proxy( |
|
99 | + $this->ldapHelper->getServerConfigurationPrefixes(true), |
|
100 | + new LDAP(), |
|
101 | + $this->ocConfig |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + if(isset($arguments['db'])) { |
|
106 | + $this->db = $arguments['db']; |
|
107 | + } else { |
|
108 | + $this->db = \OC::$server->getDatabaseConnection(); |
|
109 | + } |
|
110 | + |
|
111 | + if(isset($arguments['mapping'])) { |
|
112 | + $this->mapping = $arguments['mapping']; |
|
113 | + } else { |
|
114 | + $this->mapping = new UserMapping($this->db); |
|
115 | + } |
|
116 | + |
|
117 | + if(isset($arguments['deletedUsersIndex'])) { |
|
118 | + $this->dui = $arguments['deletedUsersIndex']; |
|
119 | + } else { |
|
120 | + $this->dui = new DeletedUsersIndex( |
|
121 | + $this->ocConfig, $this->db, $this->mapping); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * makes the background job do its work |
|
127 | + * @param array $argument |
|
128 | + */ |
|
129 | + public function run($argument) { |
|
130 | + $this->setArguments($argument); |
|
131 | + |
|
132 | + if(!$this->isCleanUpAllowed()) { |
|
133 | + return; |
|
134 | + } |
|
135 | + $users = $this->mapping->getList($this->getOffset(), $this->limit); |
|
136 | + if(!is_array($users)) { |
|
137 | + //something wrong? Let's start from the beginning next time and |
|
138 | + //abort |
|
139 | + $this->setOffset(true); |
|
140 | + return; |
|
141 | + } |
|
142 | + $resetOffset = $this->isOffsetResetNecessary(count($users)); |
|
143 | + $this->checkUsers($users); |
|
144 | + $this->setOffset($resetOffset); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * checks whether next run should start at 0 again |
|
149 | + * @param int $resultCount |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + public function isOffsetResetNecessary($resultCount) { |
|
153 | + return ($resultCount < $this->limit) ? true : false; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * checks whether cleaning up LDAP users is allowed |
|
158 | + * @return bool |
|
159 | + */ |
|
160 | + public function isCleanUpAllowed() { |
|
161 | + try { |
|
162 | + if($this->ldapHelper->haveDisabledConfigurations()) { |
|
163 | + return false; |
|
164 | + } |
|
165 | + } catch (\Exception $e) { |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + $enabled = $this->isCleanUpEnabled(); |
|
170 | + |
|
171 | + return $enabled; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * checks whether clean up is enabled by configuration |
|
176 | + * @return bool |
|
177 | + */ |
|
178 | + private function isCleanUpEnabled() { |
|
179 | + return (bool)$this->ocConfig->getSystemValue( |
|
180 | + 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * checks users whether they are still existing |
|
185 | + * @param array $users result from getMappedUsers() |
|
186 | + */ |
|
187 | + private function checkUsers(array $users) { |
|
188 | + foreach($users as $user) { |
|
189 | + $this->checkUser($user); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * checks whether a user is still existing in LDAP |
|
195 | + * @param string[] $user |
|
196 | + */ |
|
197 | + private function checkUser(array $user) { |
|
198 | + if($this->userBackend->userExistsOnLDAP($user['name'])) { |
|
199 | + //still available, all good |
|
200 | + |
|
201 | + return; |
|
202 | + } |
|
203 | + |
|
204 | + $this->dui->markUser($user['name']); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * gets the offset to fetch users from the mappings table |
|
209 | + * @return int |
|
210 | + */ |
|
211 | + private function getOffset() { |
|
212 | + return intval($this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0)); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * sets the new offset for the next run |
|
217 | + * @param bool $reset whether the offset should be set to 0 |
|
218 | + */ |
|
219 | + public function setOffset($reset = false) { |
|
220 | + $newOffset = $reset ? 0 : |
|
221 | + $this->getOffset() + $this->limit; |
|
222 | + $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * returns the chunk size (limit in DB speak) |
|
227 | + * @return int |
|
228 | + */ |
|
229 | + public function getChunkSize() { |
|
230 | + return $this->limit; |
|
231 | + } |
|
232 | 232 | |
233 | 233 | } |
@@ -27,13 +27,13 @@ |
||
27 | 27 | |
28 | 28 | |
29 | 29 | abstract class BackendUtility { |
30 | - protected $access; |
|
30 | + protected $access; |
|
31 | 31 | |
32 | - /** |
|
33 | - * constructor, make sure the subclasses call this one! |
|
34 | - * @param Access $access an instance of Access for LDAP interaction |
|
35 | - */ |
|
36 | - public function __construct(Access $access) { |
|
37 | - $this->access = $access; |
|
38 | - } |
|
32 | + /** |
|
33 | + * constructor, make sure the subclasses call this one! |
|
34 | + * @param Access $access an instance of Access for LDAP interaction |
|
35 | + */ |
|
36 | + public function __construct(Access $access) { |
|
37 | + $this->access = $access; |
|
38 | + } |
|
39 | 39 | } |
@@ -28,52 +28,52 @@ |
||
28 | 28 | namespace OCA\User_LDAP; |
29 | 29 | |
30 | 30 | class WizardResult { |
31 | - protected $changes = array(); |
|
32 | - protected $options = array(); |
|
33 | - protected $markedChange = false; |
|
31 | + protected $changes = array(); |
|
32 | + protected $options = array(); |
|
33 | + protected $markedChange = false; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string $key |
|
37 | - * @param mixed $value |
|
38 | - */ |
|
39 | - public function addChange($key, $value) { |
|
40 | - $this->changes[$key] = $value; |
|
41 | - } |
|
35 | + /** |
|
36 | + * @param string $key |
|
37 | + * @param mixed $value |
|
38 | + */ |
|
39 | + public function addChange($key, $value) { |
|
40 | + $this->changes[$key] = $value; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * |
|
45 | - */ |
|
46 | - public function markChange() { |
|
47 | - $this->markedChange = true; |
|
48 | - } |
|
43 | + /** |
|
44 | + * |
|
45 | + */ |
|
46 | + public function markChange() { |
|
47 | + $this->markedChange = true; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $key |
|
52 | - * @param array|string $values |
|
53 | - */ |
|
54 | - public function addOptions($key, $values) { |
|
55 | - if(!is_array($values)) { |
|
56 | - $values = array($values); |
|
57 | - } |
|
58 | - $this->options[$key] = $values; |
|
59 | - } |
|
50 | + /** |
|
51 | + * @param string $key |
|
52 | + * @param array|string $values |
|
53 | + */ |
|
54 | + public function addOptions($key, $values) { |
|
55 | + if(!is_array($values)) { |
|
56 | + $values = array($values); |
|
57 | + } |
|
58 | + $this->options[$key] = $values; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @return bool |
|
63 | - */ |
|
64 | - public function hasChanges() { |
|
65 | - return (count($this->changes) > 0 || $this->markedChange); |
|
66 | - } |
|
61 | + /** |
|
62 | + * @return bool |
|
63 | + */ |
|
64 | + public function hasChanges() { |
|
65 | + return (count($this->changes) > 0 || $this->markedChange); |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @return array |
|
70 | - */ |
|
71 | - public function getResultArray() { |
|
72 | - $result = array(); |
|
73 | - $result['changes'] = $this->changes; |
|
74 | - if(count($this->options) > 0) { |
|
75 | - $result['options'] = $this->options; |
|
76 | - } |
|
77 | - return $result; |
|
78 | - } |
|
68 | + /** |
|
69 | + * @return array |
|
70 | + */ |
|
71 | + public function getResultArray() { |
|
72 | + $result = array(); |
|
73 | + $result['changes'] = $this->changes; |
|
74 | + if(count($this->options) > 0) { |
|
75 | + $result['options'] = $this->options; |
|
76 | + } |
|
77 | + return $result; |
|
78 | + } |
|
79 | 79 | } |
@@ -34,126 +34,126 @@ discard block |
||
34 | 34 | |
35 | 35 | class Helper { |
36 | 36 | |
37 | - /** @var IConfig */ |
|
38 | - private $config; |
|
39 | - |
|
40 | - /** |
|
41 | - * Helper constructor. |
|
42 | - * |
|
43 | - * @param IConfig $config |
|
44 | - */ |
|
45 | - public function __construct(IConfig $config) { |
|
46 | - $this->config = $config; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * returns prefixes for each saved LDAP/AD server configuration. |
|
51 | - * @param bool $activeConfigurations optional, whether only active configuration shall be |
|
52 | - * retrieved, defaults to false |
|
53 | - * @return array with a list of the available prefixes |
|
54 | - * |
|
55 | - * Configuration prefixes are used to set up configurations for n LDAP or |
|
56 | - * AD servers. Since configuration is stored in the database, table |
|
57 | - * appconfig under appid user_ldap, the common identifiers in column |
|
58 | - * 'configkey' have a prefix. The prefix for the very first server |
|
59 | - * configuration is empty. |
|
60 | - * Configkey Examples: |
|
61 | - * Server 1: ldap_login_filter |
|
62 | - * Server 2: s1_ldap_login_filter |
|
63 | - * Server 3: s2_ldap_login_filter |
|
64 | - * |
|
65 | - * The prefix needs to be passed to the constructor of Connection class, |
|
66 | - * except the default (first) server shall be connected to. |
|
67 | - * |
|
68 | - */ |
|
69 | - public function getServerConfigurationPrefixes($activeConfigurations = false) { |
|
70 | - $referenceConfigkey = 'ldap_configuration_active'; |
|
71 | - |
|
72 | - $keys = $this->getServersConfig($referenceConfigkey); |
|
73 | - |
|
74 | - $prefixes = []; |
|
75 | - foreach ($keys as $key) { |
|
76 | - if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { |
|
77 | - continue; |
|
78 | - } |
|
79 | - |
|
80 | - $len = strlen($key) - strlen($referenceConfigkey); |
|
81 | - $prefixes[] = substr($key, 0, $len); |
|
82 | - } |
|
83 | - |
|
84 | - return $prefixes; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * |
|
89 | - * determines the host for every configured connection |
|
90 | - * @return array an array with configprefix as keys |
|
91 | - * |
|
92 | - */ |
|
93 | - public function getServerConfigurationHosts() { |
|
94 | - $referenceConfigkey = 'ldap_host'; |
|
95 | - |
|
96 | - $keys = $this->getServersConfig($referenceConfigkey); |
|
97 | - |
|
98 | - $result = array(); |
|
99 | - foreach($keys as $key) { |
|
100 | - $len = strlen($key) - strlen($referenceConfigkey); |
|
101 | - $prefix = substr($key, 0, $len); |
|
102 | - $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
|
103 | - } |
|
104 | - |
|
105 | - return $result; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * return the next available configuration prefix |
|
110 | - * |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function getNextServerConfigurationPrefix() { |
|
114 | - $serverConnections = $this->getServerConfigurationPrefixes(); |
|
115 | - |
|
116 | - if(count($serverConnections) === 0) { |
|
117 | - return 's01'; |
|
118 | - } |
|
119 | - |
|
120 | - sort($serverConnections); |
|
121 | - $lastKey = array_pop($serverConnections); |
|
122 | - $lastNumber = intval(str_replace('s', '', $lastKey)); |
|
123 | - $nextPrefix = 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
|
124 | - return $nextPrefix; |
|
125 | - } |
|
126 | - |
|
127 | - private function getServersConfig($value) { |
|
128 | - $regex = '/' . $value . '$/S'; |
|
129 | - |
|
130 | - $keys = $this->config->getAppKeys('user_ldap'); |
|
131 | - $result = []; |
|
132 | - foreach ($keys as $key) { |
|
133 | - if (preg_match($regex, $key) === 1) { |
|
134 | - $result[] = $key; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return $result; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * deletes a given saved LDAP/AD server configuration. |
|
143 | - * @param string $prefix the configuration prefix of the config to delete |
|
144 | - * @return bool true on success, false otherwise |
|
145 | - */ |
|
146 | - public function deleteServerConfiguration($prefix) { |
|
147 | - if(!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
148 | - return false; |
|
149 | - } |
|
150 | - |
|
151 | - $saveOtherConfigurations = ''; |
|
152 | - if(empty($prefix)) { |
|
153 | - $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\''; |
|
154 | - } |
|
155 | - |
|
156 | - $query = \OCP\DB::prepare(' |
|
37 | + /** @var IConfig */ |
|
38 | + private $config; |
|
39 | + |
|
40 | + /** |
|
41 | + * Helper constructor. |
|
42 | + * |
|
43 | + * @param IConfig $config |
|
44 | + */ |
|
45 | + public function __construct(IConfig $config) { |
|
46 | + $this->config = $config; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * returns prefixes for each saved LDAP/AD server configuration. |
|
51 | + * @param bool $activeConfigurations optional, whether only active configuration shall be |
|
52 | + * retrieved, defaults to false |
|
53 | + * @return array with a list of the available prefixes |
|
54 | + * |
|
55 | + * Configuration prefixes are used to set up configurations for n LDAP or |
|
56 | + * AD servers. Since configuration is stored in the database, table |
|
57 | + * appconfig under appid user_ldap, the common identifiers in column |
|
58 | + * 'configkey' have a prefix. The prefix for the very first server |
|
59 | + * configuration is empty. |
|
60 | + * Configkey Examples: |
|
61 | + * Server 1: ldap_login_filter |
|
62 | + * Server 2: s1_ldap_login_filter |
|
63 | + * Server 3: s2_ldap_login_filter |
|
64 | + * |
|
65 | + * The prefix needs to be passed to the constructor of Connection class, |
|
66 | + * except the default (first) server shall be connected to. |
|
67 | + * |
|
68 | + */ |
|
69 | + public function getServerConfigurationPrefixes($activeConfigurations = false) { |
|
70 | + $referenceConfigkey = 'ldap_configuration_active'; |
|
71 | + |
|
72 | + $keys = $this->getServersConfig($referenceConfigkey); |
|
73 | + |
|
74 | + $prefixes = []; |
|
75 | + foreach ($keys as $key) { |
|
76 | + if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { |
|
77 | + continue; |
|
78 | + } |
|
79 | + |
|
80 | + $len = strlen($key) - strlen($referenceConfigkey); |
|
81 | + $prefixes[] = substr($key, 0, $len); |
|
82 | + } |
|
83 | + |
|
84 | + return $prefixes; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * |
|
89 | + * determines the host for every configured connection |
|
90 | + * @return array an array with configprefix as keys |
|
91 | + * |
|
92 | + */ |
|
93 | + public function getServerConfigurationHosts() { |
|
94 | + $referenceConfigkey = 'ldap_host'; |
|
95 | + |
|
96 | + $keys = $this->getServersConfig($referenceConfigkey); |
|
97 | + |
|
98 | + $result = array(); |
|
99 | + foreach($keys as $key) { |
|
100 | + $len = strlen($key) - strlen($referenceConfigkey); |
|
101 | + $prefix = substr($key, 0, $len); |
|
102 | + $result[$prefix] = $this->config->getAppValue('user_ldap', $key); |
|
103 | + } |
|
104 | + |
|
105 | + return $result; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * return the next available configuration prefix |
|
110 | + * |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function getNextServerConfigurationPrefix() { |
|
114 | + $serverConnections = $this->getServerConfigurationPrefixes(); |
|
115 | + |
|
116 | + if(count($serverConnections) === 0) { |
|
117 | + return 's01'; |
|
118 | + } |
|
119 | + |
|
120 | + sort($serverConnections); |
|
121 | + $lastKey = array_pop($serverConnections); |
|
122 | + $lastNumber = intval(str_replace('s', '', $lastKey)); |
|
123 | + $nextPrefix = 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT); |
|
124 | + return $nextPrefix; |
|
125 | + } |
|
126 | + |
|
127 | + private function getServersConfig($value) { |
|
128 | + $regex = '/' . $value . '$/S'; |
|
129 | + |
|
130 | + $keys = $this->config->getAppKeys('user_ldap'); |
|
131 | + $result = []; |
|
132 | + foreach ($keys as $key) { |
|
133 | + if (preg_match($regex, $key) === 1) { |
|
134 | + $result[] = $key; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return $result; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * deletes a given saved LDAP/AD server configuration. |
|
143 | + * @param string $prefix the configuration prefix of the config to delete |
|
144 | + * @return bool true on success, false otherwise |
|
145 | + */ |
|
146 | + public function deleteServerConfiguration($prefix) { |
|
147 | + if(!in_array($prefix, self::getServerConfigurationPrefixes())) { |
|
148 | + return false; |
|
149 | + } |
|
150 | + |
|
151 | + $saveOtherConfigurations = ''; |
|
152 | + if(empty($prefix)) { |
|
153 | + $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\''; |
|
154 | + } |
|
155 | + |
|
156 | + $query = \OCP\DB::prepare(' |
|
157 | 157 | DELETE |
158 | 158 | FROM `*PREFIX*appconfig` |
159 | 159 | WHERE `configkey` LIKE ? |
@@ -161,145 +161,145 @@ discard block |
||
161 | 161 | AND `appid` = \'user_ldap\' |
162 | 162 | AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\') |
163 | 163 | '); |
164 | - $delRows = $query->execute(array($prefix.'%')); |
|
165 | - |
|
166 | - if(\OCP\DB::isError($delRows)) { |
|
167 | - return false; |
|
168 | - } |
|
169 | - |
|
170 | - if($delRows === 0) { |
|
171 | - return false; |
|
172 | - } |
|
173 | - |
|
174 | - return true; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * checks whether there is one or more disabled LDAP configurations |
|
179 | - * @throws \Exception |
|
180 | - * @return bool |
|
181 | - */ |
|
182 | - public function haveDisabledConfigurations() { |
|
183 | - $all = $this->getServerConfigurationPrefixes(false); |
|
184 | - $active = $this->getServerConfigurationPrefixes(true); |
|
185 | - |
|
186 | - if(!is_array($all) || !is_array($active)) { |
|
187 | - throw new \Exception('Unexpected Return Value'); |
|
188 | - } |
|
189 | - |
|
190 | - return count($all) !== count($active) || count($all) === 0; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * extracts the domain from a given URL |
|
195 | - * @param string $url the URL |
|
196 | - * @return string|false domain as string on success, false otherwise |
|
197 | - */ |
|
198 | - public function getDomainFromURL($url) { |
|
199 | - $uinfo = parse_url($url); |
|
200 | - if(!is_array($uinfo)) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - $domain = false; |
|
205 | - if(isset($uinfo['host'])) { |
|
206 | - $domain = $uinfo['host']; |
|
207 | - } else if(isset($uinfo['path'])) { |
|
208 | - $domain = $uinfo['path']; |
|
209 | - } |
|
210 | - |
|
211 | - return $domain; |
|
212 | - } |
|
164 | + $delRows = $query->execute(array($prefix.'%')); |
|
165 | + |
|
166 | + if(\OCP\DB::isError($delRows)) { |
|
167 | + return false; |
|
168 | + } |
|
169 | + |
|
170 | + if($delRows === 0) { |
|
171 | + return false; |
|
172 | + } |
|
173 | + |
|
174 | + return true; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * checks whether there is one or more disabled LDAP configurations |
|
179 | + * @throws \Exception |
|
180 | + * @return bool |
|
181 | + */ |
|
182 | + public function haveDisabledConfigurations() { |
|
183 | + $all = $this->getServerConfigurationPrefixes(false); |
|
184 | + $active = $this->getServerConfigurationPrefixes(true); |
|
185 | + |
|
186 | + if(!is_array($all) || !is_array($active)) { |
|
187 | + throw new \Exception('Unexpected Return Value'); |
|
188 | + } |
|
189 | + |
|
190 | + return count($all) !== count($active) || count($all) === 0; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * extracts the domain from a given URL |
|
195 | + * @param string $url the URL |
|
196 | + * @return string|false domain as string on success, false otherwise |
|
197 | + */ |
|
198 | + public function getDomainFromURL($url) { |
|
199 | + $uinfo = parse_url($url); |
|
200 | + if(!is_array($uinfo)) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + $domain = false; |
|
205 | + if(isset($uinfo['host'])) { |
|
206 | + $domain = $uinfo['host']; |
|
207 | + } else if(isset($uinfo['path'])) { |
|
208 | + $domain = $uinfo['path']; |
|
209 | + } |
|
210 | + |
|
211 | + return $domain; |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * |
|
216 | - * Set the LDAPProvider in the config |
|
217 | - * |
|
218 | - */ |
|
219 | - public function setLDAPProvider() { |
|
220 | - $current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); |
|
221 | - if(is_null($current)) { |
|
222 | - \OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); |
|
223 | - } |
|
224 | - } |
|
214 | + /** |
|
215 | + * |
|
216 | + * Set the LDAPProvider in the config |
|
217 | + * |
|
218 | + */ |
|
219 | + public function setLDAPProvider() { |
|
220 | + $current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); |
|
221 | + if(is_null($current)) { |
|
222 | + \OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); |
|
223 | + } |
|
224 | + } |
|
225 | 225 | |
226 | - /** |
|
227 | - * sanitizes a DN received from the LDAP server |
|
228 | - * @param array $dn the DN in question |
|
229 | - * @return array the sanitized DN |
|
230 | - */ |
|
231 | - public function sanitizeDN($dn) { |
|
232 | - //treating multiple base DNs |
|
233 | - if(is_array($dn)) { |
|
234 | - $result = array(); |
|
235 | - foreach($dn as $singleDN) { |
|
236 | - $result[] = $this->sanitizeDN($singleDN); |
|
237 | - } |
|
238 | - return $result; |
|
239 | - } |
|
240 | - |
|
241 | - //OID sometimes gives back DNs with whitespace after the comma |
|
242 | - // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
|
243 | - $dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
244 | - |
|
245 | - //make comparisons and everything work |
|
246 | - $dn = mb_strtolower($dn, 'UTF-8'); |
|
247 | - |
|
248 | - //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
|
249 | - //to use the DN in search filters, \ needs to be escaped to \5c additionally |
|
250 | - //to use them in bases, we convert them back to simple backslashes in readAttribute() |
|
251 | - $replacements = array( |
|
252 | - '\,' => '\5c2C', |
|
253 | - '\=' => '\5c3D', |
|
254 | - '\+' => '\5c2B', |
|
255 | - '\<' => '\5c3C', |
|
256 | - '\>' => '\5c3E', |
|
257 | - '\;' => '\5c3B', |
|
258 | - '\"' => '\5c22', |
|
259 | - '\#' => '\5c23', |
|
260 | - '(' => '\28', |
|
261 | - ')' => '\29', |
|
262 | - '*' => '\2A', |
|
263 | - ); |
|
264 | - $dn = str_replace(array_keys($replacements), array_values($replacements), $dn); |
|
265 | - |
|
266 | - return $dn; |
|
267 | - } |
|
226 | + /** |
|
227 | + * sanitizes a DN received from the LDAP server |
|
228 | + * @param array $dn the DN in question |
|
229 | + * @return array the sanitized DN |
|
230 | + */ |
|
231 | + public function sanitizeDN($dn) { |
|
232 | + //treating multiple base DNs |
|
233 | + if(is_array($dn)) { |
|
234 | + $result = array(); |
|
235 | + foreach($dn as $singleDN) { |
|
236 | + $result[] = $this->sanitizeDN($singleDN); |
|
237 | + } |
|
238 | + return $result; |
|
239 | + } |
|
240 | + |
|
241 | + //OID sometimes gives back DNs with whitespace after the comma |
|
242 | + // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
|
243 | + $dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
244 | + |
|
245 | + //make comparisons and everything work |
|
246 | + $dn = mb_strtolower($dn, 'UTF-8'); |
|
247 | + |
|
248 | + //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
|
249 | + //to use the DN in search filters, \ needs to be escaped to \5c additionally |
|
250 | + //to use them in bases, we convert them back to simple backslashes in readAttribute() |
|
251 | + $replacements = array( |
|
252 | + '\,' => '\5c2C', |
|
253 | + '\=' => '\5c3D', |
|
254 | + '\+' => '\5c2B', |
|
255 | + '\<' => '\5c3C', |
|
256 | + '\>' => '\5c3E', |
|
257 | + '\;' => '\5c3B', |
|
258 | + '\"' => '\5c22', |
|
259 | + '\#' => '\5c23', |
|
260 | + '(' => '\28', |
|
261 | + ')' => '\29', |
|
262 | + '*' => '\2A', |
|
263 | + ); |
|
264 | + $dn = str_replace(array_keys($replacements), array_values($replacements), $dn); |
|
265 | + |
|
266 | + return $dn; |
|
267 | + } |
|
268 | 268 | |
269 | - /** |
|
270 | - * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
|
271 | - * @param string $dn the DN |
|
272 | - * @return string |
|
273 | - */ |
|
274 | - public function DNasBaseParameter($dn) { |
|
275 | - return str_ireplace('\\5c', '\\', $dn); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * listens to a hook thrown by server2server sharing and replaces the given |
|
280 | - * login name by a username, if it matches an LDAP user. |
|
281 | - * |
|
282 | - * @param array $param |
|
283 | - * @throws \Exception |
|
284 | - */ |
|
285 | - public static function loginName2UserName($param) { |
|
286 | - if(!isset($param['uid'])) { |
|
287 | - throw new \Exception('key uid is expected to be set in $param'); |
|
288 | - } |
|
289 | - |
|
290 | - //ain't it ironic? |
|
291 | - $helper = new Helper(\OC::$server->getConfig()); |
|
292 | - |
|
293 | - $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
294 | - $ldapWrapper = new LDAP(); |
|
295 | - $ocConfig = \OC::$server->getConfig(); |
|
296 | - |
|
297 | - $userBackend = new User_Proxy( |
|
298 | - $configPrefixes, $ldapWrapper, $ocConfig |
|
299 | - ); |
|
300 | - $uid = $userBackend->loginName2UserName($param['uid'] ); |
|
301 | - if($uid !== false) { |
|
302 | - $param['uid'] = $uid; |
|
303 | - } |
|
304 | - } |
|
269 | + /** |
|
270 | + * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
|
271 | + * @param string $dn the DN |
|
272 | + * @return string |
|
273 | + */ |
|
274 | + public function DNasBaseParameter($dn) { |
|
275 | + return str_ireplace('\\5c', '\\', $dn); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * listens to a hook thrown by server2server sharing and replaces the given |
|
280 | + * login name by a username, if it matches an LDAP user. |
|
281 | + * |
|
282 | + * @param array $param |
|
283 | + * @throws \Exception |
|
284 | + */ |
|
285 | + public static function loginName2UserName($param) { |
|
286 | + if(!isset($param['uid'])) { |
|
287 | + throw new \Exception('key uid is expected to be set in $param'); |
|
288 | + } |
|
289 | + |
|
290 | + //ain't it ironic? |
|
291 | + $helper = new Helper(\OC::$server->getConfig()); |
|
292 | + |
|
293 | + $configPrefixes = $helper->getServerConfigurationPrefixes(true); |
|
294 | + $ldapWrapper = new LDAP(); |
|
295 | + $ocConfig = \OC::$server->getConfig(); |
|
296 | + |
|
297 | + $userBackend = new User_Proxy( |
|
298 | + $configPrefixes, $ldapWrapper, $ocConfig |
|
299 | + ); |
|
300 | + $uid = $userBackend->loginName2UserName($param['uid'] ); |
|
301 | + if($uid !== false) { |
|
302 | + $param['uid'] = $uid; |
|
303 | + } |
|
304 | + } |
|
305 | 305 | } |
@@ -35,174 +35,174 @@ |
||
35 | 35 | use OCA\User_LDAP\User\Manager; |
36 | 36 | |
37 | 37 | abstract class Proxy { |
38 | - static private $accesses = array(); |
|
39 | - private $ldap = null; |
|
40 | - |
|
41 | - /** @var \OCP\ICache|null */ |
|
42 | - private $cache; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param ILDAPWrapper $ldap |
|
46 | - */ |
|
47 | - public function __construct(ILDAPWrapper $ldap) { |
|
48 | - $this->ldap = $ldap; |
|
49 | - $memcache = \OC::$server->getMemCacheFactory(); |
|
50 | - if($memcache->isAvailable()) { |
|
51 | - $this->cache = $memcache->create(); |
|
52 | - } |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $configPrefix |
|
57 | - */ |
|
58 | - private function addAccess($configPrefix) { |
|
59 | - static $ocConfig; |
|
60 | - static $fs; |
|
61 | - static $log; |
|
62 | - static $avatarM; |
|
63 | - static $userMap; |
|
64 | - static $groupMap; |
|
65 | - static $db; |
|
66 | - static $coreUserManager; |
|
67 | - if(is_null($fs)) { |
|
68 | - $ocConfig = \OC::$server->getConfig(); |
|
69 | - $fs = new FilesystemHelper(); |
|
70 | - $log = new LogWrapper(); |
|
71 | - $avatarM = \OC::$server->getAvatarManager(); |
|
72 | - $db = \OC::$server->getDatabaseConnection(); |
|
73 | - $userMap = new UserMapping($db); |
|
74 | - $groupMap = new GroupMapping($db); |
|
75 | - $coreUserManager = \OC::$server->getUserManager(); |
|
76 | - } |
|
77 | - $userManager = |
|
78 | - new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, $coreUserManager); |
|
79 | - $connector = new Connection($this->ldap, $configPrefix); |
|
80 | - $access = new Access($connector, $this->ldap, $userManager, new Helper(\OC::$server->getConfig())); |
|
81 | - $access->setUserMapper($userMap); |
|
82 | - $access->setGroupMapper($groupMap); |
|
83 | - self::$accesses[$configPrefix] = $access; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * @param string $configPrefix |
|
88 | - * @return mixed |
|
89 | - */ |
|
90 | - protected function getAccess($configPrefix) { |
|
91 | - if(!isset(self::$accesses[$configPrefix])) { |
|
92 | - $this->addAccess($configPrefix); |
|
93 | - } |
|
94 | - return self::$accesses[$configPrefix]; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $uid |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - protected function getUserCacheKey($uid) { |
|
102 | - return 'user-'.$uid.'-lastSeenOn'; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $gid |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - protected function getGroupCacheKey($gid) { |
|
110 | - return 'group-'.$gid.'-lastSeenOn'; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @param string $id |
|
115 | - * @param string $method |
|
116 | - * @param array $parameters |
|
117 | - * @param bool $passOnWhen |
|
118 | - * @return mixed |
|
119 | - */ |
|
120 | - abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
121 | - |
|
122 | - /** |
|
123 | - * @param string $id |
|
124 | - * @param string $method |
|
125 | - * @param array $parameters |
|
126 | - * @return mixed |
|
127 | - */ |
|
128 | - abstract protected function walkBackends($id, $method, $parameters); |
|
129 | - |
|
130 | - /** |
|
131 | - * @param string $id |
|
132 | - * @return Access |
|
133 | - */ |
|
134 | - abstract public function getLDAPAccess($id); |
|
135 | - |
|
136 | - /** |
|
137 | - * Takes care of the request to the User backend |
|
138 | - * @param string $id |
|
139 | - * @param string $method string, the method of the user backend that shall be called |
|
140 | - * @param array $parameters an array of parameters to be passed |
|
141 | - * @param bool $passOnWhen |
|
142 | - * @return mixed, the result of the specified method |
|
143 | - */ |
|
144 | - protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
|
145 | - $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
146 | - if($result === $passOnWhen) { |
|
147 | - $result = $this->walkBackends($id, $method, $parameters); |
|
148 | - } |
|
149 | - return $result; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param string|null $key |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - private function getCacheKey($key) { |
|
157 | - $prefix = 'LDAP-Proxy-'; |
|
158 | - if(is_null($key)) { |
|
159 | - return $prefix; |
|
160 | - } |
|
161 | - return $prefix.md5($key); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $key |
|
166 | - * @return mixed|null |
|
167 | - */ |
|
168 | - public function getFromCache($key) { |
|
169 | - if(is_null($this->cache) || !$this->isCached($key)) { |
|
170 | - return null; |
|
171 | - } |
|
172 | - $key = $this->getCacheKey($key); |
|
173 | - |
|
174 | - return json_decode(base64_decode($this->cache->get($key))); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $key |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function isCached($key) { |
|
182 | - if(is_null($this->cache)) { |
|
183 | - return false; |
|
184 | - } |
|
185 | - $key = $this->getCacheKey($key); |
|
186 | - return $this->cache->hasKey($key); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * @param string $key |
|
191 | - * @param mixed $value |
|
192 | - */ |
|
193 | - public function writeToCache($key, $value) { |
|
194 | - if(is_null($this->cache)) { |
|
195 | - return; |
|
196 | - } |
|
197 | - $key = $this->getCacheKey($key); |
|
198 | - $value = base64_encode(json_encode($value)); |
|
199 | - $this->cache->set($key, $value, '2592000'); |
|
200 | - } |
|
201 | - |
|
202 | - public function clearCache() { |
|
203 | - if(is_null($this->cache)) { |
|
204 | - return; |
|
205 | - } |
|
206 | - $this->cache->clear($this->getCacheKey(null)); |
|
207 | - } |
|
38 | + static private $accesses = array(); |
|
39 | + private $ldap = null; |
|
40 | + |
|
41 | + /** @var \OCP\ICache|null */ |
|
42 | + private $cache; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param ILDAPWrapper $ldap |
|
46 | + */ |
|
47 | + public function __construct(ILDAPWrapper $ldap) { |
|
48 | + $this->ldap = $ldap; |
|
49 | + $memcache = \OC::$server->getMemCacheFactory(); |
|
50 | + if($memcache->isAvailable()) { |
|
51 | + $this->cache = $memcache->create(); |
|
52 | + } |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $configPrefix |
|
57 | + */ |
|
58 | + private function addAccess($configPrefix) { |
|
59 | + static $ocConfig; |
|
60 | + static $fs; |
|
61 | + static $log; |
|
62 | + static $avatarM; |
|
63 | + static $userMap; |
|
64 | + static $groupMap; |
|
65 | + static $db; |
|
66 | + static $coreUserManager; |
|
67 | + if(is_null($fs)) { |
|
68 | + $ocConfig = \OC::$server->getConfig(); |
|
69 | + $fs = new FilesystemHelper(); |
|
70 | + $log = new LogWrapper(); |
|
71 | + $avatarM = \OC::$server->getAvatarManager(); |
|
72 | + $db = \OC::$server->getDatabaseConnection(); |
|
73 | + $userMap = new UserMapping($db); |
|
74 | + $groupMap = new GroupMapping($db); |
|
75 | + $coreUserManager = \OC::$server->getUserManager(); |
|
76 | + } |
|
77 | + $userManager = |
|
78 | + new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db, $coreUserManager); |
|
79 | + $connector = new Connection($this->ldap, $configPrefix); |
|
80 | + $access = new Access($connector, $this->ldap, $userManager, new Helper(\OC::$server->getConfig())); |
|
81 | + $access->setUserMapper($userMap); |
|
82 | + $access->setGroupMapper($groupMap); |
|
83 | + self::$accesses[$configPrefix] = $access; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * @param string $configPrefix |
|
88 | + * @return mixed |
|
89 | + */ |
|
90 | + protected function getAccess($configPrefix) { |
|
91 | + if(!isset(self::$accesses[$configPrefix])) { |
|
92 | + $this->addAccess($configPrefix); |
|
93 | + } |
|
94 | + return self::$accesses[$configPrefix]; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $uid |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + protected function getUserCacheKey($uid) { |
|
102 | + return 'user-'.$uid.'-lastSeenOn'; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $gid |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + protected function getGroupCacheKey($gid) { |
|
110 | + return 'group-'.$gid.'-lastSeenOn'; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @param string $id |
|
115 | + * @param string $method |
|
116 | + * @param array $parameters |
|
117 | + * @param bool $passOnWhen |
|
118 | + * @return mixed |
|
119 | + */ |
|
120 | + abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
121 | + |
|
122 | + /** |
|
123 | + * @param string $id |
|
124 | + * @param string $method |
|
125 | + * @param array $parameters |
|
126 | + * @return mixed |
|
127 | + */ |
|
128 | + abstract protected function walkBackends($id, $method, $parameters); |
|
129 | + |
|
130 | + /** |
|
131 | + * @param string $id |
|
132 | + * @return Access |
|
133 | + */ |
|
134 | + abstract public function getLDAPAccess($id); |
|
135 | + |
|
136 | + /** |
|
137 | + * Takes care of the request to the User backend |
|
138 | + * @param string $id |
|
139 | + * @param string $method string, the method of the user backend that shall be called |
|
140 | + * @param array $parameters an array of parameters to be passed |
|
141 | + * @param bool $passOnWhen |
|
142 | + * @return mixed, the result of the specified method |
|
143 | + */ |
|
144 | + protected function handleRequest($id, $method, $parameters, $passOnWhen = false) { |
|
145 | + $result = $this->callOnLastSeenOn($id, $method, $parameters, $passOnWhen); |
|
146 | + if($result === $passOnWhen) { |
|
147 | + $result = $this->walkBackends($id, $method, $parameters); |
|
148 | + } |
|
149 | + return $result; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param string|null $key |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + private function getCacheKey($key) { |
|
157 | + $prefix = 'LDAP-Proxy-'; |
|
158 | + if(is_null($key)) { |
|
159 | + return $prefix; |
|
160 | + } |
|
161 | + return $prefix.md5($key); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $key |
|
166 | + * @return mixed|null |
|
167 | + */ |
|
168 | + public function getFromCache($key) { |
|
169 | + if(is_null($this->cache) || !$this->isCached($key)) { |
|
170 | + return null; |
|
171 | + } |
|
172 | + $key = $this->getCacheKey($key); |
|
173 | + |
|
174 | + return json_decode(base64_decode($this->cache->get($key))); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $key |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function isCached($key) { |
|
182 | + if(is_null($this->cache)) { |
|
183 | + return false; |
|
184 | + } |
|
185 | + $key = $this->getCacheKey($key); |
|
186 | + return $this->cache->hasKey($key); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * @param string $key |
|
191 | + * @param mixed $value |
|
192 | + */ |
|
193 | + public function writeToCache($key, $value) { |
|
194 | + if(is_null($this->cache)) { |
|
195 | + return; |
|
196 | + } |
|
197 | + $key = $this->getCacheKey($key); |
|
198 | + $value = base64_encode(json_encode($value)); |
|
199 | + $this->cache->set($key, $value, '2592000'); |
|
200 | + } |
|
201 | + |
|
202 | + public function clearCache() { |
|
203 | + if(is_null($this->cache)) { |
|
204 | + return; |
|
205 | + } |
|
206 | + $this->cache->clear($this->getCacheKey(null)); |
|
207 | + } |
|
208 | 208 | } |
@@ -32,70 +32,70 @@ |
||
32 | 32 | |
33 | 33 | class UUIDFixInsert implements IRepairStep { |
34 | 34 | |
35 | - /** @var IConfig */ |
|
36 | - protected $config; |
|
35 | + /** @var IConfig */ |
|
36 | + protected $config; |
|
37 | 37 | |
38 | - /** @var UserMapping */ |
|
39 | - protected $userMapper; |
|
38 | + /** @var UserMapping */ |
|
39 | + protected $userMapper; |
|
40 | 40 | |
41 | - /** @var GroupMapping */ |
|
42 | - protected $groupMapper; |
|
41 | + /** @var GroupMapping */ |
|
42 | + protected $groupMapper; |
|
43 | 43 | |
44 | - /** @var IJobList */ |
|
45 | - protected $jobList; |
|
44 | + /** @var IJobList */ |
|
45 | + protected $jobList; |
|
46 | 46 | |
47 | - public function __construct(IConfig $config, UserMapping $userMapper, GroupMapping $groupMapper, IJobList $jobList) { |
|
48 | - $this->config = $config; |
|
49 | - $this->userMapper = $userMapper; |
|
50 | - $this->groupMapper = $groupMapper; |
|
51 | - $this->jobList = $jobList; |
|
52 | - } |
|
47 | + public function __construct(IConfig $config, UserMapping $userMapper, GroupMapping $groupMapper, IJobList $jobList) { |
|
48 | + $this->config = $config; |
|
49 | + $this->userMapper = $userMapper; |
|
50 | + $this->groupMapper = $groupMapper; |
|
51 | + $this->jobList = $jobList; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Returns the step's name |
|
56 | - * |
|
57 | - * @return string |
|
58 | - * @since 9.1.0 |
|
59 | - */ |
|
60 | - public function getName() { |
|
61 | - return 'Insert UUIDFix background job for user and group in batches'; |
|
62 | - } |
|
54 | + /** |
|
55 | + * Returns the step's name |
|
56 | + * |
|
57 | + * @return string |
|
58 | + * @since 9.1.0 |
|
59 | + */ |
|
60 | + public function getName() { |
|
61 | + return 'Insert UUIDFix background job for user and group in batches'; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Run repair step. |
|
66 | - * Must throw exception on error. |
|
67 | - * |
|
68 | - * @param IOutput $output |
|
69 | - * @throws \Exception in case of failure |
|
70 | - * @since 9.1.0 |
|
71 | - */ |
|
72 | - public function run(IOutput $output) { |
|
73 | - $installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.2.1'); |
|
74 | - if(version_compare($installedVersion, '1.2.1') !== -1) { |
|
75 | - return; |
|
76 | - } |
|
64 | + /** |
|
65 | + * Run repair step. |
|
66 | + * Must throw exception on error. |
|
67 | + * |
|
68 | + * @param IOutput $output |
|
69 | + * @throws \Exception in case of failure |
|
70 | + * @since 9.1.0 |
|
71 | + */ |
|
72 | + public function run(IOutput $output) { |
|
73 | + $installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.2.1'); |
|
74 | + if(version_compare($installedVersion, '1.2.1') !== -1) { |
|
75 | + return; |
|
76 | + } |
|
77 | 77 | |
78 | - foreach ([$this->userMapper, $this->groupMapper] as $mapper) { |
|
79 | - $offset = 0; |
|
80 | - $batchSize = 50; |
|
81 | - $jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class; |
|
82 | - do { |
|
83 | - $retry = false; |
|
84 | - $records = $mapper->getList($offset, $batchSize); |
|
85 | - if(count($records) === 0){ |
|
86 | - continue; |
|
87 | - } |
|
88 | - try { |
|
89 | - $this->jobList->add($jobClass, ['records' => $records]); |
|
90 | - $offset += $batchSize; |
|
91 | - } catch (\InvalidArgumentException $e) { |
|
92 | - if(strpos($e->getMessage(), 'Background job arguments can\'t exceed 4000') !== false) { |
|
93 | - $batchSize = intval(floor(count($records) * 0.8)); |
|
94 | - $retry = true; |
|
95 | - } |
|
96 | - } |
|
97 | - } while (count($records) === $batchSize || $retry); |
|
98 | - } |
|
78 | + foreach ([$this->userMapper, $this->groupMapper] as $mapper) { |
|
79 | + $offset = 0; |
|
80 | + $batchSize = 50; |
|
81 | + $jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class; |
|
82 | + do { |
|
83 | + $retry = false; |
|
84 | + $records = $mapper->getList($offset, $batchSize); |
|
85 | + if(count($records) === 0){ |
|
86 | + continue; |
|
87 | + } |
|
88 | + try { |
|
89 | + $this->jobList->add($jobClass, ['records' => $records]); |
|
90 | + $offset += $batchSize; |
|
91 | + } catch (\InvalidArgumentException $e) { |
|
92 | + if(strpos($e->getMessage(), 'Background job arguments can\'t exceed 4000') !== false) { |
|
93 | + $batchSize = intval(floor(count($records) * 0.8)); |
|
94 | + $retry = true; |
|
95 | + } |
|
96 | + } |
|
97 | + } while (count($records) === $batchSize || $retry); |
|
98 | + } |
|
99 | 99 | |
100 | - } |
|
100 | + } |
|
101 | 101 | } |