-
Notifications
You must be signed in to change notification settings - Fork 199
/
settingcontroller.php
128 lines (110 loc) · 3.32 KB
/
settingcontroller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* ownCloud - Music app
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Morris Jobke <[email protected]>
* @copyright Morris Jobke 2013, 2014
*/
namespace OCA\Music\Controller;
use \OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\Files\Folder;
use \OCP\IConfig;
use \OCP\IL10N;
use \OCP\IRequest;
use \OCP\Security\ISecureRandom;
use \OCA\Music\Db\AmpacheUserMapper;
use \OCA\Music\Utility\Scanner;
class SettingController extends Controller {
const DEFAULT_PASSWORD_LENGTH = 10;
private $appname;
private $ampacheUserMapper;
private $scanner;
private $userId;
private $userFolder;
private $configManager;
private $secureRandom;
private $l10n;
public function __construct($appname,
IRequest $request,
AmpacheUserMapper $ampacheUserMapper,
Scanner $scanner,
$userId,
Folder $userFolder,
IConfig $configManager,
ISecureRandom $secureRandom,
$l10n){
parent::__construct($appname, $request);
$this->appname = $appname;
$this->ampacheUserMapper = $ampacheUserMapper;
$this->scanner = $scanner;
$this->userId = $userId;
$this->userFolder = $userFolder;
$this->configManager = $configManager;
$this->secureRandom = $secureRandom;
$this->l10n = $l10n;
}
/**
* @NoAdminRequired
*/
public function userPath($value) {
$success = false;
$path = $value;
// TODO check for validity
$element = $this->userFolder->get($path);
if ($element instanceof \OCP\Files\Folder) {
if ($path[0] !== '/') {
$path = '/' . $path;
}
if ($path[strlen($path)-1] !== '/') {
$path .= '/';
}
$this->configManager->setUserValue($this->userId, $this->appname, 'path', $path);
$success = true;
$this->scanner->updatePath($path, $this->userId);
}
return new JSONResponse(array('success' => $success));
}
/**
* @NoAdminRequired
*/
public function addUserKey($description, $password) {
$hash = hash('sha256', $password);
$id = $this->ampacheUserMapper->addUserKey($this->userId, $hash, $description);
$success = ($id !== null);
return new JSONResponse(array('success' => $success, 'id' => $id));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*/
public function generateUserKey($length, $description) {
if($description == NULL) {
return new JSONResponse(['message' => $this->l10n->t('Please provide a description')], Http::STATUS_BAD_REQUEST);
}
if($length == NULL || $length < self::DEFAULT_PASSWORD_LENGTH) {
$length = self::DEFAULT_PASSWORD_LENGTH;
}
$password = $this->secureRandom->generate(
$length,
ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
$hash = hash('sha256', $password);
$id = $this->ampacheUserMapper->addUserKey($this->userId, $hash, $description);
if(is_null($id)) {
return new JSONResponse(['message' => $this->l10n->t('Error while saving the credentials')], Http::STATUS_INTERNAL_SERVER_ERROR);
}
return new JSONResponse(['id' => $id, 'password' => $password, 'description' => $description], Http::STATUS_CREATED);
}
/**
* @NoAdminRequired
*/
public function removeUserKey($id) {
$this->ampacheUserMapper->removeUserKey($this->userId, $id);
return new JSONResponse(array('success' => true));
}
}