Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecoveryCodesRemoveTemporaryForm
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getHTML
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptors
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 onSuccess
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OATHAuth\HTMLForm;
4
5use MediaWiki\Context\IContextSource;
6use MediaWiki\Extension\OATHAuth\Module\RecoveryCodes;
7use MediaWiki\Extension\OATHAuth\OATHAuthModuleRegistry;
8use MediaWiki\Extension\OATHAuth\OATHUser;
9use MediaWiki\Extension\OATHAuth\OATHUserRepository;
10use MediaWiki\Logger\LoggerFactory;
11use MediaWiki\Status\Status;
12
13/**
14 * @property RecoveryCodes $module
15 */
16class RecoveryCodesRemoveTemporaryForm extends OATHAuthOOUIHTMLForm {
17
18    private int $temporaryCodesCount = 0;
19    private int $permanentCodesCount = 0;
20
21    public function __construct(
22        OATHUser $oathUser,
23        OATHUserRepository $oathRepo,
24        RecoveryCodes $module,
25        IContextSource $context,
26        OATHAuthModuleRegistry $moduleRegistry
27    ) {
28        $key = $module->ensureExistence( $oathUser );
29
30        foreach ( $key->getRecoveryCodes() as $code ) {
31            if ( $code->isPermanent() ) {
32                $this->permanentCodesCount++;
33            } else {
34                $this->temporaryCodesCount++;
35            }
36        }
37
38        parent::__construct( $oathUser, $oathRepo, $module, $context, $moduleRegistry );
39    }
40
41    /** @inheritDoc */
42    public function getHTML( $submitResult ) {
43        $out = $this->getOutput();
44        $out->addModuleStyles( 'ext.oath.recovery.styles' );
45        $out->addModules( 'ext.oath.recovery' );
46        $out->setPageTitleMsg(
47            $this->msg( 'oathauth-recoverycodes-temporary-remove-header' )
48                ->numParams( $this->temporaryCodesCount )
49        );
50        return parent::getHTML( $submitResult );
51    }
52
53    protected function getDescriptors(): array {
54        if ( $this->oathUser->userHasNonSpecialEnabledKeys() ) {
55            $submitMsg = $this->msg( 'oathauth-recoverycodes-temporary-remove-label' )
56                ->numParams( $this->temporaryCodesCount );
57            $this->setSubmitTextMsg( $submitMsg );
58            $this->setSubmitDestructive();
59            $this->showCancel();
60            $this->setCancelTarget( $this->getTitle() );
61        } else {
62            $this->suppressDefaultSubmit();
63        }
64        return [
65            'warning' => [
66                'type' => 'info',
67                'default' => $this->msg( 'oathauth-recoverycodes-temporary-remove-text' )
68                    ->numParams( $this->temporaryCodesCount )
69                    ->parse(),
70                'raw' => true,
71            ] ];
72    }
73
74    /**
75     * Add content to output when the operation was successful
76     */
77    public function onSuccess(): void {
78        $output = $this->getOutput();
79        $output->addWikiMsg(
80            $this->msg( 'oathauth-recoverycodes-temporary-remove-success' )
81                ->numParams( $this->temporaryCodesCount, $this->permanentCodesCount )
82        );
83    }
84
85    public function onSubmit( array $formData ): Status|bool|array|string {
86        $key = $this->module->ensureExistence( $this->oathUser );
87        $key->removeTemporaryCodes();
88        $this->oathRepo->updateKey( $this->oathUser, $key );
89
90        LoggerFactory::getInstance( 'authentication' )->info(
91            "OATHAuth {user} invalidated temporary recovery codes from {clientip}", [
92                'user' => $this->getUser()->getName(),
93                'clientip' => $this->getRequest()->getIP(),
94            ]
95        );
96
97        return true;
98    }
99}