Completed
Push — master ( ad24b8...a3569a )
by Lukas
67:23 queued 28:25
created

ConfigAPIController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 8
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\User_LDAP\Controller;
25
26
use OC\CapabilitiesManager;
27
use OC\Core\Controller\OCSController;
28
use OC\Security\IdentityProof\Manager;
29
use OCA\User_LDAP\Configuration;
30
use OCA\User_LDAP\Helper;
31
use OCP\AppFramework\Http\DataResponse;
32
use OCP\AppFramework\OCS\OCSBadRequestException;
33
use OCP\AppFramework\OCS\OCSException;
34
use OCP\AppFramework\OCS\OCSNotFoundException;
35
use OCP\ILogger;
36
use OCP\IRequest;
37
use OCP\IUserManager;
38
use OCP\IUserSession;
39
40
class ConfigAPIController extends OCSController {
41
42
	/** @var Helper */
43
	private $ldapHelper;
44
45
	/** @var ILogger */
46
	private $logger;
47
48
	public function __construct(
49
		$appName,
50
		IRequest $request,
51
		CapabilitiesManager $capabilitiesManager,
52
		IUserSession $userSession,
53
		IUserManager $userManager,
54
		Manager $keyManager,
55
		Helper $ldapHelper,
56
		ILogger $logger
57
	) {
58
		parent::__construct(
59
			$appName,
60
			$request,
61
			$capabilitiesManager,
62
			$userSession,
63
			$userManager,
64
			$keyManager
65
		);
66
67
68
		$this->ldapHelper = $ldapHelper;
69
		$this->logger = $logger;
70
	}
71
72
	/**
73
	 * creates a new (empty) configuration and returns the resulting prefix
74
	 *
75
	 * Example: curl -X POST -H "OCS-APIREQUEST: true"  -u $admin:$password \
76
	 *   https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config
77
	 *
78
	 * results in:
79
	 *
80
	 * <?xml version="1.0"?>
81
	 * <ocs>
82
	 *   <meta>
83
	 *     <status>ok</status>
84
	 *     <statuscode>200</statuscode>
85
	 *     <message>OK</message>
86
	 *   </meta>
87
	 *   <data>
88
	 *     <configID>s40</configID>
89
	 *   </data>
90
	 * </ocs>
91
	 *
92
	 * Failing example: if an exception is thrown (e.g. Database connection lost)
93
	 * the detailed error will be logged. The output will then look like:
94
	 *
95
	 * <?xml version="1.0"?>
96
	 * <ocs>
97
	 *   <meta>
98
	 *     <status>failure</status>
99
	 *     <statuscode>999</statuscode>
100
	 *     <message>An issue occurred when creating the new config.</message>
101
	 *   </meta>
102
	 *   <data/>
103
	 * </ocs>
104
	 *
105
	 * For JSON output provide the format=json parameter
106
	 *
107
	 * @return DataResponse
108
	 * @throws OCSException
109
	 */
110
	public function create() {
111
		try {
112
			$configPrefix = $this->ldapHelper->getNextServerConfigurationPrefix();
113
			$configHolder = new Configuration($configPrefix);
114
			$configHolder->saveConfiguration();
115
		} catch (\Exception $e) {
116
			$this->logger->logException($e);
117
			throw new OCSException('An issue occurred when creating the new config.');
118
		}
119
		return new DataResponse(['configID' => $configPrefix]);
120
	}
121
122
	/**
123
	 * Deletes a LDAP configuration, if present.
124
	 *
125
	 * Example:
126
	 *   curl -X DELETE -H "OCS-APIREQUEST: true" -u $admin:$password \
127
	 *    https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60
128
	 *
129
	 * <?xml version="1.0"?>
130
	 * <ocs>
131
	 *   <meta>
132
	 *     <status>ok</status>
133
	 *     <statuscode>200</statuscode>
134
	 *     <message>OK</message>
135
	 *   </meta>
136
	 *   <data/>
137
	 * </ocs>
138
	 *
139
	 * @param string $configID
140
	 * @return DataResponse
141
	 * @throws OCSBadRequestException
142
	 * @throws OCSException
143
	 */
144
	public function delete($configID) {
145
		try {
146
			$this->ensureConfigIDExists($configID);
147
			if(!$this->ldapHelper->deleteServerConfiguration($configID)) {
148
				throw new OCSException('Could not delete configuration');
149
			}
150
		} catch(OCSException $e) {
151
			throw $e;
152
		} catch(\Exception $e) {
153
			$this->logger->logException($e);
154
			throw new OCSException('An issue occurred when deleting the config.');
155
		}
156
157
		return new DataResponse();
158
	}
159
160
	/**
161
	 * modifies a configuration
162
	 *
163
	 * Example:
164
	 *   curl -X PUT -d "configData[ldapHost]=ldaps://my.ldap.server&configData[ldapPort]=636" \
165
	 *    -H "OCS-APIREQUEST: true" -u $admin:$password \
166
	 *    https://nextcloud.server/ocs/v2.php/apps/user_ldap/api/v1/config/s60
167
	 *
168
	 * <?xml version="1.0"?>
169
	 * <ocs>
170
	 *   <meta>
171
	 *     <status>ok</status>
172
	 *     <statuscode>200</statuscode>
173
	 *     <message>OK</message>
174
	 *   </meta>
175
	 *   <data/>
176
	 * </ocs>
177
	 *
178
	 * @param string $configID
179
	 * @param array $configData
180
	 * @return DataResponse
181
	 * @throws OCSException
182
	 */
183
	public function modify($configID, $configData) {
184
		try {
185
			$this->ensureConfigIDExists($configID);
186
187
			if(!is_array($configData)) {
188
				throw new OCSBadRequestException('configData is not properly set');
189
			}
190
191
			$configuration = new Configuration($configID);
192
			$configKeys = $configuration->getConfigTranslationArray();
193
194
			foreach ($configKeys as $i => $key) {
195
				if(isset($configData[$key])) {
196
					$configuration->$key = $configData[$key];
197
				}
198
			}
199
200
			$configuration->saveConfiguration();
201
		} catch(OCSException $e) {
202
			throw $e;
203
		} catch (\Exception $e) {
204
			$this->logger->logException($e);
205
			throw new OCSException('An issue occurred when modifying the config.');
206
		}
207
208
		return new DataResponse();
209
	}
210
211
	/**
212
	 * retrieves a configuration
213
	 *
214
	 * <?xml version="1.0"?>
215
	 * <ocs>
216
	 *   <meta>
217
	 *     <status>ok</status>
218
	 *     <statuscode>200</statuscode>
219
	 *     <message>OK</message>
220
	 *   </meta>
221
	 *   <data>
222
	 *     <ldapHost>ldaps://my.ldap.server</ldapHost>
223
	 *     <ldapPort>7770</ldapPort>
224
	 *     <ldapBackupHost></ldapBackupHost>
225
	 *     <ldapBackupPort></ldapBackupPort>
226
	 *     <ldapBase>ou=small,dc=my,dc=ldap,dc=server</ldapBase>
227
	 *     <ldapBaseUsers>ou=users,ou=small,dc=my,dc=ldap,dc=server</ldapBaseUsers>
228
	 *     <ldapBaseGroups>ou=small,dc=my,dc=ldap,dc=server</ldapBaseGroups>
229
	 *     <ldapAgentName>cn=root,dc=my,dc=ldap,dc=server</ldapAgentName>
230
	 *     <ldapAgentPassword>clearTextWithShowPassword=1</ldapAgentPassword>
231
	 *     <ldapTLS>1</ldapTLS>
232
	 *     <turnOffCertCheck>0</turnOffCertCheck>
233
	 *     <ldapIgnoreNamingRules/>
234
	 *     <ldapUserDisplayName>displayname</ldapUserDisplayName>
235
	 *     <ldapUserDisplayName2>uid</ldapUserDisplayName2>
236
	 *     <ldapUserFilterObjectclass>inetOrgPerson</ldapUserFilterObjectclass>
237
	 *     <ldapUserFilterGroups></ldapUserFilterGroups>
238
	 *     <ldapUserFilter>(&amp;(objectclass=nextcloudUser)(nextcloudEnabled=TRUE))</ldapUserFilter>
239
	 *     <ldapUserFilterMode>1</ldapUserFilterMode>
240
	 *     <ldapGroupFilter>(&amp;(|(objectclass=nextcloudGroup)))</ldapGroupFilter>
241
	 *     <ldapGroupFilterMode>0</ldapGroupFilterMode>
242
	 *     <ldapGroupFilterObjectclass>nextcloudGroup</ldapGroupFilterObjectclass>
243
	 *     <ldapGroupFilterGroups></ldapGroupFilterGroups>
244
	 *     <ldapGroupDisplayName>cn</ldapGroupDisplayName>
245
	 *     <ldapGroupMemberAssocAttr>memberUid</ldapGroupMemberAssocAttr>
246
	 *     <ldapLoginFilter>(&amp;(|(objectclass=inetOrgPerson))(uid=%uid))</ldapLoginFilter>
247
	 *     <ldapLoginFilterMode>0</ldapLoginFilterMode>
248
	 *     <ldapLoginFilterEmail>0</ldapLoginFilterEmail>
249
	 *     <ldapLoginFilterUsername>1</ldapLoginFilterUsername>
250
	 *     <ldapLoginFilterAttributes></ldapLoginFilterAttributes>
251
	 *     <ldapQuotaAttribute></ldapQuotaAttribute>
252
	 *     <ldapQuotaDefault></ldapQuotaDefault>
253
	 *     <ldapEmailAttribute>mail</ldapEmailAttribute>
254
	 *     <ldapCacheTTL>20</ldapCacheTTL>
255
	 *     <ldapUuidUserAttribute>auto</ldapUuidUserAttribute>
256
	 *     <ldapUuidGroupAttribute>auto</ldapUuidGroupAttribute>
257
	 *     <ldapOverrideMainServer></ldapOverrideMainServer>
258
	 *     <ldapConfigurationActive>1</ldapConfigurationActive>
259
	 *     <ldapAttributesForUserSearch>uid;sn;givenname</ldapAttributesForUserSearch>
260
	 *     <ldapAttributesForGroupSearch></ldapAttributesForGroupSearch>
261
	 *     <ldapExperiencedAdmin>0</ldapExperiencedAdmin>
262
	 *     <homeFolderNamingRule></homeFolderNamingRule>
263
	 *     <hasPagedResultSupport></hasPagedResultSupport>
264
	 *     <hasMemberOfFilterSupport></hasMemberOfFilterSupport>
265
	 *     <useMemberOfToDetectMembership>1</useMemberOfToDetectMembership>
266
	 *     <ldapExpertUsernameAttr>uid</ldapExpertUsernameAttr>
267
	 *     <ldapExpertUUIDUserAttr>uid</ldapExpertUUIDUserAttr>
268
	 *     <ldapExpertUUIDGroupAttr></ldapExpertUUIDGroupAttr>
269
	 *     <lastJpegPhotoLookup>0</lastJpegPhotoLookup>
270
	 *     <ldapNestedGroups>0</ldapNestedGroups>
271
	 *     <ldapPagingSize>500</ldapPagingSize>
272
	 *     <turnOnPasswordChange>1</turnOnPasswordChange>
273
	 *     <ldapDynamicGroupMemberURL></ldapDynamicGroupMemberURL>
274
	 *   </data>
275
	 * </ocs>
276
	 *
277
	 * @param string $configID
278
	 * @param bool|string $showPassword
279
	 * @return DataResponse
280
	 * @throws OCSException
281
	 */
282
	public function show($configID, $showPassword = false) {
283
		try {
284
			$this->ensureConfigIDExists($configID);
285
286
			$config = new Configuration($configID);
287
			$data = $config->getConfiguration();
288
			if(!boolval(intval($showPassword))) {
289
				$data['ldapAgentPassword'] = '***';
290
			}
291
			foreach ($data as $key => $value) {
292
				if(is_array($value)) {
293
					$value = implode(';', $value);
294
					$data[$key] = $value;
295
				}
296
			}
297
		} catch(OCSException $e) {
298
			throw $e;
299
		} catch (\Exception $e) {
300
			$this->logger->logException($e);
301
			throw new OCSException('An issue occurred when modifying the config.');
302
		}
303
304
		return new DataResponse($data);
305
	}
306
307
	/**
308
	 * if the given config ID is not available, an exception is thrown
309
	 *
310
	 * @param string $configID
311
	 * @throws OCSNotFoundException
312
	 */
313
	private function ensureConfigIDExists($configID) {
314
		$prefixes = $this->ldapHelper->getServerConfigurationPrefixes();
315
		if(!in_array($configID, $prefixes, true)) {
316
			throw new OCSNotFoundException('Config ID not found');
317
		}
318
	}
319
}
320