ApiValidatePassword.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. use MediaWiki\Auth\AuthManager;
  3. /**
  4. * @ingroup API
  5. */
  6. class ApiValidatePassword extends ApiBase {
  7. public function execute() {
  8. $params = $this->extractRequestParams();
  9. // For sanity
  10. $this->requirePostedParameters( [ 'password' ] );
  11. if ( $params['user'] !== null ) {
  12. $user = User::newFromName( $params['user'], 'creatable' );
  13. if ( !$user ) {
  14. $encParamName = $this->encodeParamName( 'user' );
  15. $this->dieWithError(
  16. [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
  17. "baduser_{$encParamName}"
  18. );
  19. }
  20. if ( !$user->isAnon() || AuthManager::singleton()->userExists( $user->getName() ) ) {
  21. $this->dieWithError( 'userexists' );
  22. }
  23. $user->setEmail( (string)$params['email'] );
  24. $user->setRealName( (string)$params['realname'] );
  25. } else {
  26. $user = $this->getUser();
  27. }
  28. $r = [];
  29. $validity = $user->checkPasswordValidity( $params['password'] );
  30. $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
  31. $messages = array_merge(
  32. $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
  33. $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
  34. );
  35. if ( $messages ) {
  36. $r['validitymessages'] = $messages;
  37. }
  38. Hooks::run( 'ApiValidatePassword', [ $this, &$r ] );
  39. $this->getResult()->addValue( null, $this->getModuleName(), $r );
  40. }
  41. public function mustBePosted() {
  42. return true;
  43. }
  44. public function getAllowedParams() {
  45. return [
  46. 'password' => [
  47. ApiBase::PARAM_TYPE => 'password',
  48. ApiBase::PARAM_REQUIRED => true
  49. ],
  50. 'user' => [
  51. ApiBase::PARAM_TYPE => 'user',
  52. ],
  53. 'email' => null,
  54. 'realname' => null,
  55. ];
  56. }
  57. protected function getExamplesMessages() {
  58. return [
  59. 'action=validatepassword&password=foobar'
  60. => 'apihelp-validatepassword-example-1',
  61. 'action=validatepassword&password=querty&user=Example'
  62. => 'apihelp-validatepassword-example-2',
  63. ];
  64. }
  65. public function getHelpUrls() {
  66. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
  67. }
  68. }