MediaWiki  1.34.0
DisableOATHForUser.php
Go to the documentation of this file.
1 <?php
2 
4 
10 use HTMLForm;
11 use User;
13 use UserNotLoggedIn;
14 use ConfigException;
15 use Message;
16 use MWException;
17 use ManualLogEntry;
18 
21  private $userRepo;
22 
23  public function __construct() {
24  parent::__construct( 'DisableOATHForUser', 'oathauth-disable-for-user' );
25 
26  $this->userRepo = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' );
27  }
28 
29  public function doesWrites() {
30  return true;
31  }
32 
33  protected function getLoginSecurityLevel() {
34  return $this->getName();
35  }
36 
42  public function alterForm( HTMLForm $form ) {
43  $form->setMessagePrefix( 'oathauth' );
44  $form->setWrapperLegend( $this->msg( 'oathauth-disable-header' ) );
45  $form->setPreText( $this->msg( 'oathauth-disable-intro' ) );
46  $form->getOutput()->setPageTitle( $this->msg( 'oathauth-disable-for-user' ) );
47  }
48 
52  protected function getDisplayFormat() {
53  return 'ooui';
54  }
55 
59  public function requiresUnblock() {
60  return false;
61  }
62 
68  protected function checkExecutePermissions( User $user ) {
69  parent::checkExecutePermissions( $user );
70 
71  $this->requireLogin();
72  }
73 
77  protected function getFormFields() {
78  return [
79  'user' => [
80  'type' => 'user',
81  'default' => '',
82  'label-message' => 'oathauth-enteruser',
83  'name' => 'user',
84  'required' => true,
85  ],
86  'reason' => [
87  'type' => 'text',
88  'default' => '',
89  'label-message' => 'oathauth-enterreason',
90  'name' => 'reason',
91  'required' => true,
92  ],
93  ];
94  }
95 
102  public function onSubmit( array $formData ) {
103  $user = User::newFromName( $formData['user'] );
104  if ( $user && $user->getId() === 0 ) {
105  return [ 'oathauth-user-not-found' ];
106  }
107  $oathUser = $this->userRepo->findByUser( $user );
108 
109  if ( !( $oathUser->getModule() instanceof IModule ) ||
110  !$oathUser->getModule()->isEnabled( $oathUser ) ) {
111  return [ 'oathauth-user-not-does-not-have-oath-enabled' ];
112  }
113 
114  if ( $this->getUser()->pingLimiter( 'disableoath', 0 ) ) {
115  // Arbitrary duration given here
116  return [ 'oathauth-throttled', Message::durationParam( 60 ) ];
117  }
118 
119  $oathUser->disable();
120  $this->userRepo->remove( $oathUser, $this->getRequest()->getIP() );
121 
122  $logEntry = new ManualLogEntry( 'oath', 'disable-other' );
123  $logEntry->setPerformer( $this->getUser() );
124  $logEntry->setTarget( $user->getUserPage() );
125  $logEntry->setComment( $formData['reason'] );
126  $logEntry->insert();
127 
128  LoggerFactory::getInstance( 'authentication' )->info(
129  'OATHAuth disabled for {usertarget} by {user} from {clientip}', [
130  'user' => $this->getUser()->getName(),
131  'usertarget' => $formData['user'],
132  'clientip' => $this->getRequest()->getIP(),
133  ]
134  );
135 
136  return true;
137  }
138 
139  public function onSuccess() {
140  $this->getOutput()->addWikiMsg( 'oathauth-disabledoath' );
141  $this->getOutput()->returnToMain();
142  }
143 
144 }
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: DisableOATHForUser.php:29
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
HTMLForm\setPreText
setPreText( $msg)
Set the introductory message HTML, overwriting any existing message.
Definition: HTMLForm.php:764
UserBlockedError
Show an error when the user tries to do something whilst blocked.
Definition: UserBlockedError.php:29
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\__construct
__construct()
Definition: DisableOATHForUser.php:23
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
MediaWiki\Logger\LoggerFactory\getInstance
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
Definition: LoggerFactory.php:92
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser
Definition: DisableOATHForUser.php:19
UserNotLoggedIn
Redirect a user to the login page.
Definition: UserNotLoggedIn.php:53
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\getDisplayFormat
getDisplayFormat()
Definition: DisableOATHForUser.php:52
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
Message
SpecialPage\getName
getName()
Get the name of this Special Page.
Definition: SpecialPage.php:153
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
MediaWiki\Extension\OATHAuth\OATHUserRepository
Definition: OATHUserRepository.php:33
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
ConfigException
Exceptions for config failures.
Definition: ConfigException.php:28
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
HTMLForm\setMessagePrefix
setMessagePrefix( $p)
Set the prefix for various default messages.
Definition: HTMLForm.php:1593
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:345
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\checkExecutePermissions
checkExecutePermissions(User $user)
Definition: DisableOATHForUser.php:68
MediaWiki\Extension\OATHAuth\Special
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: DisableOATHForUser.php:3
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\getFormFields
getFormFields()
Definition: DisableOATHForUser.php:77
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
HTMLForm\setWrapperLegend
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
Definition: HTMLForm.php:1547
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\getLoginSecurityLevel
getLoginSecurityLevel()
Tells if the special page does something security-sensitive and needs extra defense against a stolen ...
Definition: DisableOATHForUser.php:33
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\requiresUnblock
requiresUnblock()
Definition: DisableOATHForUser.php:59
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:37
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: DisableOATHForUser.php:139
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\$userRepo
OATHUserRepository $userRepo
Definition: DisableOATHForUser.php:21
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\alterForm
alterForm(HTMLForm $form)
Set the page title and add JavaScript RL modules.
Definition: DisableOATHForUser.php:42
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\onSubmit
onSubmit(array $formData)
Definition: DisableOATHForUser.php:102
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131
MediaWiki\Extension\OATHAuth\IModule
Definition: IModule.php:9