controller.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace OC\Settings\ChangePassword;
  3. class Controller {
  4. public static function changePersonalPassword($args) {
  5. // Check if we are an user
  6. \OC_JSON::callCheck();
  7. \OC_JSON::checkLoggedIn();
  8. $username = \OC_User::getUser();
  9. $password = isset($_POST['personal-password']) ? $_POST['personal-password'] : null;
  10. $oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : '';
  11. if (!\OC_User::checkPassword($username, $oldPassword)) {
  12. $l = new \OC_L10n('settings');
  13. \OC_JSON::error(array("data" => array("message" => $l->t("Wrong password")) ));
  14. exit();
  15. }
  16. if (!is_null($password) && \OC_User::setPassword($username, $password)) {
  17. \OC_JSON::success();
  18. } else {
  19. \OC_JSON::error();
  20. }
  21. }
  22. public static function changeUserPassword($args) {
  23. // Check if we are an user
  24. \OC_JSON::callCheck();
  25. \OC_JSON::checkLoggedIn();
  26. if (isset($_POST['username'])) {
  27. $username = $_POST['username'];
  28. } else {
  29. $l = new \OC_L10n('settings');
  30. \OC_JSON::error(array('data' => array('message' => $l->t('No user supplied')) ));
  31. exit();
  32. }
  33. $password = isset($_POST['password']) ? $_POST['password'] : null;
  34. $recoveryPassword = isset($_POST['recoveryPassword']) ? $_POST['recoveryPassword'] : null;
  35. if (\OC_User::isAdminUser(\OC_User::getUser())) {
  36. $userstatus = 'admin';
  37. } elseif (\OC_SubAdmin::isUserAccessible(\OC_User::getUser(), $username)) {
  38. $userstatus = 'subadmin';
  39. } else {
  40. $l = new \OC_L10n('settings');
  41. \OC_JSON::error(array('data' => array('message' => $l->t('Authentication error')) ));
  42. exit();
  43. }
  44. if (\OC_App::isEnabled('files_encryption')) {
  45. //handle the recovery case
  46. $util = new \OCA\Files_Encryption\Util(new \OC\Files\View('/'), $username);
  47. $recoveryAdminEnabled = \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled');
  48. $validRecoveryPassword = false;
  49. $recoveryEnabledForUser = false;
  50. if ($recoveryAdminEnabled) {
  51. $validRecoveryPassword = $util->checkRecoveryPassword($recoveryPassword);
  52. $recoveryEnabledForUser = $util->recoveryEnabledForUser();
  53. }
  54. if ($recoveryEnabledForUser && $recoveryPassword === '') {
  55. $l = new \OC_L10n('settings');
  56. \OC_JSON::error(array('data' => array(
  57. 'message' => $l->t('Please provide an admin recovery password, otherwise all user data will be lost')
  58. )));
  59. } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
  60. $l = new \OC_L10n('settings');
  61. \OC_JSON::error(array('data' => array(
  62. 'message' => $l->t('Wrong admin recovery password. Please check the password and try again.')
  63. )));
  64. } else { // now we know that everything is fine regarding the recovery password, let's try to change the password
  65. $result = \OC_User::setPassword($username, $password, $recoveryPassword);
  66. if (!$result && $recoveryEnabledForUser) {
  67. $l = new \OC_L10n('settings');
  68. \OC_JSON::error(array(
  69. "data" => array(
  70. "message" => $l->t("Backend doesn't support password change, but the user's encryption key was successfully updated.")
  71. )
  72. ));
  73. } elseif (!$result && !$recoveryEnabledForUser) {
  74. $l = new \OC_L10n('settings');
  75. \OC_JSON::error(array("data" => array( "message" => $l->t("Unable to change password" ) )));
  76. } else {
  77. \OC_JSON::success(array("data" => array( "username" => $username )));
  78. }
  79. }
  80. } else { // if encryption is disabled, proceed
  81. if (!is_null($password) && \OC_User::setPassword($username, $password)) {
  82. \OC_JSON::success(array('data' => array('username' => $username)));
  83. } else {
  84. $l = new \OC_L10n('settings');
  85. \OC_JSON::error(array('data' => array('message' => $l->t('Unable to change password'))));
  86. }
  87. }
  88. }
  89. }