Passed
Push — feature/329_Subsonic_API ( 793ffa...a9dee6 )
by Pauli
15:05
created

SettingController::storeUserKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2017 - 2019
13
 */
14
15
namespace OCA\Music\Controller;
16
17
use \OCP\AppFramework\Controller;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use \OCP\AppFramework\Http;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use \OCP\AppFramework\Http\JSONResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\JSONResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use \OCP\Files\Folder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use \OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use \OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use \OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use \OCP\Security\ISecureRandom;
0 ignored issues
show
Bug introduced by
The type OCP\Security\ISecureRandom was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
use \OCA\Music\Db\AmpacheUserMapper;
27
use \OCA\Music\Utility\Scanner;
28
use \OCA\Music\Utility\Util;
29
30
class SettingController extends Controller {
31
	const DEFAULT_PASSWORD_LENGTH = 10;
32
33
	private $appname;
34
	private $ampacheUserMapper;
35
	private $scanner;
36
	private $userId;
37
	private $userFolder;
38
	private $configManager;
39
	private $secureRandom;
40
	private $urlGenerator;
41
42
	public function __construct($appname,
43
								IRequest $request,
44
								AmpacheUserMapper $ampacheUserMapper,
45
								Scanner $scanner,
46
								$userId,
47
								Folder $userFolder,
48
								IConfig $configManager,
49
								ISecureRandom $secureRandom,
50
								IURLGenerator $urlGenerator) {
51
		parent::__construct($appname, $request);
52
53
		$this->appname = $appname;
54
		$this->ampacheUserMapper = $ampacheUserMapper;
55
		$this->scanner = $scanner;
56
		$this->userId = $userId;
57
		$this->userFolder = $userFolder;
58
		$this->configManager = $configManager;
59
		$this->secureRandom = $secureRandom;
60
		$this->urlGenerator = $urlGenerator;
61
	}
62
63
	/**
64
	 * @NoAdminRequired
65
	 * @UseSession to keep the session reserved while execution in progress
66
	 */
67
	public function userPath($value) {
68
		$success = false;
69
		$path = $value;
70
		// TODO check for validity
71
		$element = $this->userFolder->get($path);
72
		if ($element instanceof \OCP\Files\Folder) {
73
			if ($path[0] !== '/') {
74
				$path = '/' . $path;
75
			}
76
			if ($path[\strlen($path)-1] !== '/') {
77
				$path .= '/';
78
			}
79
			$prevPath = $this->getPath();
80
			$this->configManager->setUserValue($this->userId, $this->appname, 'path', $path);
81
			$success = true;
82
			$this->scanner->updatePath($prevPath, $path, $this->userId);
83
		}
84
		return new JSONResponse(['success' => $success]);
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 */
90
	public function getAll() {
91
		return [
92
			'path' => $this->getPath(),
93
			'ampacheUrl' => $this->getAmpacheUrl(),
94
			'subsonicUrl' => $this->getSubsonicUrl(),
95
			'ampacheKeys' => $this->getAmpacheKeys(),
96
			'appVersion' => $this->getAppVersion(),
97
			'user' => $this->userId
98
		];
99
	}
100
101
	private function getPath() {
102
		$path = $this->configManager->getUserValue($this->userId, $this->appname, 'path');
103
		return $path ?: '/';
104
	}
105
106
	private function getAmpacheUrl() {
107
		return \str_replace('/server/xml.server.php', '',
108
				$this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('music.ampache.ampache')));
109
	}
110
111
	private function getSubsonicUrl() {
112
		return \str_replace('/rest/dummy', '',
113
				$this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute(
114
						'music.subsonic.handleRequest', ['method' => 'dummy'])));
115
	}
116
117
	private function getAmpacheKeys() {
118
		return $this->ampacheUserMapper->getAll($this->userId);
119
	}
120
121
	private function getAppVersion() {
122
		// Note: the following in deprecated since NC14 but the replacement
123
		// \OCP\App\IAppManager::getAppVersion is not available before NC14.
124
		return \OCP\App::getAppVersion($this->appname);
0 ignored issues
show
Bug introduced by
The type OCP\App was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
	}
126
127
	private function storeUserKey($description, $password) {
128
		$hash = \hash('sha256', $password);
129
		$description = Util::truncate($description, 64); // some DB setups can't truncate automatically to column max size
130
		return $this->ampacheUserMapper->addUserKey($this->userId, $hash, $description);
131
	}
132
133
	/**
134
	 * @NoAdminRequired
135
	 */
136
	public function addUserKey($description, $password) {
137
		$id = $this->storeUserKey($description, $password);
138
		$success = ($id !== null);
139
		return new JSONResponse(['success' => $success, 'id' => $id]);
140
	}
141
142
	/**
143
	 * @NoAdminRequired
144
	 * @NoCSRFRequired
145
	 * @CORS
146
	 */
147
	public function generateUserKey($length, $description) {
148
		if ($description == null) {
149
			return new ErrorResponse(Http::STATUS_BAD_REQUEST, 'Please provide a description');
0 ignored issues
show
Bug introduced by
The type OCA\Music\Controller\ErrorResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
150
		}
151
152
		if ($length == null || $length < self::DEFAULT_PASSWORD_LENGTH) {
153
			$length = self::DEFAULT_PASSWORD_LENGTH;
154
		}
155
156
		$password = $this->secureRandom->generate(
157
			$length,
158
			ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
159
160
		$id = $this->storeUserKey($description, $password);
161
162
		if ($id === null) {
163
			return new ErrorResponse(Http::STATUS_INTERNAL_SERVER_ERROR, 'Error while saving the credentials');
164
		}
165
166
		return new JSONResponse(['id' => $id, 'password' => $password, 'description' => $description], Http::STATUS_CREATED);
167
	}
168
169
	/**
170
	 * @NoAdminRequired
171
	 */
172
	public function removeUserKey($id) {
173
		$this->ampacheUserMapper->removeUserKey($this->userId, $id);
174
		return new JSONResponse(['success' => true]);
175
	}
176
}
177