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