AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / SpecialBotPasswords.php
blob7e330aa63f3e25d2d2a55f1529c764c022a73fcf
1 <?php
2 /**
3 * Implements Special:BotPasswords
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users manage bot passwords
27 * @ingroup SpecialPage
29 class SpecialBotPasswords extends FormSpecialPage {
31 /** @var int Central user ID */
32 private $userId = 0;
34 /** @var BotPassword|null Bot password being edited, if any */
35 private $botPassword = null;
37 /** @var string Operation being performed: create, update, delete */
38 private $operation = null;
40 /** @var string New password set, for communication between onSubmit() and onSuccess() */
41 private $password = null;
43 public function __construct() {
44 parent::__construct( 'BotPasswords', 'editmyprivateinfo' );
47 /**
48 * @return bool
50 public function isListed() {
51 return $this->getConfig()->get( 'EnableBotPasswords' );
54 /**
55 * Main execution point
56 * @param string|null $par
58 function execute( $par ) {
59 $this->getOutput()->disallowUserJs();
60 $this->requireLogin();
62 $par = trim( $par );
63 if ( strlen( $par ) === 0 ) {
64 $par = null;
65 } elseif ( strlen( $par ) > BotPassword::APPID_MAXLENGTH ) {
66 throw new ErrorPageError( 'botpasswords', 'botpasswords-bad-appid',
67 [ htmlspecialchars( $par ) ] );
70 parent::execute( $par );
73 protected function checkExecutePermissions( User $user ) {
74 parent::checkExecutePermissions( $user );
76 if ( !$this->getConfig()->get( 'EnableBotPasswords' ) ) {
77 throw new ErrorPageError( 'botpasswords', 'botpasswords-disabled' );
80 $this->userId = CentralIdLookup::factory()->centralIdFromLocalUser( $this->getUser() );
81 if ( !$this->userId ) {
82 throw new ErrorPageError( 'botpasswords', 'botpasswords-no-central-id' );
86 protected function getFormFields() {
87 $fields = [];
89 if ( $this->par !== null ) {
90 $this->botPassword = BotPassword::newFromCentralId( $this->userId, $this->par );
91 if ( !$this->botPassword ) {
92 $this->botPassword = BotPassword::newUnsaved( [
93 'centralId' => $this->userId,
94 'appId' => $this->par,
95 ] );
98 $sep = BotPassword::getSeparator();
99 $fields[] = [
100 'type' => 'info',
101 'label-message' => 'username',
102 'default' => $this->getUser()->getName() . $sep . $this->par
105 if ( $this->botPassword->isSaved() ) {
106 $fields['resetPassword'] = [
107 'type' => 'check',
108 'label-message' => 'botpasswords-label-resetpassword',
112 $lang = $this->getLanguage();
113 $showGrants = MWGrants::getValidGrants();
114 $fields['grants'] = [
115 'type' => 'checkmatrix',
116 'label-message' => 'botpasswords-label-grants',
117 'help-message' => 'botpasswords-help-grants',
118 'columns' => [
119 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
121 'rows' => array_combine(
122 array_map( 'MWGrants::getGrantsLink', $showGrants ),
123 $showGrants
125 'default' => array_map(
126 function( $g ) {
127 return "grant-$g";
129 $this->botPassword->getGrants()
131 'tooltips' => array_combine(
132 array_map( 'MWGrants::getGrantsLink', $showGrants ),
133 array_map(
134 function( $rights ) use ( $lang ) {
135 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
137 array_intersect_key( MWGrants::getRightsByGrant(), array_flip( $showGrants ) )
140 'force-options-on' => array_map(
141 function( $g ) {
142 return "grant-$g";
144 MWGrants::getHiddenGrants()
148 $fields['restrictions'] = [
149 'type' => 'textarea',
150 'label-message' => 'botpasswords-label-restrictions',
151 'required' => true,
152 'default' => $this->botPassword->getRestrictions()->toJson( true ),
153 'rows' => 5,
154 'validation-callback' => function ( $v ) {
155 try {
156 MWRestrictions::newFromJson( $v );
157 return true;
158 } catch ( InvalidArgumentException $ex ) {
159 return $ex->getMessage();
164 } else {
165 $linkRenderer = $this->getLinkRenderer();
166 $dbr = BotPassword::getDB( DB_SLAVE );
167 $res = $dbr->select(
168 'bot_passwords',
169 [ 'bp_app_id' ],
170 [ 'bp_user' => $this->userId ],
171 __METHOD__
173 foreach ( $res as $row ) {
174 $fields[] = [
175 'section' => 'existing',
176 'type' => 'info',
177 'raw' => true,
178 'default' => $linkRenderer->makeKnownLink(
179 $this->getPageTitle( $row->bp_app_id ),
180 $row->bp_app_id
185 $fields['appId'] = [
186 'section' => 'createnew',
187 'type' => 'textwithbutton',
188 'label-message' => 'botpasswords-label-appid',
189 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
190 'buttonflags' => [ 'progressive', 'primary' ],
191 'required' => true,
192 'size' => BotPassword::APPID_MAXLENGTH,
193 'maxlength' => BotPassword::APPID_MAXLENGTH,
194 'validation-callback' => function ( $v ) {
195 $v = trim( $v );
196 return $v !== '' && strlen( $v ) <= BotPassword::APPID_MAXLENGTH;
200 $fields[] = [
201 'type' => 'hidden',
202 'default' => 'new',
203 'name' => 'op',
207 return $fields;
210 protected function alterForm( HTMLForm $form ) {
211 $form->setId( 'mw-botpasswords-form' );
212 $form->setTableId( 'mw-botpasswords-table' );
213 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
214 $form->suppressDefaultSubmit();
216 if ( $this->par !== null ) {
217 if ( $this->botPassword->isSaved() ) {
218 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
219 $form->addButton( [
220 'name' => 'op',
221 'value' => 'update',
222 'label-message' => 'botpasswords-label-update',
223 'flags' => [ 'primary', 'progressive' ],
224 ] );
225 $form->addButton( [
226 'name' => 'op',
227 'value' => 'delete',
228 'label-message' => 'botpasswords-label-delete',
229 'flags' => [ 'destructive' ],
230 ] );
231 } else {
232 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
233 $form->addButton( [
234 'name' => 'op',
235 'value' => 'create',
236 'label-message' => 'botpasswords-label-create',
237 'flags' => [ 'primary', 'constructive' ],
238 ] );
241 $form->addButton( [
242 'name' => 'op',
243 'value' => 'cancel',
244 'label-message' => 'botpasswords-label-cancel'
245 ] );
249 public function onSubmit( array $data ) {
250 $op = $this->getRequest()->getVal( 'op', '' );
252 switch ( $op ) {
253 case 'new':
254 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
255 return false;
257 case 'create':
258 $this->operation = 'insert';
259 return $this->save( $data );
261 case 'update':
262 $this->operation = 'update';
263 return $this->save( $data );
265 case 'delete':
266 $this->operation = 'delete';
267 $bp = BotPassword::newFromCentralId( $this->userId, $this->par );
268 if ( $bp ) {
269 $bp->delete();
271 return Status::newGood();
273 case 'cancel':
274 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
275 return false;
278 return false;
281 private function save( array $data ) {
282 $bp = BotPassword::newUnsaved( [
283 'centralId' => $this->userId,
284 'appId' => $this->par,
285 'restrictions' => MWRestrictions::newFromJson( $data['restrictions'] ),
286 'grants' => array_merge(
287 MWGrants::getHiddenGrants(),
288 preg_replace( '/^grant-/', '', $data['grants'] )
290 ] );
292 if ( $this->operation === 'insert' || !empty( $data['resetPassword'] ) ) {
293 $this->password = PasswordFactory::generateRandomPasswordString(
294 max( 32, $this->getConfig()->get( 'MinimalPasswordLength' ) )
296 $passwordFactory = new PasswordFactory();
297 $passwordFactory->init( RequestContext::getMain()->getConfig() );
298 $password = $passwordFactory->newFromPlaintext( $this->password );
299 } else {
300 $password = null;
303 if ( $bp->save( $this->operation, $password ) ) {
304 return Status::newGood();
305 } else {
306 // Messages: botpasswords-insert-failed, botpasswords-update-failed
307 return Status::newFatal( "botpasswords-{$this->operation}-failed", $this->par );
311 public function onSuccess() {
312 $out = $this->getOutput();
314 $username = $this->getUser()->getName();
315 switch ( $this->operation ) {
316 case 'insert':
317 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
318 $out->addWikiMsg( 'botpasswords-created-body', $this->par, $username );
319 break;
321 case 'update':
322 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
323 $out->addWikiMsg( 'botpasswords-updated-body', $this->par, $username );
324 break;
326 case 'delete':
327 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
328 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par, $username );
329 $this->password = null;
330 break;
333 if ( $this->password !== null ) {
334 $sep = BotPassword::getSeparator();
335 $out->addWikiMsg(
336 'botpasswords-newpassword',
337 htmlspecialchars( $username . $sep . $this->par ),
338 htmlspecialchars( $this->password )
340 $this->password = null;
343 $out->addReturnTo( $this->getPageTitle() );
346 protected function getGroupName() {
347 return 'users';
350 protected function getDisplayFormat() {
351 return 'ooui';