Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiValidatePassword.php
blob4602a9c44f43095eacd6e2df634ec76f6bdb93e0
1 <?php
3 namespace MediaWiki\Api;
5 use MediaWiki\Auth\AuthManager;
6 use MediaWiki\ParamValidator\TypeDef\UserDef;
7 use MediaWiki\User\UserFactory;
8 use MediaWiki\User\UserRigorOptions;
9 use Wikimedia\ParamValidator\ParamValidator;
11 /**
12 * @ingroup API
14 class ApiValidatePassword extends ApiBase {
16 private AuthManager $authManager;
17 private UserFactory $userFactory;
19 public function __construct(
20 ApiMain $mainModule,
21 string $moduleName,
22 AuthManager $authManager,
23 UserFactory $userFactory
24 ) {
25 parent::__construct( $mainModule, $moduleName );
26 $this->authManager = $authManager;
27 $this->userFactory = $userFactory;
30 public function execute() {
31 $params = $this->extractRequestParams();
33 $this->requirePostedParameters( [ 'password' ] );
35 if ( $params['user'] !== null ) {
36 $user = $this->userFactory->newFromName(
37 $params['user'],
38 UserRigorOptions::RIGOR_CREATABLE
40 if ( !$user ) {
41 $encParamName = $this->encodeParamName( 'user' );
42 $this->dieWithError(
43 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $params['user'] ) ],
44 "baduser_{$encParamName}"
48 if ( $user->isRegistered() || $this->authManager->userExists( $user->getName() ) ) {
49 $this->dieWithError( 'userexists' );
52 $user->setEmail( (string)$params['email'] );
53 $user->setRealName( (string)$params['realname'] );
54 } else {
55 $user = $this->getUser();
58 $r = [];
59 $validity = $user->checkPasswordValidity( $params['password'] );
60 $r['validity'] = $validity->isGood() ? 'Good' : ( $validity->isOK() ? 'Change' : 'Invalid' );
61 $messages = array_merge(
62 $this->getErrorFormatter()->arrayFromStatus( $validity, 'error' ),
63 $this->getErrorFormatter()->arrayFromStatus( $validity, 'warning' )
65 if ( $messages ) {
66 $r['validitymessages'] = $messages;
69 $this->getHookRunner()->onApiValidatePassword( $this, $r );
71 $this->getResult()->addValue( null, $this->getModuleName(), $r );
74 public function mustBePosted() {
75 return true;
78 public function getAllowedParams() {
79 return [
80 'password' => [
81 ParamValidator::PARAM_TYPE => 'password',
82 ParamValidator::PARAM_REQUIRED => true
84 'user' => [
85 ParamValidator::PARAM_TYPE => 'user',
86 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
88 'email' => null,
89 'realname' => null,
93 protected function getExamplesMessages() {
94 return [
95 'action=validatepassword&password=foobar'
96 => 'apihelp-validatepassword-example-1',
97 'action=validatepassword&password=querty&user=Example'
98 => 'apihelp-validatepassword-example-2',
102 public function getHelpUrls() {
103 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Validatepassword';
107 /** @deprecated class alias since 1.43 */
108 class_alias( ApiValidatePassword::class, 'ApiValidatePassword' );