Completed
Push — stable9 ( 212ff8...07c437 )
by Morris
26:13 queued 09:36
created

GlobalAuth::manipulateStorageConfig()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 5
nop 2
dl 0
loc 14
rs 9.2
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author Robin Appelman <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Files_External\Lib\Auth\Password;
23
24
use OCA\Files_External\Service\BackendService;
25
use OCP\IL10N;
26
use OCP\IUser;
27
use OCA\Files_External\Lib\Auth\AuthMechanism;
28
use OCA\Files_External\Lib\StorageConfig;
29
use OCP\Security\ICredentialsManager;
30
use OCP\Files\Storage;
31
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
32
33
/**
34
 * Global Username and Password
35
 */
36
class GlobalAuth extends AuthMechanism {
37
38
	const CREDENTIALS_IDENTIFIER = 'password::global';
39
40
	/** @var ICredentialsManager */
41
	protected $credentialsManager;
42
43
	public function __construct(IL10N $l, ICredentialsManager $credentialsManager) {
44
		$this->credentialsManager = $credentialsManager;
45
46
		$this
47
			->setIdentifier('password::global')
48
			->setVisibility(BackendService::VISIBILITY_DEFAULT)
49
			->setScheme(self::SCHEME_PASSWORD)
50
			->setText($l->t('Global Credentials'));
51
	}
52
53
	public function getAuth($uid) {
54
		$auth = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
55
		if (!is_array($auth)) {
56
			return [
57
				'user' => '',
58
				'password' => ''
59
			];
60
		} else {
61
			return $auth;
62
		}
63
	}
64
65
	public function saveAuth($uid, $user, $password) {
66
		$this->credentialsManager->store($uid, self::CREDENTIALS_IDENTIFIER, [
67
			'user' => $user,
68
			'password' => $password
69
		]);
70
	}
71
72
	public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
73
		if ($storage->getType() === StorageConfig::MOUNT_TYPE_ADMIN) {
74
			$uid = '';
75
		} elseif (is_null($user)) {
76
			throw new InsufficientDataForMeaningfulAnswerException('No credentials saved');
77
		} else {
78
			$uid = $user->getUID();
79
		}
80
		$credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);
81
		if (is_array($credentials)) {
82
			$storage->setBackendOption('user', $credentials['user']);
83
			$storage->setBackendOption('password', $credentials['password']);
84
		}
85
	}
86
87
}
88