Parent5446 has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/53860


Change subject: Cleaned up extension and refactored Special Page.
......................................................................

Cleaned up extension and refactored Special Page.

Made SpecialDisableAccount use FormSpecialPage since it uses
an HTMLForm already. Adjusted the form attributes to make
processing more uniform.

Also added message documentation (qqq) as well as code
documentation that was missing.

Change-Id: Ifb4a1d0b445b284ea57323bc7671a312e1d23e91
---
M DisableAccount.i18n.php
M DisableAccount_body.php
2 files changed, 80 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DisableAccount 
refs/changes/60/53860/1

diff --git a/DisableAccount.i18n.php b/DisableAccount.i18n.php
index a41b3a8..d62dde7 100644
--- a/DisableAccount.i18n.php
+++ b/DisableAccount.i18n.php
@@ -8,14 +8,16 @@
        'disableaccount-desc' => 'Allows administrators to disable individual 
accounts',
        'right-disableaccount' => 'Disable accounts',
        'disableaccount' => 'Disable a user account',
-       'disableaccount-user' => 'User name:',
-       'disableaccount-confirm' => "Disable this user account.
-The user will not be able to log in, reset their password, or receive email 
notifications.
+       'disableaccount-user' => 'Username:',
+       'disableaccount-legend' => 'User information',
+       'disableaccount-text' => "The user will not be able to log in, reset 
their password, or receive email notifications.
 If the user is currently logged in anywhere, they will be immediately logged 
out.
 ''Note that disabling an account is not reversible without system 
administrator intervention.''",
+       'disableaccount-confirm' => 'Are you sure you want to disable this 
account?',
        'disableaccount-mustconfirm' => 'You must confirm that you wish to 
disable this account.',
        'disableaccount-nosuchuser' => 'The user account "$1" does not exist.',
        'disableaccount-success' => 'The user account "$1" has been permanently 
disabled.',
+       'action-disableaccount' => 'disable user accounts',
 );
 
 /** Message documentation (Message documentation)
@@ -30,8 +32,17 @@
        'right-disableaccount' => '{{doc-right|disableaccount}}',
        'disableaccount' => 'Name of the special page',
        'disableaccount-user' => '{{Identical|Username}}',
-       'disableaccount-confirm' => '{{doc-singularthey}}
+       'disableaccount-legend' => 'Label for the <legend> that the 
Special:DisableAccount form is in.',
+       'disableaccount-text' => 'Instructions displayed at the top of the form 
on Special:DisableAccount.
+{{doc-singularthey}}
 A system administrator here is one having access to the system the wiki is 
running on beyond that of a sysop or a bureaucrat. In Wikimedia terminology 
they have \'shell access\'. This message refers to "no return without direct 
database changes."',
+       'disableaccount-confirm' => 'Label for the confirmation checkbox on 
Special:DisableAccount.',
+       'disableaccount-mustconfirm' => 'Validation error message displayed 
when the user fails to check the confirmation box',
+       'disableaccount-nosuchuser' => 'Validation error message displayed when 
the requested user does not exist
+$1 - The username that was supplied that does not exist',
+       'disableaccount-success' => 'Message displayed upon successful 
disabling of a user.
+$1 - The user\'s username that was disabled',
+       'action-disableaccount' => 'Message displayed when a user does not have 
the disableaccount permission and a PermissionError (ErrorPageError) is thrown'
 );
 
 /** Afrikaans (Afrikaans)
diff --git a/DisableAccount_body.php b/DisableAccount_body.php
index 1ffb08e..6ecc4c0 100644
--- a/DisableAccount_body.php
+++ b/DisableAccount_body.php
@@ -1,53 +1,44 @@
 <?php
 
-class SpecialDisableAccount extends SpecialPage {
+/**
+ * Implements Special:DisableAccount, which allows authorized users
+ * to disable other users.
+ */
+class SpecialDisableAccount extends FormSpecialPage {
        function __construct() {
-               parent::__construct( 'DisableAccount', 'disableaccount',
-                                       true, array( $this, 'show' ) );
+               parent::__construct( 'DisableAccount', 'disableaccount' );
        }
 
-       public function show( $par ) {
-               $formFields = array(
+       function getFormFields() {
+               return array(
                        'account' => array(
                                'type' => 'text',
-                               'validation-callback' => array( __CLASS__, 
'validateUser' ),
+                               'required' => true,
+                               'filter-callback' => function( $field ) { 
return $field ? User::newFromName( $field ) : ''; },
+                               'validation-callback' => array( $this, 
'validateUser' ),
                                'label-message' => 'disableaccount-user',
                        ),
                        'confirm' => array(
                                'type' => 'toggle',
-                               'validation-callback' => array( __CLASS__, 
'checkConfirmation' ),
+                               'required' => true,
+                               'validation-callback' => array( $this, 
'checkConfirmation' ),
                                'label-message' => 'disableaccount-confirm',
                        ),
                );
-
-               $htmlForm = new HTMLForm( $formFields, 'disableaccount' );
-
-               $htmlForm->setSubmitCallback( array( __CLASS__, 'submit' ) );
-               $htmlForm->setTitle( $this->getTitle() );
-
-               $htmlForm->show();
        }
 
-       static function validateUser( $field, $allFields ) {
-               $u = User::newFromName( $field );
-
-               if ( $u && $u->getID() != 0 ) {
-                       return true;
-               } else {
-                       return wfMessage( 'disableaccount-nosuchuser', array( 
$field ) )->parse();
-               }
-       }
-
-       static function checkConfirmation( $field, $allFields ) {
-               if ( $field ) {
-                       return true;
-               } else {
-                       return wfMessage( 'disableaccount-mustconfirm' 
)->parse();
-               }
-       }
-
-       static function submit( $fields ) {
-               $user = User::newFromName( $fields['account'] );
+       /**
+        * Disable a user account based on HTMLForm fields.
+        *
+        * Set the user's email and password to null so they cannot log in or 
reset their password.
+        * Then reset their token to log them out everywhere. Finally, add them 
to a user group
+        * for inactive users and save settings.
+        *
+        * @param array $fields Fields from HTMLForm
+        * @return bool True
+        */
+       function onSubmit( array $fields ) {
+               $user = $fields['account'];
 
                $user->setPassword( null );
                $user->setEmail( null );
@@ -55,11 +46,47 @@
                $user->addGroup( 'inactive' );
 
                $user->saveSettings();
-               $user->invalidateCache();
-
-               global $wgOut;
-               $wgOut->addWikiMsg( 'disableaccount-success', $user->getName() 
);
+               $this->getOutput()->addWikiMsg( 'disableaccount-success', 
$user->getName() );
 
                return true;
        }
+
+       /**
+        * Do nothing on success because it's handled in the submit function.
+        *
+        * @see SpecialDisableAccount::onSubmit
+        */
+       function onSuccess() {}
+
+       /**
+        * Validate a user object passed by the form.
+        *
+        * Check if the user object is indeed a user object, and then check if 
the user
+        * exists or not. Return an appropriate error message.
+        *
+        * @param bool|User $u User object or false if invalid username
+        * @return bool|string True on success, otherwise returns a validation 
error
+        */
+       function validateUser( $u ) {
+               if ( !$u ) {
+                       // Invalid username.
+                       return false;
+               } elseif ( $u->isAnon() ) {
+                       // Non-existant username.
+                       return $this->msg( 'disableaccount-nosuchuser', 
$u->getName() )->escaped();
+               } else {
+                       // All good.
+                       return true;
+               }
+       }
+
+       /**
+        * Check to see if the user checked the confirmation box.
+        *
+        * @param bool $field The value of the confirmation field
+        * @return bool|string True on success, otherwise returns a validation 
error
+        */
+       function checkConfirmation( $field ) {
+               return $field ?: $this->msg( 'disableaccount-mustconfirm' 
)->escaped();
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/53860
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifb4a1d0b445b284ea57323bc7671a312e1d23e91
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DisableAccount
Gerrit-Branch: master
Gerrit-Owner: Parent5446 <tylerro...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to