changepassword.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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['personal-password']) ? $_POST['personal-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. }