PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/settings/changepassword/controller.php

https://gitlab.com/Red54/core
PHP | 152 lines | 110 code | 11 blank | 31 comment | 22 complexity | 33adc618713907d40f88c536ff83dcea MD5 | raw file
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author cmeh <cmeh@users.noreply.github.com>
  6. * @author Florin Peter <github@florin-peter.de>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Joas Schilling <nickvergessen@owncloud.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <icewind@owncloud.com>
  11. * @author Sam Tuke <mail@samtuke.com>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Settings\ChangePassword;
  31. class Controller {
  32. public static function changePersonalPassword($args) {
  33. // Check if we are an user
  34. \OC_JSON::callCheck();
  35. \OC_JSON::checkLoggedIn();
  36. $username = \OC_User::getUser();
  37. $password = isset($_POST['personal-password']) ? $_POST['personal-password'] : null;
  38. $oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : '';
  39. if (!\OC_User::checkPassword($username, $oldPassword)) {
  40. $l = new \OC_L10n('settings');
  41. \OC_JSON::error(array("data" => array("message" => $l->t("Wrong password")) ));
  42. exit();
  43. }
  44. if (!is_null($password) && \OC_User::setPassword($username, $password)) {
  45. \OC_JSON::success();
  46. } else {
  47. \OC_JSON::error();
  48. }
  49. }
  50. public static function changeUserPassword($args) {
  51. // Check if we are an user
  52. \OC_JSON::callCheck();
  53. \OC_JSON::checkLoggedIn();
  54. if (isset($_POST['username'])) {
  55. $username = $_POST['username'];
  56. } else {
  57. $l = new \OC_L10n('settings');
  58. \OC_JSON::error(array('data' => array('message' => $l->t('No user supplied')) ));
  59. exit();
  60. }
  61. $password = isset($_POST['password']) ? $_POST['password'] : null;
  62. $recoveryPassword = isset($_POST['recoveryPassword']) ? $_POST['recoveryPassword'] : null;
  63. if (\OC_User::isAdminUser(\OC_User::getUser())) {
  64. $userstatus = 'admin';
  65. } elseif (\OC_SubAdmin::isUserAccessible(\OC_User::getUser(), $username)) {
  66. $userstatus = 'subadmin';
  67. } else {
  68. $l = new \OC_L10n('settings');
  69. \OC_JSON::error(array('data' => array('message' => $l->t('Authentication error')) ));
  70. exit();
  71. }
  72. if (\OC_App::isEnabled('encryption')) {
  73. //handle the recovery case
  74. $crypt = new \OCA\Encryption\Crypto\Crypt(
  75. \OC::$server->getLogger(),
  76. \OC::$server->getUserSession(),
  77. \OC::$server->getConfig());
  78. $keyStorage = \OC::$server->getEncryptionKeyStorage();
  79. $util = new \OCA\Encryption\Util(
  80. new \OC\Files\View(),
  81. $crypt,
  82. \OC::$server->getLogger(),
  83. \OC::$server->getUserSession(),
  84. \OC::$server->getConfig(),
  85. \OC::$server->getUserManager());
  86. $keyManager = new \OCA\Encryption\KeyManager(
  87. $keyStorage,
  88. $crypt,
  89. \OC::$server->getConfig(),
  90. \OC::$server->getUserSession(),
  91. new \OCA\Encryption\Session(\OC::$server->getSession()),
  92. \OC::$server->getLogger(),
  93. $util);
  94. $recovery = new \OCA\Encryption\Recovery(
  95. \OC::$server->getUserSession(),
  96. $crypt,
  97. \OC::$server->getSecureRandom(),
  98. $keyManager,
  99. \OC::$server->getConfig(),
  100. $keyStorage,
  101. \OC::$server->getEncryptionFilesHelper(),
  102. new \OC\Files\View());
  103. $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled();
  104. $validRecoveryPassword = false;
  105. $recoveryEnabledForUser = false;
  106. if ($recoveryAdminEnabled) {
  107. $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword);
  108. $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username);
  109. }
  110. $l = new \OC_L10n('settings');
  111. if ($recoveryEnabledForUser && $recoveryPassword === '') {
  112. \OC_JSON::error(array('data' => array(
  113. 'message' => $l->t('Please provide an admin recovery password, otherwise all user data will be lost')
  114. )));
  115. } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
  116. \OC_JSON::error(array('data' => array(
  117. 'message' => $l->t('Wrong admin recovery password. Please check the password and try again.')
  118. )));
  119. } else { // now we know that everything is fine regarding the recovery password, let's try to change the password
  120. $result = \OC_User::setPassword($username, $password, $recoveryPassword);
  121. if (!$result && $recoveryEnabledForUser) {
  122. \OC_JSON::error(array(
  123. "data" => array(
  124. "message" => $l->t("Backend doesn't support password change, but the user's encryption key was successfully updated.")
  125. )
  126. ));
  127. } elseif (!$result && !$recoveryEnabledForUser) {
  128. \OC_JSON::error(array("data" => array( "message" => $l->t("Unable to change password" ) )));
  129. } else {
  130. \OC_JSON::success(array("data" => array( "username" => $username )));
  131. }
  132. }
  133. } else { // if encryption is disabled, proceed
  134. if (!is_null($password) && \OC_User::setPassword($username, $password)) {
  135. \OC_JSON::success(array('data' => array('username' => $username)));
  136. } else {
  137. \OC_JSON::error(array('data' => array('message' => $l->t('Unable to change password'))));
  138. }
  139. }
  140. }
  141. }