1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Morris Jobke <[email protected]> |
4
|
|
|
* @author Robin Appelman <[email protected]> |
5
|
|
|
* @author Robin McCorkell <[email protected]> |
6
|
|
|
* @author Ross Nicoll <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
9
|
|
|
* @license AGPL-3.0 |
10
|
|
|
* |
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
13
|
|
|
* as published by the Free Software Foundation. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\Files_External\Controller; |
26
|
|
|
|
27
|
|
|
use OCA\Files_External\Lib\Auth\Password\GlobalAuth; |
28
|
|
|
use OCP\AppFramework\Controller; |
29
|
|
|
use OCP\AppFramework\Http; |
30
|
|
|
use OCP\AppFramework\Http\Response; |
31
|
|
|
use OCP\IGroupManager; |
32
|
|
|
use OCP\IRequest; |
33
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
34
|
|
|
use OCA\Files_External\Lib\Auth\PublicKey\RSA; |
35
|
|
|
use OCP\IUserSession; |
36
|
|
|
|
37
|
|
|
class AjaxController extends Controller { |
38
|
|
|
/** @var RSA */ |
39
|
|
|
private $rsaMechanism; |
40
|
|
|
/** @var GlobalAuth */ |
41
|
|
|
private $globalAuth; |
42
|
|
|
/** @var IUserSession */ |
43
|
|
|
private $userSession; |
44
|
|
|
/** @var IGroupManager */ |
45
|
|
|
private $groupManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $appName |
49
|
|
|
* @param IRequest $request |
50
|
|
|
* @param RSA $rsaMechanism |
51
|
|
|
* @param GlobalAuth $globalAuth |
52
|
|
|
* @param IUserSession $userSession |
53
|
|
|
* @param IGroupManager $groupManager |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function __construct($appName, |
|
|
|
|
56
|
|
|
IRequest $request, |
57
|
|
|
RSA $rsaMechanism, |
58
|
|
|
GlobalAuth $globalAuth, |
59
|
|
|
IUserSession $userSession, |
60
|
|
|
IGroupManager $groupManager) { |
61
|
|
|
parent::__construct($appName, $request); |
62
|
|
|
$this->rsaMechanism = $rsaMechanism; |
63
|
|
|
$this->globalAuth = $globalAuth; |
64
|
|
|
$this->userSession = $userSession; |
65
|
|
|
$this->groupManager = $groupManager; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
private function generateSshKeys() { |
72
|
|
|
$key = $this->rsaMechanism->createKey(); |
73
|
|
|
// Replace the placeholder label with a more meaningful one |
74
|
|
|
$key['publicKey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']); |
75
|
|
|
|
76
|
|
|
return $key; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Generates an SSH public/private key pair. |
81
|
|
|
* |
82
|
|
|
* @NoAdminRequired |
83
|
|
|
*/ |
84
|
|
|
public function getSshKeys() { |
85
|
|
|
$key = $this->generateSshKeys(); |
86
|
|
|
return new JSONResponse( |
87
|
|
|
array('data' => array( |
88
|
|
|
'private_key' => $key['privatekey'], |
89
|
|
|
'public_key' => $key['publickey'] |
90
|
|
|
), |
91
|
|
|
'status' => 'success' |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @NoAdminRequired |
97
|
|
|
* |
98
|
|
|
* @param string $uid |
99
|
|
|
* @param string $user |
100
|
|
|
* @param string $password |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function saveGlobalCredentials($uid, $user, $password) { |
104
|
|
|
$currentUser = $this->userSession->getUser(); |
105
|
|
|
|
106
|
|
|
// Non-admins can only edit their own credentials |
107
|
|
|
$allowedToEdit = ( |
108
|
|
|
$this->groupManager->isAdmin($currentUser->getUID()) || $currentUser->getUID() === $uid |
109
|
|
|
) ? true : false; |
110
|
|
|
|
111
|
|
|
if ($allowedToEdit) { |
112
|
|
|
$this->globalAuth->saveAuth($uid, $user, $password); |
113
|
|
|
return true; |
114
|
|
|
} else { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.