MediaWiki  master
ApiChangeAuthenticationData.php
Go to the documentation of this file.
1 <?php
25 
33  private $authManager;
34 
40  public function __construct(
41  ApiMain $main,
42  $action,
43  AuthManager $authManager
44  ) {
45  parent::__construct( $main, $action, 'changeauth' );
46  $this->authManager = $authManager;
47  }
48 
49  public function execute() {
50  if ( !$this->getUser()->isRegistered() ) {
51  $this->dieWithError( 'apierror-mustbeloggedin-changeauthenticationdata', 'notloggedin' );
52  }
53 
54  $helper = new ApiAuthManagerHelper( $this, $this->authManager );
55 
56  // Check security-sensitive operation status
57  $helper->securitySensitiveOperation( 'ChangeCredentials' );
58 
59  // Fetch the request
61  $helper->loadAuthenticationRequests( AuthManager::ACTION_CHANGE ),
62  $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist )
63  );
64  if ( count( $reqs ) !== 1 ) {
65  $this->dieWithError( 'apierror-changeauth-norequest', 'badrequest' );
66  }
67  $req = reset( $reqs );
68 
69  // Make the change
70  $status = $this->authManager->allowsAuthenticationDataChange( $req, true );
71  $this->getHookRunner()->onChangeAuthenticationDataAudit( $req, $status );
72  if ( !$status->isGood() ) {
73  $this->dieStatus( $status );
74  }
75  $this->authManager->changeAuthenticationData( $req );
76 
77  $this->getResult()->addValue( null, 'changeauthenticationdata', [ 'status' => 'success' ] );
78  }
79 
80  public function isWriteMode() {
81  return true;
82  }
83 
84  public function needsToken() {
85  return 'csrf';
86  }
87 
88  public function getAllowedParams() {
89  return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_CHANGE,
90  'request'
91  );
92  }
93 
94  public function dynamicParameterDocumentation() {
95  return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_CHANGE ];
96  }
97 
98  protected function getExamplesMessages() {
99  return [
100  'action=changeauthenticationdata' .
101  '&changeauthrequest=MediaWiki%5CAuth%5CPasswordAuthenticationRequest' .
102  '&password=ExamplePassword&retype=ExamplePassword&changeauthtoken=123ABC'
103  => 'apihelp-changeauthenticationdata-example-password',
104  ];
105  }
106 
107  public function getHelpUrls() {
108  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Manage_authentication_data';
109  }
110 }
Helper class for AuthManager-using API modules.
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
static blacklistAuthenticationRequests(array $reqs, array $remove)
Filter out authentication requests by class name.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:58
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1469
getResult()
Get the result object.
Definition: ApiBase.php:636
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1524
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition: ApiBase.php:720
Change authentication data with AuthManager.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiMain $main, $action, AuthManager $authManager)
getHelpUrls()
Return links to more detailed help pages about the module.
isWriteMode()
Indicates whether this module requires write mode.
needsToken()
Returns the token type this module requires in order to execute.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:59
This serves as the entry point to the authentication system.
A class containing constants representing the names of configuration variables.