|
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
|
|
|
* @copyright Morris Jobke 2013, 2014 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Music\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use \OCP\AppFramework\Controller; |
|
16
|
|
|
use \OCP\AppFramework\Http\JSONResponse; |
|
17
|
|
|
use \OCP\Files\Folder; |
|
18
|
|
|
use \OCP\IConfig; |
|
19
|
|
|
use \OCP\IL10N; |
|
20
|
|
|
use \OCP\IRequest; |
|
21
|
|
|
use \OCP\Security\ISecureRandom; |
|
22
|
|
|
|
|
23
|
|
|
use \OCA\Music\Db\AmpacheUserMapper; |
|
24
|
|
|
use \OCA\Music\Utility\Scanner; |
|
25
|
|
|
|
|
26
|
|
|
class SettingController extends Controller { |
|
27
|
|
|
|
|
28
|
|
|
const DEFAULT_PASSWORD_LENGTH = 10; |
|
29
|
|
|
|
|
30
|
|
|
private $appname; |
|
31
|
|
|
private $ampacheUserMapper; |
|
32
|
|
|
private $scanner; |
|
33
|
|
|
private $userId; |
|
34
|
|
|
private $userFolder; |
|
35
|
|
|
private $configManager; |
|
36
|
|
|
private $secureRandom; |
|
37
|
|
|
private $l10n; |
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
public function __construct($appname, |
|
|
|
|
|
|
40
|
|
|
IRequest $request, |
|
41
|
|
|
AmpacheUserMapper $ampacheUserMapper, |
|
42
|
|
|
Scanner $scanner, |
|
43
|
|
|
$userId, |
|
44
|
|
|
Folder $userFolder, |
|
45
|
|
|
IConfig $configManager, |
|
46
|
|
|
ISecureRandom $secureRandom, |
|
47
|
|
|
$l10n){ |
|
48
|
|
|
parent::__construct($appname, $request); |
|
49
|
|
|
|
|
50
|
|
|
$this->appname = $appname; |
|
51
|
|
|
$this->ampacheUserMapper = $ampacheUserMapper; |
|
52
|
|
|
$this->scanner = $scanner; |
|
53
|
|
|
$this->userId = $userId; |
|
54
|
|
|
$this->userFolder = $userFolder; |
|
55
|
|
|
$this->configManager = $configManager; |
|
56
|
|
|
$this->secureRandom = $secureRandom; |
|
57
|
|
|
$this->l10n = $l10n; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @NoAdminRequired |
|
62
|
|
|
*/ |
|
63
|
|
|
public function userPath() { |
|
64
|
|
|
$success = false; |
|
65
|
|
|
$path = $this->params('value'); |
|
66
|
|
|
// TODO check for validity |
|
67
|
|
|
$element = $this->userFolder->get($path); |
|
68
|
|
|
if ($element instanceof \OCP\Files\Folder) { |
|
69
|
|
|
if ($path[0] !== '/') { |
|
70
|
|
|
$path = '/' . $path; |
|
71
|
|
|
} |
|
72
|
|
|
if ($path[strlen($path)-1] !== '/') { |
|
73
|
|
|
$path .= '/'; |
|
74
|
|
|
} |
|
75
|
|
|
$this->configManager->setUserValue($this->userId, $this->appname, 'path', $path); |
|
76
|
|
|
$success = true; |
|
77
|
|
|
$this->scanner->updatePath($path); |
|
78
|
|
|
} |
|
79
|
|
|
return new JSONResponse(array('success' => $success)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @NoAdminRequired |
|
84
|
|
|
*/ |
|
85
|
|
|
public function addUserKey() { |
|
86
|
|
|
$success = false; |
|
87
|
|
|
$description = $this->params('description'); |
|
88
|
|
|
$password = $this->params('password'); |
|
89
|
|
|
|
|
90
|
|
|
$hash = hash('sha256', $password); |
|
91
|
|
|
$id = $this->ampacheUserMapper->addUserKey($this->userId, $hash, $description); |
|
92
|
|
|
if($id !== null) { |
|
93
|
|
|
$success = true; |
|
94
|
|
|
} |
|
95
|
|
|
return new JSONResponse(array('success' => $success, 'id' => $id)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @NoAdminRequired |
|
100
|
|
|
* @NoCSRFRequired |
|
101
|
|
|
* @CORS |
|
102
|
|
|
*/ |
|
103
|
|
|
public function generateUserKey($length, $description) { |
|
104
|
|
|
$success = false; |
|
105
|
|
|
|
|
106
|
|
|
if($description == NULL) { |
|
107
|
|
|
return new JSONResponse(array('success' => $success, 'message' => $this->l10n->t('Please provide a description'))); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if($length == NULL || $length < self::DEFAULT_PASSWORD_LENGTH) { |
|
111
|
|
|
$length = self::DEFAULT_PASSWORD_LENGTH; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$password = $this->secureRandom->generate( |
|
115
|
|
|
$length, |
|
116
|
|
|
ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS); |
|
117
|
|
|
|
|
118
|
|
|
$hash = hash('sha256', $password); |
|
119
|
|
|
$id = $this->ampacheUserMapper->addUserKey($this->userId, $hash, $description); |
|
120
|
|
|
|
|
121
|
|
|
if($id !== null) { |
|
122
|
|
|
$success = true; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return new JSONResponse(array('success' => $success, 'id' => $id, 'password' => $password, 'description' => $description)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @NoAdminRequired |
|
130
|
|
|
*/ |
|
131
|
|
|
public function removeUserKey() { |
|
132
|
|
|
$id = $this->params('id'); |
|
133
|
|
|
$this->ampacheUserMapper->removeUserKey($this->userId, $id); |
|
134
|
|
|
return new JSONResponse(array('success' => true)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.