MediaWiki  1.34.4
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  public function execute( $par ) {
78  $this->getOutput()->disallowUserJs();
79  parent::execute( $par );
80  }
81 
85  protected function getFormFields() {
86  return [
87  'user' => [
88  'type' => 'user',
89  'default' => '',
90  'label-message' => 'oathauth-enteruser',
91  'name' => 'user',
92  'required' => true,
93  ],
94  'reason' => [
95  'type' => 'text',
96  'default' => '',
97  'label-message' => 'oathauth-enterreason',
98  'name' => 'reason',
99  'required' => true,
100  ],
101  ];
102  }
103 
110  public function onSubmit( array $formData ) {
111  $user = User::newFromName( $formData['user'] );
112  if ( $user && $user->getId() === 0 ) {
113  return [ 'oathauth-user-not-found' ];
114  }
115  $oathUser = $this->userRepo->findByUser( $user );
116 
117  if ( !( $oathUser->getModule() instanceof IModule ) ||
118  !$oathUser->getModule()->isEnabled( $oathUser ) ) {
119  return [ 'oathauth-user-not-does-not-have-oath-enabled' ];
120  }
121 
122  if ( $this->getUser()->pingLimiter( 'disableoath', 0 ) ) {
123  // Arbitrary duration given here
124  return [ 'oathauth-throttled', Message::durationParam( 60 ) ];
125  }
126 
127  $oathUser->disable();
128  $this->userRepo->remove( $oathUser, $this->getRequest()->getIP() );
129 
130  $logEntry = new ManualLogEntry( 'oath', 'disable-other' );
131  $logEntry->setPerformer( $this->getUser() );
132  $logEntry->setTarget( $user->getUserPage() );
133  $logEntry->setComment( $formData['reason'] );
134  $logEntry->insert();
135 
136  LoggerFactory::getInstance( 'authentication' )->info(
137  'OATHAuth disabled for {usertarget} by {user} from {clientip}', [
138  'user' => $this->getUser()->getName(),
139  'usertarget' => $formData['user'],
140  'clientip' => $this->getRequest()->getIP(),
141  ]
142  );
143 
144  return true;
145  }
146 
147  public function onSuccess() {
148  $this->getOutput()->addWikiMsg( 'oathauth-disabledoath' );
149  $this->getOutput()->returnToMain();
150  }
151 
152 }
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:518
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:34
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
MediaWiki\Extension\OATHAuth\Special\DisableOATHForUser\execute
execute( $par)
Definition: DisableOATHForUser.php:77
FormSpecialPage\$par
string null $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
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:85
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
Message
The Message class provides methods which fulfil two basic services:
Definition: Message.php:162
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:147
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
Message\durationParam
static durationParam( $duration)
Definition: Message.php:1049
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:110
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