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' );
55 * Main execution point
56 * @param string|null $par
58 function execute( $par ) {
59 $this->getOutput()->disallowUserJs();
60 $this->requireLogin();
63 if ( strlen( $par ) === 0 ) {
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() {
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
,
98 $sep = BotPassword
::getSeparator();
101 'label-message' => 'username',
102 'default' => $this->getUser()->getName() . $sep . $this->par
105 if ( $this->botPassword
->isSaved() ) {
106 $fields['resetPassword'] = [
108 'label-message' => 'botpasswords-label-resetpassword',
110 if ( $this->botPassword
->isInvalid() ) {
111 $fields['resetPassword']['default'] = true;
115 $lang = $this->getLanguage();
116 $showGrants = MWGrants
::getValidGrants();
117 $fields['grants'] = [
118 'type' => 'checkmatrix',
119 'label-message' => 'botpasswords-label-grants',
120 'help-message' => 'botpasswords-help-grants',
122 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
124 'rows' => array_combine(
125 array_map( 'MWGrants::getGrantsLink', $showGrants ),
128 'default' => array_map(
132 $this->botPassword
->getGrants()
134 'tooltips' => array_combine(
135 array_map( 'MWGrants::getGrantsLink', $showGrants ),
137 function ( $rights ) use ( $lang ) {
138 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
140 array_intersect_key( MWGrants
::getRightsByGrant(), array_flip( $showGrants ) )
143 'force-options-on' => array_map(
147 MWGrants
::getHiddenGrants()
151 $fields['restrictions'] = [
152 'class' => HTMLRestrictionsField
::class,
154 'default' => $this->botPassword
->getRestrictions(),
158 $linkRenderer = $this->getLinkRenderer();
159 $passwordFactory = new PasswordFactory();
160 $passwordFactory->init( $this->getConfig() );
162 $dbr = BotPassword
::getDB( DB_REPLICA
);
165 [ 'bp_app_id', 'bp_password' ],
166 [ 'bp_user' => $this->userId
],
169 foreach ( $res as $row ) {
171 $password = $passwordFactory->newFromCiphertext( $row->bp_password
);
172 $passwordInvalid = $password instanceof InvalidPassword
;
174 } catch ( PasswordError
$ex ) {
175 $passwordInvalid = true;
178 $text = $linkRenderer->makeKnownLink(
179 $this->getPageTitle( $row->bp_app_id
),
182 if ( $passwordInvalid ) {
183 $text .= $this->msg( 'word-separator' )->escaped()
184 . $this->msg( 'botpasswords-label-needsreset' )->parse();
188 'section' => 'existing',
196 'section' => 'createnew',
197 'type' => 'textwithbutton',
198 'label-message' => 'botpasswords-label-appid',
199 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
200 'buttonflags' => [ 'progressive', 'primary' ],
202 'size' => BotPassword
::APPID_MAXLENGTH
,
203 'maxlength' => BotPassword
::APPID_MAXLENGTH
,
204 'validation-callback' => function ( $v ) {
206 return $v !== '' && strlen( $v ) <= BotPassword
::APPID_MAXLENGTH
;
220 protected function alterForm( HTMLForm
$form ) {
221 $form->setId( 'mw-botpasswords-form' );
222 $form->setTableId( 'mw-botpasswords-table' );
223 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
224 $form->suppressDefaultSubmit();
226 if ( $this->par
!== null ) {
227 if ( $this->botPassword
->isSaved() ) {
228 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
232 'label-message' => 'botpasswords-label-update',
233 'flags' => [ 'primary', 'progressive' ],
238 'label-message' => 'botpasswords-label-delete',
239 'flags' => [ 'destructive' ],
242 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
246 'label-message' => 'botpasswords-label-create',
247 'flags' => [ 'primary', 'progressive' ],
254 'label-message' => 'botpasswords-label-cancel'
259 public function onSubmit( array $data ) {
260 $op = $this->getRequest()->getVal( 'op', '' );
264 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
268 $this->operation
= 'insert';
269 return $this->save( $data );
272 $this->operation
= 'update';
273 return $this->save( $data );
276 $this->operation
= 'delete';
277 $bp = BotPassword
::newFromCentralId( $this->userId
, $this->par
);
281 return Status
::newGood();
284 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
291 private function save( array $data ) {
292 $bp = BotPassword
::newUnsaved( [
293 'centralId' => $this->userId
,
294 'appId' => $this->par
,
295 'restrictions' => $data['restrictions'],
296 'grants' => array_merge(
297 MWGrants
::getHiddenGrants(),
298 preg_replace( '/^grant-/', '', $data['grants'] )
302 if ( $this->operation
=== 'insert' ||
!empty( $data['resetPassword'] ) ) {
303 $this->password
= BotPassword
::generatePassword( $this->getConfig() );
304 $passwordFactory = new PasswordFactory();
305 $passwordFactory->init( RequestContext
::getMain()->getConfig() );
306 $password = $passwordFactory->newFromPlaintext( $this->password
);
311 if ( $bp->save( $this->operation
, $password ) ) {
312 return Status
::newGood();
314 // Messages: botpasswords-insert-failed, botpasswords-update-failed
315 return Status
::newFatal( "botpasswords-{$this->operation}-failed", $this->par
);
319 public function onSuccess() {
320 $out = $this->getOutput();
322 $username = $this->getUser()->getName();
323 switch ( $this->operation
) {
325 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
326 $out->addWikiMsg( 'botpasswords-created-body', $this->par
, $username );
330 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
331 $out->addWikiMsg( 'botpasswords-updated-body', $this->par
, $username );
335 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
336 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par
, $username );
337 $this->password
= null;
341 if ( $this->password
!== null ) {
342 $sep = BotPassword
::getSeparator();
344 'botpasswords-newpassword',
345 htmlspecialchars( $username . $sep . $this->par
),
346 htmlspecialchars( $this->password
),
347 htmlspecialchars( $username ),
348 htmlspecialchars( $this->par
. $sep . $this->password
)
350 $this->password
= null;
353 $out->addReturnTo( $this->getPageTitle() );
356 protected function getGroupName() {
360 protected function getDisplayFormat() {