Passed
Push — feature/329_Subsonic_API ( a9dee6...9d1353 )
by Pauli
14:33
created

SettingController::getPath()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\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...
21
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...
22
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...
23
24
use \OCA\Music\Db\AmpacheUserMapper;
25
use \OCA\Music\Utility\Scanner;
26
use \OCA\Music\Utility\Util;
27
use \OCA\Music\Utility\UserMusicFolder;
28
29
class SettingController extends Controller {
30
	const DEFAULT_PASSWORD_LENGTH = 10;
31
32
	private $ampacheUserMapper;
33
	private $scanner;
34
	private $userId;
35
	private $userMusicFolder;
36
	private $secureRandom;
37
	private $urlGenerator;
38
39
	public function __construct($appName,
40
								IRequest $request,
41
								AmpacheUserMapper $ampacheUserMapper,
42
								Scanner $scanner,
43
								$userId,
44
								UserMusicFolder $userMusicFolder,
45
								ISecureRandom $secureRandom,
46
								IURLGenerator $urlGenerator) {
47
		parent::__construct($appName, $request);
48
49
		$this->ampacheUserMapper = $ampacheUserMapper;
50
		$this->scanner = $scanner;
51
		$this->userId = $userId;
52
		$this->userMusicFolder = $userMusicFolder;
53
		$this->secureRandom = $secureRandom;
54
		$this->urlGenerator = $urlGenerator;
55
	}
56
57
	/**
58
	 * @NoAdminRequired
59
	 * @UseSession to keep the session reserved while execution in progress
60
	 */
61
	public function userPath($value) {
62
		$prevPath = $this->userMusicFolder->getPath($this->userId);
63
		$success = $this->userMusicFolder->setPath($this->userId, $value);
64
65
		if ($success) {
66
			$this->scanner->updatePath($prevPath, $value, $this->userId);
67
		}
68
69
		return new JSONResponse(['success' => $success]);
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 */
75
	public function getAll() {
76
		return [
77
			'path' => $this->userMusicFolder->getPath($this->userId),
78
			'ampacheUrl' => $this->getAmpacheUrl(),
79
			'subsonicUrl' => $this->getSubsonicUrl(),
80
			'ampacheKeys' => $this->getAmpacheKeys(),
81
			'appVersion' => $this->getAppVersion(),
82
			'user' => $this->userId
83
		];
84
	}
85
86
	private function getAmpacheUrl() {
87
		return \str_replace('/server/xml.server.php', '',
88
				$this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute('music.ampache.ampache')));
89
	}
90
91
	private function getSubsonicUrl() {
92
		return \str_replace('/rest/dummy', '',
93
				$this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute(
94
						'music.subsonic.handleRequest', ['method' => 'dummy'])));
95
	}
96
97
	private function getAmpacheKeys() {
98
		return $this->ampacheUserMapper->getAll($this->userId);
99
	}
100
101
	private function getAppVersion() {
102
		// Note: the following in deprecated since NC14 but the replacement
103
		// \OCP\App\IAppManager::getAppVersion is not available before NC14.
104
		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...
105
	}
106
107
	private function storeUserKey($description, $password) {
108
		$hash = \hash('sha256', $password);
109
		$description = Util::truncate($description, 64); // some DB setups can't truncate automatically to column max size
110
		return $this->ampacheUserMapper->addUserKey($this->userId, $hash, $description);
111
	}
112
113
	/**
114
	 * @NoAdminRequired
115
	 */
116
	public function addUserKey($description, $password) {
117
		$id = $this->storeUserKey($description, $password);
118
		$success = ($id !== null);
119
		return new JSONResponse(['success' => $success, 'id' => $id]);
120
	}
121
122
	/**
123
	 * @NoAdminRequired
124
	 * @NoCSRFRequired
125
	 * @CORS
126
	 */
127
	public function generateUserKey($length, $description) {
128
		if ($description == null) {
129
			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...
130
		}
131
132
		if ($length == null || $length < self::DEFAULT_PASSWORD_LENGTH) {
133
			$length = self::DEFAULT_PASSWORD_LENGTH;
134
		}
135
136
		$password = $this->secureRandom->generate(
137
			$length,
138
			ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
139
140
		$id = $this->storeUserKey($description, $password);
141
142
		if ($id === null) {
143
			return new ErrorResponse(Http::STATUS_INTERNAL_SERVER_ERROR, 'Error while saving the credentials');
144
		}
145
146
		return new JSONResponse(['id' => $id, 'password' => $password, 'description' => $description], Http::STATUS_CREATED);
147
	}
148
149
	/**
150
	 * @NoAdminRequired
151
	 */
152
	public function removeUserKey($id) {
153
		$this->ampacheUserMapper->removeUserKey($this->userId, $id);
154
		return new JSONResponse(['success' => true]);
155
	}
156
}
157