Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialPasswordPolicies
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 4
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
42
 formatPolicies
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
72
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Specials;
8
9use MediaWiki\Html\Html;
10use MediaWiki\MainConfigNames;
11use MediaWiki\Message\Message;
12use MediaWiki\Parser\Sanitizer;
13use MediaWiki\Password\UserPasswordPolicy;
14use MediaWiki\SpecialPage\SpecialPage;
15use MediaWiki\Title\Title;
16use MediaWiki\User\UserGroupManager;
17use MediaWiki\User\UserGroupMembership;
18use Wikimedia\Message\ListType;
19
20/**
21 * This special page lists the defined password policies for user groups.
22 *
23 * See also @ref $wgPasswordPolicy.
24 *
25 * @ingroup SpecialPage
26 * @since 1.32
27 */
28class SpecialPasswordPolicies extends SpecialPage {
29
30    public function __construct(
31        private readonly UserGroupManager $userGroupManager,
32    ) {
33        parent::__construct( 'PasswordPolicies' );
34    }
35
36    /**
37     * Show the special page
38     * @param string|null $par
39     */
40    public function execute( $par ) {
41        $this->setHeaders();
42        $this->outputHeader();
43
44        $out = $this->getOutput();
45        $out->addModuleStyles( 'mediawiki.special' );
46
47        // TODO: Have specific user documentation page for this feature
48        $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
49
50        $out->addHTML(
51            Html::openElement( 'table', [ 'class' => [ 'wikitable', 'mw-passwordpolicies-table' ] ] ) .
52                '<tr>' .
53            Html::element( 'th', [], $this->msg( 'passwordpolicies-group' )->text() ) .
54            Html::element( 'th', [], $this->msg( 'passwordpolicies-policies' )->text() ) .
55                '</tr>'
56        );
57
58        $config = $this->getConfig();
59        $policies = $config->get( MainConfigNames::PasswordPolicy );
60
61        $implicitGroups = $this->userGroupManager->listAllImplicitGroups();
62        $allGroups = array_merge(
63            $this->userGroupManager->listAllGroups(),
64            $implicitGroups
65        );
66        asort( $allGroups );
67
68        $linkRenderer = $this->getLinkRenderer();
69        $lang = $this->getLanguage();
70
71        foreach ( $allGroups as $group ) {
72            if ( $group == '*' ) {
73                continue;
74            }
75
76            $groupnameLocalized = $lang->getGroupName( $group );
77
78            $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
79                ?: Title::makeTitle( NS_PROJECT, $group );
80
81            $grouppage = $linkRenderer->makeLink(
82                $grouppageLocalizedTitle,
83                $groupnameLocalized
84            );
85
86            if ( $group === 'user' ) {
87                // Link to Special:listusers for implicit group 'user'
88                $grouplink = '<br />' . $linkRenderer->makeKnownLink(
89                    SpecialPage::getTitleFor( 'Listusers' ),
90                    $this->msg( 'listgrouprights-members' )->text()
91                );
92            } elseif ( !in_array( $group, $implicitGroups ) ) {
93                $grouplink = '<br />' . $linkRenderer->makeKnownLink(
94                    SpecialPage::getTitleFor( 'Listusers' ),
95                    $this->msg( 'listgrouprights-members' )->text(),
96                    [],
97                    [ 'group' => $group ]
98                );
99            } else {
100                // No link to Special:listusers for other implicit groups as they are unlistable
101                $grouplink = '';
102            }
103
104            $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
105                <td>$grouppage$grouplink</td>
106                <td>" . $this->formatPolicies( $policies, $group ) . '</td>
107                '
108            ) );
109
110        }
111
112        $out->addHTML( Html::closeElement( 'table' ) );
113    }
114
115    /**
116     * Create a HTML list of password policies for $group
117     *
118     * @param array $policies Original $wgPasswordPolicy array
119     * @param string $group Group to format password policies for
120     *
121     * @return string HTML list of all applied password policies
122     */
123    private function formatPolicies( $policies, $group ) {
124        $groupPolicies = UserPasswordPolicy::getPoliciesForGroups(
125            $policies['policies'],
126            [ $group ],
127            $policies['policies']['default']
128        );
129
130        $ret = [];
131        foreach ( $groupPolicies as $gp => $settings ) {
132            if ( !is_array( $settings ) ) {
133                $settings = [ 'value' => $settings ];
134            }
135            $val = $settings['value'];
136            $flags = array_diff_key( $settings, [ 'value' => true ] );
137            if ( !$val ) {
138                // Policy isn't enabled, so no need to display it
139                continue;
140            }
141
142            $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) );
143
144            if ( is_numeric( $val ) ) {
145                $msg->numParams( $val );
146            }
147
148            $flagMsgs = [];
149            foreach ( array_filter( $flags ) as $flag => $unused ) {
150                $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
151                $flagMsgs[] = $flagMsg;
152            }
153            if ( $flagMsgs ) {
154                $ret[] = $this->msg(
155                    'passwordpolicies-policy-displaywithflags',
156                    $msg,
157                    '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
158                    Message::listParam( $flagMsgs, ListType::COMMA )
159                )->parse();
160            } else {
161                $ret[] = $this->msg(
162                    'passwordpolicies-policy-display',
163                    $msg,
164                    '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
165                )->parse();
166            }
167        }
168        if ( $ret === [] ) {
169            return '';
170        } else {
171            return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
172        }
173    }
174
175    /** @inheritDoc */
176    protected function getGroupName() {
177        return 'users';
178    }
179}
180
181// @codeCoverageIgnoreStart
182/**
183 * Retain the old class name for backwards compatibility.
184 * @deprecated since 1.41
185 */
186class_alias( SpecialPasswordPolicies::class, 'SpecialPasswordPolicies' );
187// @codeCoverageIgnoreEnd