Passed
Push — master ( 9b5245...a3dc81 )
by John
13:58 queued 12s
created

SMB::manipulateStorageConfig()   B

Complexity

Conditions 11
Paths 25

Size

Total Lines 62
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 11
eloc 47
c 4
b 0
f 2
nc 25
nop 2
dl 0
loc 62
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Daniel Kesselberg <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Valdnet <[email protected]>
11
 *
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
namespace OCA\Files_External\Lib\Backend;
29
30
use Icewind\SMB\BasicAuth;
31
use Icewind\SMB\KerberosApacheAuth;
32
use Icewind\SMB\KerberosAuth;
33
use OCA\Files_External\Lib\Auth\AuthMechanism;
34
use OCA\Files_External\Lib\Auth\Password\Password;
35
use OCA\Files_External\Lib\Auth\SMB\KerberosApacheAuth as KerberosApacheAuthMechanism;
36
use OCA\Files_External\Lib\DefinitionParameter;
37
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
38
use OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
39
use OCA\Files_External\Lib\StorageConfig;
40
use OCP\IL10N;
41
use OCP\IUser;
42
43
class SMB extends Backend {
44
	use LegacyDependencyCheckPolyfill;
45
46
	public function __construct(IL10N $l, Password $legacyAuth) {
47
		$this
48
			->setIdentifier('smb')
49
			->addIdentifierAlias('\OC\Files\Storage\SMB')// legacy compat
50
			->setStorageClass('\OCA\Files_External\Lib\Storage\SMB')
51
			->setText($l->t('SMB/CIFS'))
52
			->addParameters([
53
				new DefinitionParameter('host', $l->t('Host')),
54
				new DefinitionParameter('share', $l->t('Share')),
55
				(new DefinitionParameter('root', $l->t('Remote subfolder')))
56
					->setFlag(DefinitionParameter::FLAG_OPTIONAL),
57
				(new DefinitionParameter('domain', $l->t('Domain')))
58
					->setFlag(DefinitionParameter::FLAG_OPTIONAL),
59
				(new DefinitionParameter('show_hidden', $l->t('Show hidden files')))
60
					->setType(DefinitionParameter::VALUE_BOOLEAN)
61
					->setFlag(DefinitionParameter::FLAG_OPTIONAL),
62
				(new DefinitionParameter('check_acl', $l->t('Verify ACL access when listing files')))
63
					->setType(DefinitionParameter::VALUE_BOOLEAN)
64
					->setFlag(DefinitionParameter::FLAG_OPTIONAL)
65
					->setTooltip($l->t("Check the ACL's of each file or folder inside a directory to filter out items where the user has no read permissions, comes with a performance penalty")),
66
				(new DefinitionParameter('timeout', $l->t('Timeout')))
67
					->setType(DefinitionParameter::VALUE_HIDDEN)
68
					->setFlag(DefinitionParameter::FLAG_OPTIONAL),
69
			])
70
			->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
71
			->addAuthScheme(AuthMechanism::SCHEME_SMB)
72
			->setLegacyAuthMechanism($legacyAuth);
73
	}
74
75
	public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

75
	public function manipulateStorageConfig(StorageConfig &$storage, /** @scrutinizer ignore-unused */ IUser $user = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
		$auth = $storage->getAuthMechanism();
77
		if ($auth->getScheme() === AuthMechanism::SCHEME_PASSWORD) {
78
			if (!is_string($storage->getBackendOption('user')) || !is_string($storage->getBackendOption('password'))) {
79
				throw new \InvalidArgumentException('user or password is not set');
80
			}
81
82
			$smbAuth = new BasicAuth(
83
				$storage->getBackendOption('user'),
84
				$storage->getBackendOption('domain'),
85
				$storage->getBackendOption('password')
86
			);
87
		} else {
88
			switch ($auth->getIdentifier()) {
89
				case 'smb::kerberos':
90
					$smbAuth = new KerberosAuth();
91
					break;
92
				case 'smb::kerberosapache':
93
					if (!$auth instanceof KerberosApacheAuthMechanism) {
94
						throw new \InvalidArgumentException('invalid authentication backend');
95
					}
96
					$credentialsStore = $auth->getCredentialsStore();
97
					$kerbAuth = new KerberosApacheAuth();
98
					// check if a kerberos ticket is available, else fallback to session credentials
99
					if ($kerbAuth->checkTicket()) {
100
						$smbAuth = $kerbAuth;
101
					} else {
102
						try {
103
							$credentials = $credentialsStore->getLoginCredentials();
104
							$user = $credentials->getLoginName();
105
							$pass = $credentials->getPassword();
106
							preg_match('/(.*)@(.*)/', $user, $matches);
107
							$realm = $storage->getBackendOption('default_realm');
108
							if (empty($realm)) {
109
								$realm = 'WORKGROUP';
110
							}
111
							$userPart = $matches[1];
112
							$domainPart = $matches[2];
113
							if (count($matches) === 0) {
114
								$username = $user;
115
								$workgroup = $realm;
116
							} else {
117
								$username = $userPart;
118
								$workgroup = $domainPart;
119
							}
120
							$smbAuth = new BasicAuth(
121
								$username,
122
								$workgroup,
123
								$pass
124
							);
125
						} catch (\Exception $e) {
126
							throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved');
127
						}
128
					}
129
130
					break;
131
				default:
132
					throw new \InvalidArgumentException('unknown authentication backend');
133
			}
134
		}
135
136
		$storage->setBackendOption('auth', $smbAuth);
137
	}
138
}
139