50 parent::__construct(
'BotPasswords',
'editmyprivateinfo' );
51 $this->logger = LoggerFactory::getInstance(
'authentication' );
58 return $this->
getConfig()->get(
'EnableBotPasswords' );
75 if ( strlen(
$par ) === 0 ) {
77 } elseif ( strlen(
$par ) > BotPassword::APPID_MAXLENGTH ) {
78 throw new ErrorPageError(
'botpasswords',
'botpasswords-bad-appid',
79 [ htmlspecialchars(
$par ) ] );
82 parent::execute(
$par );
86 parent::checkExecutePermissions( $user );
88 if ( !$this->
getConfig()->
get(
'EnableBotPasswords' ) ) {
89 throw new ErrorPageError(
'botpasswords',
'botpasswords-disabled' );
92 $this->userId = CentralIdLookup::factory()->centralIdFromLocalUser( $this->
getUser() );
93 if ( !$this->userId ) {
94 throw new ErrorPageError(
'botpasswords',
'botpasswords-no-central-id' );
101 if ( $this->par !==
null ) {
102 $this->botPassword = BotPassword::newFromCentralId( $this->userId, $this->par );
103 if ( !$this->botPassword ) {
104 $this->botPassword = BotPassword::newUnsaved( [
105 'centralId' => $this->userId,
106 'appId' => $this->par,
110 $sep = BotPassword::getSeparator();
113 'label-message' =>
'username',
117 if ( $this->botPassword->isSaved() ) {
118 $fields[
'resetPassword'] = [
120 'label-message' =>
'botpasswords-label-resetpassword',
122 if ( $this->botPassword->isInvalid() ) {
123 $fields[
'resetPassword'][
'default'] =
true;
129 $fields[
'grants'] = [
130 'type' =>
'checkmatrix',
131 'label-message' =>
'botpasswords-label-grants',
132 'help-message' =>
'botpasswords-help-grants',
134 $this->
msg(
'botpasswords-label-grants-column' )->escaped() =>
'grant'
136 'rows' => array_combine(
137 array_map(
'MWGrants::getGrantsLink', $showGrants ),
140 'default' => array_map(
144 $this->botPassword->getGrants()
146 'tooltips' => array_combine(
147 array_map(
'MWGrants::getGrantsLink', $showGrants ),
149 function ( $rights ) use (
$lang ) {
150 return $lang->semicolonList( array_map(
'User::getRightDescription', $rights ) );
155 'force-options-on' => array_map(
163 $fields[
'restrictions'] = [
164 'class' => HTMLRestrictionsField::class,
166 'default' => $this->botPassword->getRestrictions(),
171 $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
176 [
'bp_app_id',
'bp_password' ],
177 [
'bp_user' => $this->userId ],
180 foreach (
$res as $row ) {
182 $password = $passwordFactory->newFromCiphertext( $row->bp_password );
186 $passwordInvalid =
true;
193 if ( $passwordInvalid ) {
194 $text .= $this->
msg(
'word-separator' )->escaped()
195 . $this->
msg(
'botpasswords-label-needsreset' )->parse();
199 'section' =>
'existing',
207 'section' =>
'createnew',
208 'type' =>
'textwithbutton',
209 'label-message' =>
'botpasswords-label-appid',
210 'buttondefault' => $this->
msg(
'botpasswords-label-create' )->text(),
211 'buttonflags' => [
'progressive',
'primary' ],
213 'size' => BotPassword::APPID_MAXLENGTH,
214 'maxlength' => BotPassword::APPID_MAXLENGTH,
215 'validation-callback' =>
function ( $v ) {
217 return $v !==
'' && strlen( $v ) <= BotPassword::APPID_MAXLENGTH;
232 $form->
setId(
'mw-botpasswords-form' );
234 $form->
addPreText( $this->
msg(
'botpasswords-summary' )->parseAsBlock() );
237 if ( $this->par !==
null ) {
238 if ( $this->botPassword->isSaved() ) {
243 'label-message' =>
'botpasswords-label-update',
244 'flags' => [
'primary',
'progressive' ],
249 'label-message' =>
'botpasswords-label-delete',
250 'flags' => [
'destructive' ],
257 'label-message' =>
'botpasswords-label-create',
258 'flags' => [
'primary',
'progressive' ],
265 'label-message' =>
'botpasswords-label-cancel'
271 $op = $this->
getRequest()->getVal(
'op',
'' );
279 $this->operation =
'insert';
280 return $this->
save( $data );
283 $this->operation =
'update';
284 return $this->
save( $data );
287 $this->operation =
'delete';
288 $bp = BotPassword::newFromCentralId( $this->userId, $this->par );
292 "Bot password {op} for {user}@{app_id}",
294 'app_id' => $this->par,
296 'centralId' => $this->userId,
302 return Status::newGood();
312 private function save( array $data ) {
313 $bp = BotPassword::newUnsaved( [
314 'centralId' => $this->userId,
315 'appId' => $this->par,
316 'restrictions' => $data[
'restrictions'],
317 'grants' => array_merge(
321 preg_replace(
'/^grant-/',
'', $data[
'grants'] )
325 if ( $this->operation ===
'insert' || !empty( $data[
'resetPassword'] ) ) {
326 $this->password = BotPassword::generatePassword( $this->
getConfig() );
327 $passwordFactory = MediaWikiServices::getInstance()->getPasswordFactory();
328 $password = $passwordFactory->newFromPlaintext( $this->password );
333 if ( $bp->save( $this->operation,
$password ) ) {
335 "Bot password {op} for {user}@{app_id}",
337 'op' => $this->operation,
339 'app_id' => $this->par,
340 'centralId' => $this->userId,
341 'restrictions' => $data[
'restrictions'],
342 'grants' => $bp->getGrants(),
343 'client_ip' => $this->getRequest()->getIP()
346 return Status::newGood();
349 return Status::newFatal(
"botpasswords-{$this->operation}-failed", $this->par );
356 $username = $this->
getUser()->getName();
357 switch ( $this->operation ) {
359 $out->setPageTitle( $this->
msg(
'botpasswords-created-title' )->text() );
360 $out->addWikiMsg(
'botpasswords-created-body', $this->par, $username );
364 $out->setPageTitle( $this->
msg(
'botpasswords-updated-title' )->text() );
365 $out->addWikiMsg(
'botpasswords-updated-body', $this->par, $username );
369 $out->setPageTitle( $this->
msg(
'botpasswords-deleted-title' )->text() );
370 $out->addWikiMsg(
'botpasswords-deleted-body', $this->par, $username );
371 $this->password =
null;
375 if ( $this->password !==
null ) {
376 $sep = BotPassword::getSeparator();
378 'botpasswords-newpassword',
379 htmlspecialchars( $username . $sep . $this->par ),
380 htmlspecialchars( $this->password ),
381 htmlspecialchars( $username ),
382 htmlspecialchars( $this->par . $sep . $this->password )
384 $this->password =
null;
Utility class for bot passwords.
An error page which can definitely be safely rendered using the OutputPage.
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
Represents an invalid password hash.
static getHiddenGrants()
Get the list of grants that are hidden and should always be granted.
static getRightsByGrant()
Map all grants to corresponding user rights.
static getValidGrants()
List all known grants.
Show an error when any operation involving passwords fails to run.
Let users manage bot passwords.
string $password
New password set, for communication between onSubmit() and onSuccess()
BotPassword null $botPassword
Bot password being edited, if any.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
execute( $par)
Main execution point.
getFormFields()
Get an HTMLForm descriptor array.
onSubmit(array $data)
Process the form on POST submission.
checkExecutePermissions(User $user)
Called from execute() to check if the given user can perform this action.
getDisplayFormat()
Get display format for the form.
getLoginSecurityLevel()
Tells if the special page does something security-sensitive and needs extra defense against a stolen ...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
int $userId
Central user ID.
string $operation
Operation being performed: create, update, delete.
Psr Log LoggerInterface $logger
getName()
Get the name of this Special Page.
getOutput()
Get the OutputPage being used for this instance.
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
getUser()
Shortcut to get the User executing this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
MediaWiki Linker LinkRenderer null $linkRenderer
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
if(!isset( $args[0])) $lang