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 array( 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() {
88 $user = $this->getUser();
89 $request = $this->getRequest();
93 if ( $this->par
!== null ) {
94 $this->botPassword
= BotPassword
::newFromCentralId( $this->userId
, $this->par
);
95 if ( !$this->botPassword
) {
96 $this->botPassword
= BotPassword
::newUnsaved( array(
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'] = array(
112 'label-message' => 'botpasswords-label-resetpassword',
116 $lang = $this->getLanguage();
117 $showGrants = MWGrants
::getValidGrants();
118 $fields['grants'] = array(
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'] = array(
153 'type' => 'textarea',
154 'label-message' => 'botpasswords-label-restrictions',
156 'default' => $this->botPassword
->getRestrictions()->toJson( true ),
158 'validation-callback' => function ( $v ) {
160 MWRestrictions
::newFromJson( $v );
162 } catch ( InvalidArgumentException
$ex ) {
163 return $ex->getMessage();
169 $dbr = BotPassword
::getDB( DB_SLAVE
);
172 array( 'bp_app_id' ),
173 array( 'bp_user' => $this->userId
),
176 foreach ( $res as $row ) {
178 'section' => 'existing',
181 'default' => Linker
::link(
182 $this->getPageTitle( $row->bp_app_id
),
183 htmlspecialchars( $row->bp_app_id
),
191 $fields['appId'] = array(
192 'section' => 'createnew',
193 'type' => 'textwithbutton',
194 'label-message' => 'botpasswords-label-appid',
195 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(),
197 'size' => BotPassword
::APPID_MAXLENGTH
,
198 'maxlength' => BotPassword
::APPID_MAXLENGTH
,
199 'validation-callback' => function ( $v ) {
201 return $v !== '' && strlen( $v ) <= BotPassword
::APPID_MAXLENGTH
;
215 protected function alterForm( HTMLForm
$form ) {
216 $form->setId( 'mw-botpasswords-form' );
217 $form->setTableId( 'mw-botpasswords-table' );
218 $form->addPreText( $this->msg( 'botpasswords-summary' )->parseAsBlock() );
219 $form->suppressDefaultSubmit();
221 if ( $this->par
!== null ) {
222 if ( $this->botPassword
->isSaved() ) {
223 $form->setWrapperLegendMsg( 'botpasswords-editexisting' );
224 $form->addButton( array(
227 'label-message' => 'botpasswords-label-update',
228 'flags' => array( 'primary', 'progressive' ),
230 $form->addButton( array(
233 'label-message' => 'botpasswords-label-delete',
234 'flags' => array( 'destructive' ),
237 $form->setWrapperLegendMsg( 'botpasswords-createnew' );
238 $form->addButton( array(
241 'label-message' => 'botpasswords-label-create',
242 'flags' => array( 'primary', 'constructive' ),
246 $form->addButton( array(
249 'label-message' => 'botpasswords-label-cancel'
254 public function onSubmit( array $data ) {
255 $op = $this->getRequest()->getVal( 'op', '' );
259 $this->getOutput()->redirect( $this->getPageTitle( $data['appId'] )->getFullURL() );
263 $this->operation
= 'insert';
264 return $this->save( $data );
267 $this->operation
= 'update';
268 return $this->save( $data );
271 $this->operation
= 'delete';
272 $bp = BotPassword
::newFromCentralId( $this->userId
, $this->par
);
276 return Status
::newGood();
279 $this->getOutput()->redirect( $this->getPageTitle()->getFullURL() );
286 private function save( array $data ) {
287 $bp = BotPassword
::newUnsaved( array(
288 'centralId' => $this->userId
,
289 'appId' => $this->par
,
290 'restrictions' => MWRestrictions
::newFromJson( $data['restrictions'] ),
291 'grants' => array_merge(
292 MWGrants
::getHiddenGrants(),
293 preg_replace( '/^grant-/', '', $data['grants'] )
297 if ( $this->operation
=== 'insert' ||
!empty( $data['resetPassword'] ) ) {
298 $this->password
= PasswordFactory
::generateRandomPasswordString(
299 max( 32, $this->getConfig()->get( 'MinimalPasswordLength' ) )
301 $passwordFactory = new PasswordFactory();
302 $passwordFactory->init( RequestContext
::getMain()->getConfig() );
303 $password = $passwordFactory->newFromPlaintext( $this->password
);
308 if ( $bp->save( $this->operation
, $password ) ) {
309 return Status
::newGood();
311 // Messages: botpasswords-insert-failed, botpasswords-update-failed
312 return Status
::newFatal( "botpasswords-{$this->operation}-failed", $this->par
);
316 public function onSuccess() {
317 $out = $this->getOutput();
319 switch ( $this->operation
) {
321 $out->setPageTitle( $this->msg( 'botpasswords-created-title' )->text() );
322 $out->addWikiMsg( 'botpasswords-created-body', $this->par
);
326 $out->setPageTitle( $this->msg( 'botpasswords-updated-title' )->text() );
327 $out->addWikiMsg( 'botpasswords-updated-body', $this->par
);
331 $out->setPageTitle( $this->msg( 'botpasswords-deleted-title' )->text() );
332 $out->addWikiMsg( 'botpasswords-deleted-body', $this->par
);
333 $this->password
= null;
337 if ( $this->password
!== null ) {
338 $sep = BotPassword
::getSeparator();
340 'botpasswords-newpassword',
341 htmlspecialchars( $this->getUser()->getName() . $sep . $this->par
),
342 htmlspecialchars( $this->password
)
344 $this->password
= null;
347 $out->addReturnTo( $this->getPageTitle() );
350 protected function getGroupName() {
354 protected function getDisplayFormat() {