Set lang in api createaccount regardless of $wgLoginLanguageSelector
[lhc/web/wiklou.git] / includes / api / ApiCreateAccount.php
index 3f059d6..1eb1aa5 100644 (file)
  */
 class ApiCreateAccount extends ApiBase {
        public function execute() {
+
+               // $loginForm->addNewaccountInternal will throw exceptions
+               // if wiki is read only (already handled by api), user is blocked or does not have rights.
+               // Use userCan in order to hit GlobalBlock checks (according to Special:userlogin)
+               $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
+               if ( !$loginTitle->userCan( 'createaccount', $this->getUser() ) ) {
+                       $this->dieUsage( 'You do not have the right to create a new account', 'permdenied-createaccount' );
+               }
+               if ( $this->getUser()->isBlockedFromCreateAccount() ) {
+                       $this->dieUsage( 'You cannot create a new account because you are blocked', 'blocked' );
+               }
+
                $params = $this->extractRequestParams();
 
                $result = array();
@@ -42,6 +54,10 @@ class ApiCreateAccount extends ApiBase {
                        $this->dieUsageMsg( 'noemail' );
                }
 
+               if ( $params['language'] && !Language::isSupportedLanguage( $params['language'] ) ) {
+                       $this->dieUsage( 'Invalid language parameter', 'langinvalid' );
+               }
+
                $context = new DerivativeContext( $this->getContext() );
                $context->setRequest( new DerivativeRequest(
                        $this->getContext()->getRequest(),
@@ -68,12 +84,10 @@ class ApiCreateAccount extends ApiBase {
                $result = array();
                if( $status->isGood() ) {
                        // Success!
+                       global $wgEmailAuthentication;
                        $user = $status->getValue();
 
-                       // If we showed up language selection links, and one was in use, be
-                       // smart (and sensible) and save that language as the user's preference
-                       global $wgLoginLanguageSelector, $wgEmailAuthentication;
-                       if( $wgLoginLanguageSelector && $params['language'] ) {
+                       if( $params['language'] ) {
                                $user->setOption( 'language', $params['language'] );
                        }
 
@@ -230,16 +244,19 @@ class ApiCreateAccount extends ApiBase {
        }
 
        public function getPossibleErrors() {
+               // Note the following errors aren't possible and don't need to be listed:
+               // sessionfailure, nocookiesfornew, badretype
                $localErrors = array(
-                       'wrongpassword',
+                       'wrongpassword', // Actually caused by wrong domain field. Riddle me that...
                        'sorbs_create_account_reason',
                        'noname',
                        'userexists',
-                       'password-name-match',
-                       'password-login-forbidden',
+                       'password-name-match', // from User::getPasswordValidity
+                       'password-login-forbidden', // from User::getPasswordValidity
                        'noemailtitle',
                        'invalidemailaddress',
-                       'externaldberror'
+                       'externaldberror',
+                       'acct_creation_throttle_hit',
                );
 
                $errors = parent::getPossibleErrors();
@@ -248,6 +265,23 @@ class ApiCreateAccount extends ApiBase {
                        $errors[] = array( 'code' => $error, 'info' => wfMessage( $error )->parse() );
                }
 
+               $errors[] = array(
+                       'code' => 'permdenied-createaccount',
+                       'info' => 'You do not have the right to create a new account'
+               );
+               $errors[] = array(
+                       'code' => 'blocked',
+                       'info' => 'You cannot create a new account because you are blocked'
+               );
+               $errors[] = array(
+                       'code' => 'aborted',
+                       'info' => 'Account creation aborted by hook (info may vary)'
+               );
+               $errors[] = array(
+                       'code' => 'langinvalid',
+                       'info' => 'Invalid language parameter'
+               );
+
                // 'passwordtooshort' has parameters. :(
                global $wgMinimalPasswordLength;
                $errors[] = array(