userRepo = $userRepo; $this->moduleRegistry = $moduleRegistry; } /** * @inheritDoc */ protected function getGroupName() { return 'login'; } /** * @inheritDoc */ protected function getLoginSecurityLevel() { return $this->getName(); } /** * @param null|string $subPage */ public function execute( $subPage ) { $this->authUser = $this->userRepo->findByUser( $this->getUser() ); $this->getOutput()->enableOOUI(); $this->getOutput()->disallowUserJs(); $this->setAction(); $this->setModule(); parent::execute( $subPage ); if ( $this->requestedModule instanceof IModule ) { // Performing an action on a requested module $this->clearPage(); if ( $this->shouldShowDisableWarning() ) { $this->showDisableWarning( $this->requestedModule ); return; } $this->addModuleHTML( $this->requestedModule ); return; } $this->addGeneralHelp(); if ( $this->authUser->isTwoFactorAuthEnabled() ) { $this->addEnabledHTML(); if ( $this->hasAlternativeModules() ) { $this->addAlternativesHTML(); } return; } $this->nothingEnabled(); } /** * @throws PermissionsError * @throws UserNotLoggedIn */ public function checkPermissions() { $this->requireNamedUser(); if ( !$this->authUser->getCentralId() ) { throw new ErrorPageError( 'oathauth-enable', 'oathauth-must-be-central', [ $this->getUser()->getName() ] ); } $canEnable = $this->getUser()->isAllowed( 'oathauth-enable' ); if ( $this->action === static::ACTION_ENABLE && !$canEnable ) { $this->displayRestrictionError(); } if ( !$this->authUser->isTwoFactorAuthEnabled() && !$canEnable ) { // No enabled module and cannot enable - nothing to do $this->displayRestrictionError(); } } private function setAction(): void { $this->action = $this->getRequest()->getVal( 'action', '' ); } private function setModule(): void { $moduleKey = $this->getRequest()->getVal( 'module', '' ); $this->requestedModule = ( $moduleKey && $this->moduleRegistry->moduleExists( $moduleKey ) ) ? $this->moduleRegistry->getModuleByKey( $moduleKey ) : null; } private function addEnabledHTML(): void { $this->addHeading( $this->msg( 'oathauth-ui-enabled-module' ) ); $modules = array_unique( array_map( static fn ( IAuthKey $key ) => $key->getModule(), $this->authUser->getKeys(), ) ); foreach ( $modules as $module ) { $this->addModuleHTML( $this->moduleRegistry->getModuleByKey( $module ) ); } } private function addAlternativesHTML(): void { $this->addHeading( $this->msg( 'oathauth-ui-not-enabled-modules' ) ); $this->addInactiveHTML(); } private function nothingEnabled(): void { $this->addHeading( $this->msg( 'oathauth-ui-available-modules' ) ); $this->addInactiveHTML(); } private function addInactiveHTML(): void { foreach ( $this->moduleRegistry->getAllModules() as $module ) { if ( $this->isModuleEnabled( $module ) || !$this->isModuleAvailable( $module ) ) { continue; } $this->addModuleHTML( $module ); } } private function addGeneralHelp(): void { $this->getOutput()->addHTML( $this->msg( 'oathauth-ui-general-help' )->parseAsBlock() ); } private function addModuleHTML( IModule $module ): void { if ( $this->isModuleRequested( $module ) ) { $this->addCustomContent( $module ); return; } $panel = $this->getGenericContent( $module ); if ( $this->isModuleEnabled( $module ) ) { $this->addCustomContent( $module, $panel ); } $this->getOutput()->addHTML( (string)$panel ); } /** * Get the panel with generic content for a module */ private function getGenericContent( IModule $module ): PanelLayout { $modulePanel = new PanelLayout( [ 'framed' => true, 'expanded' => false, 'padded' => true ] ); $headerLayout = new HorizontalLayout(); $label = new LabelWidget( [ 'label' => $module->getDisplayName()->text() ] ); if ( $this->shouldShowGenericButtons() ) { $enabled = $this->isModuleEnabled( $module ); $button = new ButtonWidget( [ 'label' => $this ->msg( $enabled ? 'oathauth-disable-generic' : 'oathauth-enable-generic' ) ->text(), 'href' => $this->getOutput()->getTitle()->getLocalURL( [ 'action' => $enabled ? static::ACTION_DISABLE : static::ACTION_ENABLE, 'module' => $module->getName(), 'warn' => 1 ] ) ] ); $headerLayout->addItems( [ $button ] ); } $headerLayout->addItems( [ $label ] ); $modulePanel->appendContent( $headerLayout ); $modulePanel->appendContent( new HtmlSnippet( $module->getDescriptionMessage()->parseAsBlock() ) ); return $modulePanel; } private function addCustomContent( IModule $module, ?PanelLayout $panel = null ): void { $form = $module->getManageForm( $this->action, $this->authUser, $this->userRepo, $this->getContext() ); if ( $form === null || !$this->isValidFormType( $form ) ) { return; } $form->setTitle( $this->getOutput()->getTitle() ); $this->ensureRequiredFormFields( $form, $module ); $form->setSubmitCallback( [ $form, 'onSubmit' ] ); if ( $form->show( $panel ) ) { $form->onSuccess(); } } private function addHeading( Message $message ): void { $this->getOutput()->addHTML( Html::element( 'h2', [], $message->text() ) ); } private function shouldShowGenericButtons(): bool { return !$this->requestedModule instanceof IModule || !$this->isGenericAction(); } private function isModuleRequested( ?IModule $module ): bool { return ( $this->requestedModule instanceof IModule && $module instanceof IModule && $this->requestedModule->getName() === $module->getName() ); } private function isModuleEnabled( IModule $module ): bool { return (bool)$this->authUser->getKeysForModule( $module->getName() ); } /** * Verifies if the module is available to be enabled * * @param IModule $module * @return bool */ private function isModuleAvailable( IModule $module ): bool { $form = $module->getManageForm( static::ACTION_ENABLE, $this->authUser, $this->userRepo, $this->getContext() ); if ( $form === '' ) { return false; } return true; } /** * Verifies if the given form instance fulfills the required conditions * * @param mixed $form * @return bool */ private function isValidFormType( $form ): bool { if ( !( $form instanceof HTMLForm ) ) { return false; } $implements = class_implements( $form ); if ( !isset( $implements[IManageForm::class] ) ) { return false; } return true; } private function ensureRequiredFormFields( IManageForm $form, IModule $module ): void { if ( !$form->hasField( 'module' ) ) { $form->addHiddenField( 'module', $module->getName() ); } if ( !$form->hasField( 'action' ) ) { $form->addHiddenField( 'action', $this->action ); } } /** * When performing an action on a module (like enable/disable), * page should contain only the form for that action. */ private function clearPage(): void { if ( $this->isGenericAction() ) { $displayName = $this->requestedModule->getDisplayName(); $pageTitleMessage = $this->isModuleEnabled( $this->requestedModule ) ? $this->msg( 'oathauth-disable-page-title', $displayName ) : $this->msg( 'oathauth-enable-page-title', $displayName ); $this->getOutput()->setPageTitleMsg( $pageTitleMessage ); } $this->getOutput()->clearHTML(); $this->getOutput()->addBacklinkSubtitle( $this->getOutput()->getTitle() ); } /** * The enable and disable actions are generic, and all modules must * implement them, while all other actions are module-specific. */ private function isGenericAction(): bool { return in_array( $this->action, [ static::ACTION_ENABLE, static::ACTION_DISABLE ] ); } private function hasAlternativeModules(): bool { foreach ( $this->moduleRegistry->getAllModules() as $module ) { if ( !$this->isModuleEnabled( $module ) && $this->isModuleAvailable( $module ) ) { return true; } } return false; } private function shouldShowDisableWarning(): bool { return $this->getRequest()->getBool( 'warn' ) && $this->requestedModule instanceof IModule && $this->authUser->isTwoFactorAuthEnabled(); } private function showDisableWarning( IModule $module ): void { $panel = new PanelLayout( [ 'padded' => true, 'framed' => true, 'expanded' => false ] ); $isSwitch = $this->isSwitch(); $currentDisplayName = $module->getDisplayName(); $newDisplayName = $this->requestedModule->getDisplayName(); $genericMessage = $isSwitch ? $this->msg( 'oathauth-switch-method-warning', $currentDisplayName, $newDisplayName ) : $this->msg( 'oathauth-disable-method-warning', $currentDisplayName ); $panel->appendContent( new HtmlSnippet( $genericMessage->parseAsBlock() ) ); $customMessage = $module->getDisableWarningMessage(); if ( $customMessage instanceof Message ) { $panel->appendContent( new HtmlSnippet( $customMessage->parseAsBlock() ) ); } $nextStepMessage = $isSwitch ? $this->msg( 'oathauth-switch-method-next-step', $currentDisplayName, $newDisplayName ) : $this->msg( 'oathauth-disable-method-next-step', $currentDisplayName ); $panel->appendContent( new HtmlSnippet( $nextStepMessage->parseAsBlock() ) ); $button = new ButtonWidget( [ 'label' => $this->msg( 'oathauth-disable-method-warning-button-label' )->plain(), 'href' => $this->getOutput()->getTitle()->getLocalURL( [ 'action' => $this->action, 'module' => $this->requestedModule->getName() ] ), 'flags' => [ 'primary', 'progressive' ] ] ); $panel->appendContent( $button ); $headerMessage = $isSwitch ? $this->msg( 'oathauth-switch-method-warning-header' ) : $this->msg( 'oathauth-disable-method-warning-header' ); $this->getOutput()->setPageTitleMsg( $headerMessage ); $this->getOutput()->addHTML( $panel->toString() ); } private function isSwitch(): bool { return $this->requestedModule instanceof IModule && $this->action === static::ACTION_ENABLE && $this->authUser->isTwoFactorAuthEnabled(); } }