MediaWiki master
SpecialPasswordPolicies.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
19
29
30 public function __construct(
31 private readonly UserGroupManager $userGroupManager,
32 ) {
33 parent::__construct( 'PasswordPolicies' );
34 }
35
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
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
176 protected function getGroupName() {
177 return 'users';
178 }
179}
180
185class_alias( SpecialPasswordPolicies::class, 'SpecialPasswordPolicies' );
const NS_PROJECT
Definition Defines.php:55
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
A class containing constants representing the names of configuration variables.
const PasswordPolicy
Name constant for the PasswordPolicy setting, for use with Config::get()
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
static listParam(array $list, $type=ListType::AND)
Definition Message.php:1355
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:34
Check if a user's password complies with any password policies that apply to that user,...
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getConfig()
Shortcut to get main config object.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
This special page lists the defined password policies for user groups.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(private readonly UserGroupManager $userGroupManager,)
Represents a title within MediaWiki.
Definition Title.php:69
Manage user group memberships.
Represents the membership of one user in one user group.
element(SerializerNode $parent, SerializerNode $node, $contents)
ListType
The constants used to specify list types.
Definition ListType.php:9