1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Arthur Schiwon <[email protected]> |
4
|
|
|
* @author Björn Schießle <[email protected]> |
5
|
|
|
* @author Christopher Schäpers <[email protected]> |
6
|
|
|
* @author Clark Tomlinson <[email protected]> |
7
|
|
|
* @author cmeh <[email protected]> |
8
|
|
|
* @author Florin Peter <[email protected]> |
9
|
|
|
* @author Jakob Sack <[email protected]> |
10
|
|
|
* @author Lukas Reschke <[email protected]> |
11
|
|
|
* @author Robin Appelman <[email protected]> |
12
|
|
|
* @author Sam Tuke <[email protected]> |
13
|
|
|
* @author Thomas Müller <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
16
|
|
|
* @license AGPL-3.0 |
17
|
|
|
* |
18
|
|
|
* This code is free software: you can redistribute it and/or modify |
19
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
20
|
|
|
* as published by the Free Software Foundation. |
21
|
|
|
* |
22
|
|
|
* This program is distributed in the hope that it will be useful, |
23
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
24
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25
|
|
|
* GNU Affero General Public License for more details. |
26
|
|
|
* |
27
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
28
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
namespace OC\Settings\ChangePassword; |
32
|
|
|
|
33
|
|
|
use OC\HintException; |
34
|
|
|
|
35
|
|
|
class Controller { |
36
|
|
|
public static function changePersonalPassword($args) { |
|
|
|
|
37
|
|
|
// Check if we are an user |
38
|
|
|
\OC_JSON::callCheck(); |
|
|
|
|
39
|
|
|
\OC_JSON::checkLoggedIn(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$username = \OC_User::getUser(); |
42
|
|
|
$password = isset($_POST['personal-password']) ? $_POST['personal-password'] : null; |
43
|
|
|
$oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : ''; |
44
|
|
|
$l = new \OC_L10n('settings'); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (!\OC_User::checkPassword($username, $oldPassword)) { |
|
|
|
|
47
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("Wrong password")) )); |
|
|
|
|
48
|
|
|
exit(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
if (!is_null($password) && \OC_User::setPassword($username, $password)) { |
53
|
|
|
\OC_JSON::success(['data' => ['message' => $l->t('Saved')]]); |
|
|
|
|
54
|
|
|
} else { |
55
|
|
|
\OC_JSON::error(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} catch (HintException $e) { |
58
|
|
|
\OC_JSON::error(['data' => ['message' => $e->getHint()]]); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function changeUserPassword($args) { |
|
|
|
|
63
|
|
|
// Check if we are an user |
64
|
|
|
\OC_JSON::callCheck(); |
|
|
|
|
65
|
|
|
\OC_JSON::checkLoggedIn(); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$l = new \OC_L10n('settings'); |
|
|
|
|
68
|
|
|
if (isset($_POST['username'])) { |
69
|
|
|
$username = $_POST['username']; |
70
|
|
|
} else { |
71
|
|
|
\OC_JSON::error(array('data' => array('message' => $l->t('No user supplied')) )); |
|
|
|
|
72
|
|
|
exit(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$password = isset($_POST['password']) ? $_POST['password'] : null; |
76
|
|
|
$recoveryPassword = isset($_POST['recoveryPassword']) ? $_POST['recoveryPassword'] : null; |
77
|
|
|
|
78
|
|
|
$isUserAccessible = false; |
79
|
|
|
$currentUserObject = \OC::$server->getUserSession()->getUser(); |
80
|
|
|
$targetUserObject = \OC::$server->getUserManager()->get($username); |
81
|
|
View Code Duplication |
if($currentUserObject !== null && $targetUserObject !== null) { |
|
|
|
|
82
|
|
|
$isUserAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isUserAccessible($currentUserObject, $targetUserObject); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (\OC_User::isAdminUser(\OC_User::getUser())) { |
86
|
|
|
$userstatus = 'admin'; |
|
|
|
|
87
|
|
|
} elseif ($isUserAccessible) { |
88
|
|
|
$userstatus = 'subadmin'; |
|
|
|
|
89
|
|
|
} else { |
90
|
|
|
\OC_JSON::error(array('data' => array('message' => $l->t('Authentication error')) )); |
|
|
|
|
91
|
|
|
exit(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (\OC_App::isEnabled('encryption')) { |
95
|
|
|
//handle the recovery case |
96
|
|
|
$crypt = new \OCA\Encryption\Crypto\Crypt( |
97
|
|
|
\OC::$server->getLogger(), |
98
|
|
|
\OC::$server->getUserSession(), |
99
|
|
|
\OC::$server->getConfig(), |
100
|
|
|
\OC::$server->getL10N('encryption')); |
101
|
|
|
$keyStorage = \OC::$server->getEncryptionKeyStorage(); |
102
|
|
|
$util = new \OCA\Encryption\Util( |
103
|
|
|
new \OC\Files\View(), |
104
|
|
|
$crypt, |
105
|
|
|
\OC::$server->getLogger(), |
106
|
|
|
\OC::$server->getUserSession(), |
107
|
|
|
\OC::$server->getConfig(), |
108
|
|
|
\OC::$server->getUserManager()); |
109
|
|
|
$keyManager = new \OCA\Encryption\KeyManager( |
110
|
|
|
$keyStorage, |
111
|
|
|
$crypt, |
112
|
|
|
\OC::$server->getConfig(), |
113
|
|
|
\OC::$server->getUserSession(), |
114
|
|
|
new \OCA\Encryption\Session(\OC::$server->getSession()), |
115
|
|
|
\OC::$server->getLogger(), |
116
|
|
|
$util); |
117
|
|
|
$recovery = new \OCA\Encryption\Recovery( |
118
|
|
|
\OC::$server->getUserSession(), |
119
|
|
|
$crypt, |
120
|
|
|
\OC::$server->getSecureRandom(), |
121
|
|
|
$keyManager, |
122
|
|
|
\OC::$server->getConfig(), |
123
|
|
|
$keyStorage, |
124
|
|
|
\OC::$server->getEncryptionFilesHelper(), |
125
|
|
|
new \OC\Files\View()); |
126
|
|
|
$recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
127
|
|
|
|
128
|
|
|
$validRecoveryPassword = false; |
129
|
|
|
$recoveryEnabledForUser = false; |
130
|
|
|
if ($recoveryAdminEnabled) { |
131
|
|
|
$validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
132
|
|
|
$recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($recoveryEnabledForUser && $recoveryPassword === '') { |
136
|
|
|
\OC_JSON::error(array('data' => array( |
|
|
|
|
137
|
|
|
'message' => $l->t('Please provide an admin recovery password, otherwise all user data will be lost') |
138
|
|
|
))); |
139
|
|
View Code Duplication |
} elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { |
|
|
|
|
140
|
|
|
\OC_JSON::error(array('data' => array( |
|
|
|
|
141
|
|
|
'message' => $l->t('Wrong admin recovery password. Please check the password and try again.') |
142
|
|
|
))); |
143
|
|
|
} else { // now we know that everything is fine regarding the recovery password, let's try to change the password |
144
|
|
|
$result = \OC_User::setPassword($username, $password, $recoveryPassword); |
145
|
|
|
if (!$result && $recoveryEnabledForUser) { |
146
|
|
|
\OC_JSON::error(array( |
|
|
|
|
147
|
|
|
"data" => array( |
148
|
|
|
"message" => $l->t("Backend doesn't support password change, but the user's encryption key was successfully updated.") |
149
|
|
|
) |
150
|
|
|
)); |
151
|
|
View Code Duplication |
} elseif (!$result && !$recoveryEnabledForUser) { |
|
|
|
|
152
|
|
|
\OC_JSON::error(array("data" => array( "message" => $l->t("Unable to change password" ) ))); |
|
|
|
|
153
|
|
|
} else { |
154
|
|
|
\OC_JSON::success(array("data" => array("username" => $username, 'message' => $l->t('Saved')))); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
} else { // if encryption is disabled, proceed |
159
|
|
|
try { |
160
|
|
|
if (!is_null($password) && \OC_User::setPassword($username, $password)) { |
161
|
|
|
\OC_JSON::success(array('data' => array('username' => $username, 'message' => $l->t('Saved')))); |
|
|
|
|
162
|
|
|
} else { |
163
|
|
|
\OC_JSON::error(array('data' => array('message' => $l->t('Unable to change password')))); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
} catch (HintException $e) { |
166
|
|
|
\OC_JSON::error(array('data' => array('message' => $e->getHint()))); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.