PageRenderTime 142ms CodeModel.GetById 80ms app.highlight 5ms RepoModel.GetById 56ms app.codeStats 0ms

/settings/ajax/changepassword.php

https://github.com/sezuan/core
PHP | 64 lines | 53 code | 8 blank | 3 comment | 14 complexity | c6c62a5c60c402f68bbac7a7d43e0434 MD5 | raw file
 1<?php
 2
 3// Check if we are a user
 4OCP\JSON::callCheck();
 5OC_JSON::checkLoggedIn();
 6
 7// Manually load apps to ensure hooks work correctly (workaround for issue 1503)
 8OC_APP::loadApps();
 9
10$username = isset($_POST['username']) ? $_POST['username'] : OC_User::getUser();
11$password = isset($_POST['password']) ? $_POST['password'] : null;
12$oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : '';
13$recoveryPassword = isset($_POST['recoveryPassword']) ? $_POST['recoveryPassword'] : null;
14
15$userstatus = null;
16if (OC_User::isAdminUser(OC_User::getUser())) {
17	$userstatus = 'admin';
18}
19if (OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)) {
20	$userstatus = 'subadmin';
21}
22if (OC_User::getUser() === $username && OC_User::checkPassword($username, $oldPassword)) {
23	$userstatus = 'user';
24}
25
26if (is_null($userstatus)) {
27	OC_JSON::error(array('data' => array('message' => 'Authentication error')));
28	exit();
29}
30
31if (\OCP\App::isEnabled('files_encryption') && $userstatus !== 'user') {
32	//handle the recovery case
33	$util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), $username);
34	$recoveryAdminEnabled = OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled');
35
36	$validRecoveryPassword = false;
37	$recoveryPasswordSupported = false;
38	if ($recoveryAdminEnabled) {
39		$validRecoveryPassword = $util->checkRecoveryPassword($recoveryPassword);
40		$recoveryEnabledForUser = $util->recoveryEnabledForUser();
41	}
42
43	if ($recoveryEnabledForUser && $recoveryPassword === '') {
44		OC_JSON::error(array('data' => array('message' => 'Please provide a admin recovery password, otherwise all user data will be lost')));
45	} elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
46		OC_JSON::error(array('data' => array('message' => 'Wrong admin recovery password. Please check the password and try again.')));
47	} else { // now we know that everything is fine regarding the recovery password, let's try to change the password
48	$result = OC_User::setPassword($username, $password, $recoveryPassword);
49	if (!$result && $recoveryPasswordSupported) {
50		OC_JSON::error(array("data" => array( "message" => "Back-end doesn't support password change, but the users encryption key was successfully updated." )));
51	} elseif (!$result && !$recoveryPasswordSupported) {
52		OC_JSON::error(array("data" => array( "message" => "Unable to change password" )));
53	} else {
54		OC_JSON::success(array("data" => array( "username" => $username )));
55	}
56
57	}
58} else { // if user changes his own password or if encryption is disabled, proceed
59	if (!is_null($password) && OC_User::setPassword($username, $password)) {
60		OC_JSON::success(array('data' => array('username' => $username)));
61	} else {
62		OC_JSON::error(array('data' => array('message' => 'Unable to change password')));
63	}
64}