controller.php 5.6 KB

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