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',
116 $lang = $this->getLanguage();
117 $showGrants = MWGrants
::getValidGrants();
118 $fields['grants'] = [
119 'type' => 'checkmatrix',
120 'label-message' => 'botpasswords-label-grants',
121 'help-message' => 'botpasswords-help-grants',
123 $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant'
125 'rows' => array_combine(
126 array_map( 'MWGrants::getGrantsLink', $showGrants ),
129 'default' => array_map(
133 $this->botPassword
->getGrants()
135 'tooltips' => array_combine(
136 array_map( 'MWGrants::getGrantsLink', $showGrants ),
138 function ( $rights ) use ( $lang ) {
139 return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) );
141 array_intersect_key( MWGrants
::getRightsByGrant(), array_flip( $showGrants ) )
144 'force-options-on' => array_map(
148 MWGrants
::getHiddenGrants()
152 $fields['restrictions'] = [
153 'class' => HTMLRestrictionsField
::class,
155 'default' => $this->botPassword
->getRestrictions(),
159 $linkRenderer = $this->getLinkRenderer();
160 $dbr = BotPassword
::getDB( DB_REPLICA
);
164 [ 'bp_user' => $this->userId
],
167 foreach ( $res as $row ) {
169 'section' => 'existing',
172 'default' => $linkRenderer->makeKnownLink(
173 $this->getPageTitle( $row->bp_app_id
),
180 'section' => 'createnew',
181 'type' => 'textwithbutton',
182 'label-message' => 'botpasswords-label-appid',
183 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
184 'buttonflags' => [ 'progressive', 'primary' ],
186 'size' => BotPassword
::APPID_MAXLENGTH
,
187 'maxlength' => BotPassword
::APPID_MAXLENGTH
,
188 'validation-callback' => function ( $v ) {
190 return $v !== '' && strlen( $v ) <= BotPassword
::APPID_MAXLENGTH
;
204 protected function alterForm( HTMLForm
$form ) {
205 $form->setId( 'mw-botpasswords-form' );
206 $form->setTableId( 'mw-botpasswords-table' );
207 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
208 $form->suppressDefaultSubmit();
210 if ( $this->par
!== null ) {
211 if ( $this->botPassword
->isSaved() ) {
212 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
216 'label-message' => 'botpasswords-label-update',
217 'flags' => [ 'primary', 'progressive' ],
222 'label-message' => 'botpasswords-label-delete',
223 'flags' => [ 'destructive' ],
226 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
230 'label-message' => 'botpasswords-label-create',
231 'flags' => [ 'primary', 'progressive' ],
238 'label-message' => 'botpasswords-label-cancel'
243 public function onSubmit( array $data ) {
244 $op = $this->getRequest()->getVal( 'op', '' );
248 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
252 $this->operation
= 'insert';
253 return $this->save( $data );
256 $this->operation
= 'update';
257 return $this->save( $data );
260 $this->operation
= 'delete';
261 $bp = BotPassword
::newFromCentralId( $this->userId
, $this->par
);
265 return Status
::newGood();
268 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
275 private function save( array $data ) {
276 $bp = BotPassword
::newUnsaved( [
277 'centralId' => $this->userId
,
278 'appId' => $this->par
,
279 'restrictions' => $data['restrictions'],
280 'grants' => array_merge(
281 MWGrants
::getHiddenGrants(),
282 preg_replace( '/^grant-/', '', $data['grants'] )
286 if ( $this->operation
=== 'insert' ||
!empty( $data['resetPassword'] ) ) {
287 $this->password
= BotPassword
::generatePassword( $this->getConfig() );
288 $passwordFactory = new PasswordFactory();
289 $passwordFactory->init( RequestContext
::getMain()->getConfig() );
290 $password = $passwordFactory->newFromPlaintext( $this->password
);
295 if ( $bp->save( $this->operation
, $password ) ) {
296 return Status
::newGood();
298 // Messages: botpasswords-insert-failed, botpasswords-update-failed
299 return Status
::newFatal( "botpasswords-{$this->operation}-failed", $this->par
);
303 public function onSuccess() {
304 $out = $this->getOutput();
306 $username = $this->getUser()->getName();
307 switch ( $this->operation
) {
309 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
310 $out->addWikiMsg( 'botpasswords-created-body', $this->par
, $username );
314 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
315 $out->addWikiMsg( 'botpasswords-updated-body', $this->par
, $username );
319 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
320 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par
, $username );
321 $this->password
= null;
325 if ( $this->password
!== null ) {
326 $sep = BotPassword
::getSeparator();
328 'botpasswords-newpassword',
329 htmlspecialchars( $username . $sep . $this->par
),
330 htmlspecialchars( $this->password
),
331 htmlspecialchars( $username ),
332 htmlspecialchars( $this->par
. $sep . $this->password
)
334 $this->password
= null;
337 $out->addReturnTo( $this->getPageTitle() );
340 protected function getGroupName() {
344 protected function getDisplayFormat() {