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