| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Extension\OATHAuth\Special; |
| 21 |
|
| 22 |
use ErrorPageError; |
| 23 |
use MediaWiki\Config\ConfigException; |
| 24 |
use MediaWiki\Extension\OATHAuth\HTMLForm\IManageForm; |
| 25 |
use MediaWiki\Extension\OATHAuth\IAuthKey; |
| 26 |
use MediaWiki\Extension\OATHAuth\IModule; |
| 27 |
use MediaWiki\Extension\OATHAuth\OATHAuthModuleRegistry; |
| 28 |
use MediaWiki\Extension\OATHAuth\OATHUser; |
| 29 |
use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
| 30 |
use MediaWiki\Html\Html; |
| 31 |
use MediaWiki\HTMLForm\HTMLForm; |
| 32 |
use MediaWiki\Message\Message; |
| 33 |
use MediaWiki\SpecialPage\SpecialPage; |
| 34 |
use MWException; |
| 35 |
use OOUI\ButtonWidget; |
| 36 |
use OOUI\HorizontalLayout; |
| 37 |
use OOUI\HtmlSnippet; |
| 38 |
use OOUI\LabelWidget; |
| 39 |
use OOUI\PanelLayout; |
| 40 |
use PermissionsError; |
| 41 |
use UserNotLoggedIn; |
| 42 |
|
| 43 |
class OATHManage extends SpecialPage { |
| 44 |
public const ACTION_ENABLE = 'enable'; |
| 45 |
public const ACTION_DISABLE = 'disable'; |
| 46 |
|
| 47 |
protected OATHAuthModuleRegistry $moduleRegistry; |
| 48 |
|
| 49 |
protected OATHUserRepository $userRepo; |
| 50 |
|
| 51 |
protected OATHUser $authUser; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected $action; |
| 57 |
|
| 58 |
protected ?IModule $requestedModule; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function __construct( OATHUserRepository $userRepo, OATHAuthModuleRegistry $moduleRegistry ) { |
| 70 |
|
| 71 |
|
| 72 |
parent::__construct( 'OATHManage', 'oathauth-enable' ); |
| 73 |
|
| 74 |
$this->userRepo = $userRepo; |
| 75 |
$this->moduleRegistry = $moduleRegistry; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
protected function getGroupName() { |
| 82 |
return 'login'; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
protected function getLoginSecurityLevel() { |
| 89 |
return $this->getName(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function execute( $subPage ) { |
| 96 |
$this->authUser = $this->userRepo->findByUser( $this->getUser() ); |
| 97 |
|
| 98 |
$this->getOutput()->enableOOUI(); |
| 99 |
$this->getOutput()->disallowUserJs(); |
| 100 |
$this->setAction(); |
| 101 |
$this->setModule(); |
| 102 |
|
| 103 |
parent::execute( $subPage ); |
| 104 |
|
| 105 |
if ( $this->requestedModule instanceof IModule ) { |
| 106 |
|
| 107 |
$this->clearPage(); |
| 108 |
if ( $this->shouldShowDisableWarning() ) { |
| 109 |
$this->showDisableWarning( $this->requestedModule ); |
| 110 |
return; |
| 111 |
} |
| 112 |
$this->addModuleHTML( $this->requestedModule ); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
$this->addGeneralHelp(); |
| 117 |
if ( $this->authUser->isTwoFactorAuthEnabled() ) { |
| 118 |
$this->addEnabledHTML(); |
| 119 |
if ( $this->hasAlternativeModules() ) { |
| 120 |
$this->addAlternativesHTML(); |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
$this->nothingEnabled(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function checkPermissions() { |
| 132 |
$this->requireNamedUser(); |
| 133 |
|
| 134 |
if ( !$this->authUser->getCentralId() ) { |
| 135 |
throw new ErrorPageError( |
| 136 |
'oathauth-enable', |
| 137 |
'oathauth-must-be-central', |
| 138 |
[ $this->getUser()->getName() ] |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
$canEnable = $this->getUser()->isAllowed( 'oathauth-enable' ); |
| 143 |
|
| 144 |
if ( $this->action === static::ACTION_ENABLE && !$canEnable ) { |
| 145 |
$this->displayRestrictionError(); |
| 146 |
} |
| 147 |
|
| 148 |
if ( !$this->authUser->isTwoFactorAuthEnabled() && !$canEnable ) { |
| 149 |
|
| 150 |
$this->displayRestrictionError(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
private function setAction(): void { |
| 155 |
$this->action = $this->getRequest()->getVal( 'action', '' ); |
| 156 |
} |
| 157 |
|
| 158 |
private function setModule(): void { |
| 159 |
$moduleKey = $this->getRequest()->getVal( 'module', '' ); |
| 160 |
$this->requestedModule = ( $moduleKey && $this->moduleRegistry->moduleExists( $moduleKey ) ) |
| 161 |
? $this->moduleRegistry->getModuleByKey( $moduleKey ) |
| 162 |
: null; |
| 163 |
} |
| 164 |
|
| 165 |
private function addEnabledHTML(): void { |
| 166 |
$this->addHeading( $this->msg( 'oathauth-ui-enabled-module' ) ); |
| 167 |
|
| 168 |
$modules = array_unique( |
| 169 |
array_map( |
| 170 |
static fn ( IAuthKey $key ) => $key->getModule(), |
| 171 |
$this->authUser->getKeys(), |
| 172 |
) |
| 173 |
); |
| 174 |
|
| 175 |
foreach ( $modules as $module ) { |
| 176 |
$this->addModuleHTML( $this->moduleRegistry->getModuleByKey( $module ) ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private function addAlternativesHTML(): void { |
| 181 |
$this->addHeading( $this->msg( 'oathauth-ui-not-enabled-modules' ) ); |
| 182 |
$this->addInactiveHTML(); |
| 183 |
} |
| 184 |
|
| 185 |
private function nothingEnabled(): void { |
| 186 |
$this->addHeading( $this->msg( 'oathauth-ui-available-modules' ) ); |
| 187 |
$this->addInactiveHTML(); |
| 188 |
} |
| 189 |
|
| 190 |
private function addInactiveHTML(): void { |
| 191 |
foreach ( $this->moduleRegistry->getAllModules() as $module ) { |
| 192 |
if ( $this->isModuleEnabled( $module ) || !$this->isModuleAvailable( $module ) ) { |
| 193 |
continue; |
| 194 |
} |
| 195 |
$this->addModuleHTML( $module ); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private function addGeneralHelp(): void { |
| 200 |
$this->getOutput()->addHTML( $this->msg( |
| 201 |
'oathauth-ui-general-help' |
| 202 |
)->parseAsBlock() ); |
| 203 |
} |
| 204 |
|
| 205 |
private function addModuleHTML( IModule $module ): void { |
| 206 |
if ( $this->isModuleRequested( $module ) ) { |
| 207 |
$this->addCustomContent( $module ); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
$panel = $this->getGenericContent( $module ); |
| 212 |
if ( $this->isModuleEnabled( $module ) ) { |
| 213 |
$this->addCustomContent( $module, $panel ); |
| 214 |
} |
| 215 |
|
| 216 |
$this->getOutput()->addHTML( (string)$panel ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private function getGenericContent( IModule $module ): PanelLayout { |
| 223 |
$modulePanel = new PanelLayout( [ |
| 224 |
'framed' => true, |
| 225 |
'expanded' => false, |
| 226 |
'padded' => true |
| 227 |
] ); |
| 228 |
$headerLayout = new HorizontalLayout(); |
| 229 |
|
| 230 |
$label = new LabelWidget( [ |
| 231 |
'label' => $module->getDisplayName()->text() |
| 232 |
] ); |
| 233 |
if ( $this->shouldShowGenericButtons() ) { |
| 234 |
$enabled = $this->isModuleEnabled( $module ); |
| 235 |
$button = new ButtonWidget( [ |
| 236 |
'label' => $this |
| 237 |
->msg( $enabled ? 'oathauth-disable-generic' : 'oathauth-enable-generic' ) |
| 238 |
->text(), |
| 239 |
'href' => $this->getOutput()->getTitle()->getLocalURL( [ |
| 240 |
'action' => $enabled ? static::ACTION_DISABLE : static::ACTION_ENABLE, |
| 241 |
'module' => $module->getName(), |
| 242 |
'warn' => 1 |
| 243 |
] ) |
| 244 |
] ); |
| 245 |
$headerLayout->addItems( [ $button ] ); |
| 246 |
} |
| 247 |
$headerLayout->addItems( [ $label ] ); |
| 248 |
|
| 249 |
$modulePanel->appendContent( $headerLayout ); |
| 250 |
$modulePanel->appendContent( new HtmlSnippet( |
| 251 |
$module->getDescriptionMessage()->parseAsBlock() |
| 252 |
) ); |
| 253 |
return $modulePanel; |
| 254 |
} |
| 255 |
|
| 256 |
private function addCustomContent( IModule $module, ?PanelLayout $panel = null ): void { |
| 257 |
$form = $module->getManageForm( |
| 258 |
$this->action, |
| 259 |
$this->authUser, |
| 260 |
$this->userRepo, |
| 261 |
$this->getContext() |
| 262 |
); |
| 263 |
if ( $form === null || !$this->isValidFormType( $form ) ) { |
| 264 |
return; |
| 265 |
} |
| 266 |
$form->setTitle( $this->getOutput()->getTitle() ); |
| 267 |
$this->ensureRequiredFormFields( $form, $module ); |
| 268 |
$form->setSubmitCallback( [ $form, 'onSubmit' ] ); |
| 269 |
if ( $form->show( $panel ) ) { |
| 270 |
$form->onSuccess(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
private function addHeading( Message $message ): void { |
| 275 |
$this->getOutput()->addHTML( Html::element( 'h2', [], $message->text() ) ); |
| 276 |
} |
| 277 |
|
| 278 |
private function shouldShowGenericButtons(): bool { |
| 279 |
return !$this->requestedModule instanceof IModule || !$this->isGenericAction(); |
| 280 |
} |
| 281 |
|
| 282 |
private function isModuleRequested( ?IModule $module ): bool { |
| 283 |
return ( |
| 284 |
$this->requestedModule instanceof IModule |
| 285 |
&& $module instanceof IModule |
| 286 |
&& $this->requestedModule->getName() === $module->getName() |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
private function isModuleEnabled( IModule $module ): bool { |
| 291 |
return (bool)$this->authUser->getKeysForModule( $module->getName() ); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private function isModuleAvailable( IModule $module ): bool { |
| 301 |
$form = $module->getManageForm( |
| 302 |
static::ACTION_ENABLE, |
| 303 |
$this->authUser, |
| 304 |
$this->userRepo, |
| 305 |
$this->getContext() |
| 306 |
); |
| 307 |
if ( $form === '' ) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private function isValidFormType( $form ): bool { |
| 320 |
if ( !( $form instanceof HTMLForm ) ) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
$implements = class_implements( $form ); |
| 324 |
if ( !isset( $implements[IManageForm::class] ) ) { |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
return true; |
| 329 |
} |
| 330 |
|
| 331 |
private function ensureRequiredFormFields( IManageForm $form, IModule $module ): void { |
| 332 |
if ( !$form->hasField( 'module' ) ) { |
| 333 |
$form->addHiddenField( 'module', $module->getName() ); |
| 334 |
} |
| 335 |
if ( !$form->hasField( 'action' ) ) { |
| 336 |
$form->addHiddenField( 'action', $this->action ); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
private function clearPage(): void { |
| 345 |
if ( $this->isGenericAction() ) { |
| 346 |
$displayName = $this->requestedModule->getDisplayName(); |
| 347 |
$pageTitleMessage = $this->isModuleEnabled( $this->requestedModule ) ? |
| 348 |
$this->msg( 'oathauth-disable-page-title', $displayName ) : |
| 349 |
$this->msg( 'oathauth-enable-page-title', $displayName ); |
| 350 |
$this->getOutput()->setPageTitleMsg( $pageTitleMessage ); |
| 351 |
} |
| 352 |
|
| 353 |
$this->getOutput()->clearHTML(); |
| 354 |
$this->getOutput()->addBacklinkSubtitle( $this->getOutput()->getTitle() ); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private function isGenericAction(): bool { |
| 362 |
return in_array( $this->action, [ static::ACTION_ENABLE, static::ACTION_DISABLE ] ); |
| 363 |
} |
| 364 |
|
| 365 |
private function hasAlternativeModules(): bool { |
| 366 |
foreach ( $this->moduleRegistry->getAllModules() as $module ) { |
| 367 |
if ( !$this->isModuleEnabled( $module ) && $this->isModuleAvailable( $module ) ) { |
| 368 |
return true; |
| 369 |
} |
| 370 |
} |
| 371 |
return false; |
| 372 |
} |
| 373 |
|
| 374 |
private function shouldShowDisableWarning(): bool { |
| 375 |
return $this->getRequest()->getBool( 'warn' ) && |
| 376 |
$this->requestedModule instanceof IModule && |
| 377 |
$this->authUser->isTwoFactorAuthEnabled(); |
| 378 |
} |
| 379 |
|
| 380 |
private function showDisableWarning( IModule $module ): void { |
| 381 |
$panel = new PanelLayout( [ |
| 382 |
'padded' => true, |
| 383 |
'framed' => true, |
| 384 |
'expanded' => false |
| 385 |
] ); |
| 386 |
|
| 387 |
$isSwitch = $this->isSwitch(); |
| 388 |
$currentDisplayName = $module->getDisplayName(); |
| 389 |
$newDisplayName = $this->requestedModule->getDisplayName(); |
| 390 |
|
| 391 |
$genericMessage = $isSwitch ? |
| 392 |
$this->msg( |
| 393 |
'oathauth-switch-method-warning', |
| 394 |
$currentDisplayName, |
| 395 |
$newDisplayName |
| 396 |
) : |
| 397 |
$this->msg( 'oathauth-disable-method-warning', $currentDisplayName ); |
| 398 |
|
| 399 |
$panel->appendContent( new HtmlSnippet( |
| 400 |
$genericMessage->parseAsBlock() |
| 401 |
) ); |
| 402 |
|
| 403 |
$customMessage = $module->getDisableWarningMessage(); |
| 404 |
if ( $customMessage instanceof Message ) { |
| 405 |
$panel->appendContent( new HtmlSnippet( |
| 406 |
$customMessage->parseAsBlock() |
| 407 |
) ); |
| 408 |
} |
| 409 |
|
| 410 |
$nextStepMessage = $isSwitch ? |
| 411 |
$this->msg( 'oathauth-switch-method-next-step', $currentDisplayName, $newDisplayName ) : |
| 412 |
$this->msg( 'oathauth-disable-method-next-step', $currentDisplayName ); |
| 413 |
|
| 414 |
$panel->appendContent( new HtmlSnippet( |
| 415 |
$nextStepMessage->parseAsBlock() |
| 416 |
) ); |
| 417 |
|
| 418 |
$button = new ButtonWidget( [ |
| 419 |
'label' => $this->msg( 'oathauth-disable-method-warning-button-label' )->plain(), |
| 420 |
'href' => $this->getOutput()->getTitle()->getLocalURL( [ |
| 421 |
'action' => $this->action, |
| 422 |
'module' => $this->requestedModule->getName() |
| 423 |
] ), |
| 424 |
'flags' => [ 'primary', 'progressive' ] |
| 425 |
] ); |
| 426 |
$panel->appendContent( $button ); |
| 427 |
|
| 428 |
$headerMessage = $isSwitch ? |
| 429 |
$this->msg( 'oathauth-switch-method-warning-header' ) : |
| 430 |
$this->msg( 'oathauth-disable-method-warning-header' ); |
| 431 |
|
| 432 |
$this->getOutput()->setPageTitleMsg( $headerMessage ); |
| 433 |
$this->getOutput()->addHTML( $panel->toString() ); |
| 434 |
} |
| 435 |
|
| 436 |
private function isSwitch(): bool { |
| 437 |
return $this->requestedModule instanceof IModule && |
| 438 |
$this->action === static::ACTION_ENABLE && |
| 439 |
$this->authUser->isTwoFactorAuthEnabled(); |
| 440 |
} |
| 441 |
|
| 442 |
} |