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
21 * @ingroup SpecialPage
25 * Let users manage bot passwords
27 * @ingroup SpecialPage
29 class SpecialBotPasswords
extends FormSpecialPage
{
31 /** @var int Central user ID */
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' );
50 public function isListed() {
51 return $this->getConfig()->get( 'EnableBotPasswords' );
54 protected function getLoginSecurityLevel() {
55 return $this->getName();
59 * Main execution point
60 * @param string|null $par
62 function execute( $par ) {
63 $this->getOutput()->disallowUserJs();
64 $this->requireLogin();
67 if ( strlen( $par ) === 0 ) {
69 } elseif ( strlen( $par ) > BotPassword
::APPID_MAXLENGTH
) {
70 throw new ErrorPageError( 'botpasswords', 'botpasswords-bad-appid',
71 [ htmlspecialchars( $par ) ] );
74 parent
::execute( $par );
77 protected function checkExecutePermissions( User
$user ) {
78 parent
::checkExecutePermissions( $user );
80 if ( !$this->getConfig()->get( 'EnableBotPasswords' ) ) {
81 throw new ErrorPageError( 'botpasswords', 'botpasswords-disabled' );
84 $this->userId
= CentralIdLookup
::factory()->centralIdFromLocalUser( $this->getUser() );
85 if ( !$this->userId
) {
86 throw new ErrorPageError( 'botpasswords', 'botpasswords-no-central-id' );
90 protected function getFormFields() {
93 if ( $this->par
!== null ) {
94 $this->botPassword
= BotPassword
::newFromCentralId( $this->userId
, $this->par
);
95 if ( !$this->botPassword
) {
96 $this->botPassword
= BotPassword
::newUnsaved( [
97 'centralId' => $this->userId
,
98 'appId' => $this->par
,
102 $sep = BotPassword
::getSeparator();
105 'label-message' => 'username',
106 'default' => $this->getUser()->getName() . $sep . $this->par
109 if ( $this->botPassword
->isSaved() ) {
110 $fields['resetPassword'] = [
112 'label-message' => 'botpasswords-label-resetpassword',
114 if ( $this->botPassword
->isInvalid() ) {
115 $fields['resetPassword']['default'] = true;
119 $lang = $this->getLanguage();
120 $showGrants = MWGrants
::getValidGrants();
121 $fields['grants'] = [
122 'type' => 'checkmatrix',
123 'label-message' => 'botpasswords-label-grants',
124 'help-message' => 'botpasswords-help-grants',
126 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
128 'rows' => array_combine(
129 array_map( 'MWGrants::getGrantsLink', $showGrants ),
132 'default' => array_map(
136 $this->botPassword
->getGrants()
138 'tooltips' => array_combine(
139 array_map( 'MWGrants::getGrantsLink', $showGrants ),
141 function ( $rights ) use ( $lang ) {
142 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
144 array_intersect_key( MWGrants
::getRightsByGrant(), array_flip( $showGrants ) )
147 'force-options-on' => array_map(
151 MWGrants
::getHiddenGrants()
155 $fields['restrictions'] = [
156 'class' => HTMLRestrictionsField
::class,
158 'default' => $this->botPassword
->getRestrictions(),
162 $linkRenderer = $this->getLinkRenderer();
163 $passwordFactory = new PasswordFactory();
164 $passwordFactory->init( $this->getConfig() );
166 $dbr = BotPassword
::getDB( DB_REPLICA
);
169 [ 'bp_app_id', 'bp_password' ],
170 [ 'bp_user' => $this->userId
],
173 foreach ( $res as $row ) {
175 $password = $passwordFactory->newFromCiphertext( $row->bp_password
);
176 $passwordInvalid = $password instanceof InvalidPassword
;
178 } catch ( PasswordError
$ex ) {
179 $passwordInvalid = true;
182 $text = $linkRenderer->makeKnownLink(
183 $this->getPageTitle( $row->bp_app_id
),
186 if ( $passwordInvalid ) {
187 $text .= $this->msg( 'word-separator' )->escaped()
188 . $this->msg( 'botpasswords-label-needsreset' )->parse();
192 'section' => 'existing',
200 'section' => 'createnew',
201 'type' => 'textwithbutton',
202 'label-message' => 'botpasswords-label-appid',
203 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
204 'buttonflags' => [ 'progressive', 'primary' ],
206 'size' => BotPassword
::APPID_MAXLENGTH
,
207 'maxlength' => BotPassword
::APPID_MAXLENGTH
,
208 'validation-callback' => function ( $v ) {
210 return $v !== '' && strlen( $v ) <= BotPassword
::APPID_MAXLENGTH
;
224 protected function alterForm( HTMLForm
$form ) {
225 $form->setId( 'mw-botpasswords-form' );
226 $form->setTableId( 'mw-botpasswords-table' );
227 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
228 $form->suppressDefaultSubmit();
230 if ( $this->par
!== null ) {
231 if ( $this->botPassword
->isSaved() ) {
232 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
236 'label-message' => 'botpasswords-label-update',
237 'flags' => [ 'primary', 'progressive' ],
242 'label-message' => 'botpasswords-label-delete',
243 'flags' => [ 'destructive' ],
246 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
250 'label-message' => 'botpasswords-label-create',
251 'flags' => [ 'primary', 'progressive' ],
258 'label-message' => 'botpasswords-label-cancel'
263 public function onSubmit( array $data ) {
264 $op = $this->getRequest()->getVal( 'op', '' );
268 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
272 $this->operation
= 'insert';
273 return $this->save( $data );
276 $this->operation
= 'update';
277 return $this->save( $data );
280 $this->operation
= 'delete';
281 $bp = BotPassword
::newFromCentralId( $this->userId
, $this->par
);
285 return Status
::newGood();
288 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
295 private function save( array $data ) {
296 $bp = BotPassword
::newUnsaved( [
297 'centralId' => $this->userId
,
298 'appId' => $this->par
,
299 'restrictions' => $data['restrictions'],
300 'grants' => array_merge(
301 MWGrants
::getHiddenGrants(),
302 preg_replace( '/^grant-/', '', $data['grants'] )
306 if ( $this->operation
=== 'insert' ||
!empty( $data['resetPassword'] ) ) {
307 $this->password
= BotPassword
::generatePassword( $this->getConfig() );
308 $passwordFactory = new PasswordFactory();
309 $passwordFactory->init( RequestContext
::getMain()->getConfig() );
310 $password = $passwordFactory->newFromPlaintext( $this->password
);
315 if ( $bp->save( $this->operation
, $password ) ) {
316 return Status
::newGood();
318 // Messages: botpasswords-insert-failed, botpasswords-update-failed
319 return Status
::newFatal( "botpasswords-{$this->operation}-failed", $this->par
);
323 public function onSuccess() {
324 $out = $this->getOutput();
326 $username = $this->getUser()->getName();
327 switch ( $this->operation
) {
329 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
330 $out->addWikiMsg( 'botpasswords-created-body', $this->par
, $username );
334 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
335 $out->addWikiMsg( 'botpasswords-updated-body', $this->par
, $username );
339 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
340 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par
, $username );
341 $this->password
= null;
345 if ( $this->password
!== null ) {
346 $sep = BotPassword
::getSeparator();
348 'botpasswords-newpassword',
349 htmlspecialchars( $username . $sep . $this->par
),
350 htmlspecialchars( $this->password
),
351 htmlspecialchars( $username ),
352 htmlspecialchars( $this->par
. $sep . $this->password
)
354 $this->password
= null;
357 $out->addReturnTo( $this->getPageTitle() );
360 protected function getGroupName() {
364 protected function getDisplayFormat() {