Debian Codegraph @

extensions/OATHAuth/src/Special/DisableOATHForUser.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\OATHAuth\Special;
4
5 use ManualLogEntry;
6 use MediaWiki\CheckUser\Hooks as CheckUserHooks;
7 use MediaWiki\Config\ConfigException;
8 use MediaWiki\Extension\OATHAuth\OATHUserRepository;
9 use MediaWiki\HTMLForm\HTMLForm;
10 use MediaWiki\Logger\LoggerFactory;
11 use MediaWiki\Registration\ExtensionRegistry;
12 use MediaWiki\SpecialPage\FormSpecialPage;
13 use MediaWiki\User\CentralId\CentralIdLookup;
14 use MediaWiki\User\User;
15 use MediaWiki\User\UserFactory;
16 use MWException;
17 use UserBlockedError;
18 use UserNotLoggedIn;
19
20 class DisableOATHForUser extends FormSpecialPage {
21
22 private OATHUserRepository $userRepo;
23
24 private UserFactory $userFactory;
25
26 private CentralIdLookup $centralIdLookup;
27
28 /**
29 * @param OATHUserRepository $userRepo
30 * @param UserFactory $userFactory
31 * @param CentralIdLookup $centralIdLookup
32 */
33 public function __construct( $userRepo, $userFactory, $centralIdLookup ) {
34 // messages used: disableoathforuser (display "name" on Special:SpecialPages),
35 // right-oathauth-disable-for-user, action-oathauth-disable-for-user
36 parent::__construct( 'DisableOATHForUser', 'oathauth-disable-for-user' );
37
38 $this->userRepo = $userRepo;
39 $this->userFactory = $userFactory;
40 $this->centralIdLookup = $centralIdLookup;
41 }
42
43 /**
44 * @inheritDoc
45 */
46 protected function getGroupName() {
47 return 'users';
48 }
49
50 public function doesWrites() {
51 return true;
52 }
53
54 /**
55 * @return string
56 */
57 protected function getLoginSecurityLevel() {
58 return $this->getName();
59 }
60
61 /**
62 * Set the page title and add JavaScript RL modules
63 *
64 * @param HTMLForm $form
65 */
66 public function alterForm( HTMLForm $form ) {
67 $form->setMessagePrefix( 'oathauth' );
68 $form->setWrapperLegendMsg( 'oathauth-disable-for-user' );
69 $form->setPreHtml( $this->msg( 'oathauth-disable-intro' )->parse() );
70 $form->getOutput()->setPageTitleMsg( $this->msg( 'oathauth-disable-for-user' ) );
71 }
72
73 /**
74 * @return string
75 */
76 protected function getDisplayFormat() {
77 return 'ooui';
78 }
79
80 /**
81 * @param User $user
82 * @throws UserBlockedError
83 * @throws UserNotLoggedIn
84 */
85 protected function checkExecutePermissions( User $user ) {
86 $this->requireNamedUser();
87
88 parent::checkExecutePermissions( $user );
89 }
90
91 /**
92 * @param string $par
93 */
94 public function execute( $par ) {
95 $this->getOutput()->disallowUserJs();
96 parent::execute( $par );
97 }
98
99 /**
100 * @return array[]
101 */
102 protected function getFormFields() {
103 return [
104 'user' => [
105 'type' => 'user',
106 'default' => '',
107 'label-message' => 'oathauth-enteruser',
108 'name' => 'user',
109 'required' => true,
110 'excludetemp' => true,
111 ],
112 'reason' => [
113 'type' => 'text',
114 'default' => '',
115 'label-message' => 'oathauth-enterdisablereason',
116 'name' => 'reason',
117 'required' => true,
118 ],
119 ];
120 }
121
122 /**
123 * @param array $formData
124 * @return array|bool
125 * @throws ConfigException
126 * @throws MWException
127 */
128 public function onSubmit( array $formData ) {
129 $user = $this->userFactory->newFromName( $formData['user'] );
130 // T393253 - Check the username is valid, but don't check if it exists on the local wiki.
131 // Instead, check there is a valid central ID.
132 if ( !$user || $this->centralIdLookup->centralIdFromName( $formData['user'] ) === 0 ) {
133 return [ 'oathauth-user-not-found' ];
134 }
135
136 if ( $this->getUser()->pingLimiter( 'disableoath' ) ) {
137 return [ 'oathauth-throttled' ];
138 }
139
140 $oathUser = $this->userRepo->findByUser( $user );
141
142 if ( !$oathUser->isTwoFactorAuthEnabled() ) {
143 return [ 'oathauth-user-not-does-not-have-oath-enabled' ];
144 }
145
146 $this->userRepo->removeAll( $oathUser, $this->getRequest()->getIP(), false );
147
148 // messages used: logentry-oath-disable-other, log-action-oath-disable-other
149 $logEntry = new ManualLogEntry( 'oath', 'disable-other' );
150 $logEntry->setPerformer( $this->getUser() );
151 $logEntry->setTarget( $user->getUserPage() );
152 $logEntry->setComment( $formData['reason'] );
153 $logEntry->insert();
154
155 if ( ExtensionRegistry::getInstance()->isLoaded( 'CheckUser' ) ) {
156 CheckUserHooks::updateCheckUserData( $logEntry->getRecentChange() );
157 }
158
159 LoggerFactory::getInstance( 'authentication' )->info(
160 'OATHAuth disabled for {usertarget} by {user} from {clientip}', [
161 'user' => $this->getUser()->getName(),
162 'usertarget' => $formData['user'],
163 'clientip' => $this->getRequest()->getIP(),
164 ]
165 );
166
167 return true;
168 }
169
170 public function onSuccess() {
171 $this->getOutput()->addWikiMsg( 'oathauth-disabledoath' );
172 $this->getOutput()->returnToMain();
173 }
174
175 }