MediaWiki master
SpecialPasswordPolicies.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
32
42
43 private UserGroupManager $userGroupManager;
44
45 public function __construct( UserGroupManager $userGroupManager ) {
46 parent::__construct( 'PasswordPolicies' );
47 $this->userGroupManager = $userGroupManager;
48 }
49
54 public function execute( $par ) {
55 $this->setHeaders();
56 $this->outputHeader();
57
58 $out = $this->getOutput();
59 $out->addModuleStyles( 'mediawiki.special' );
60
61 // TODO: Have specific user documentation page for this feature
62 $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
63
64 $out->addHTML(
65 Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
66 '<tr>' .
67 Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
68 Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
69 '</tr>'
70 );
71
72 $config = $this->getConfig();
73 $policies = $config->get( MainConfigNames::PasswordPolicy );
74
75 $implicitGroups = $this->userGroupManager->listAllImplicitGroups();
76 $allGroups = array_merge(
77 $this->userGroupManager->listAllGroups(),
78 $implicitGroups
79 );
80 asort( $allGroups );
81
82 $linkRenderer = $this->getLinkRenderer();
83 $lang = $this->getLanguage();
84
85 foreach ( $allGroups as $group ) {
86 if ( $group == '*' ) {
87 continue;
88 }
89
90 $groupnameLocalized = $lang->getGroupName( $group );
91
92 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
93 ?: Title::makeTitle( NS_PROJECT, $group );
94
95 $grouppage = $linkRenderer->makeLink(
96 $grouppageLocalizedTitle,
97 $groupnameLocalized
98 );
99
100 if ( $group === 'user' ) {
101 // Link to Special:listusers for implicit group 'user'
102 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
103 SpecialPage::getTitleFor( 'Listusers' ),
104 $this->msg( 'listgrouprights-members' )->text()
105 );
106 } elseif ( !in_array( $group, $implicitGroups ) ) {
107 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
108 SpecialPage::getTitleFor( 'Listusers' ),
109 $this->msg( 'listgrouprights-members' )->text(),
110 [],
111 [ 'group' => $group ]
112 );
113 } else {
114 // No link to Special:listusers for other implicit groups as they are unlistable
115 $grouplink = '';
116 }
117
118 $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
119 <td>$grouppage$grouplink</td>
120 <td>" . $this->formatPolicies( $policies, $group ) . '</td>
121 '
122 ) );
123
124 }
125
126 $out->addHTML( Xml::closeElement( 'table' ) );
127 }
128
137 private function formatPolicies( $policies, $group ) {
138 $groupPolicies = UserPasswordPolicy::getPoliciesForGroups(
139 $policies['policies'],
140 [ $group ],
141 $policies['policies']['default']
142 );
143
144 $ret = [];
145 foreach ( $groupPolicies as $gp => $settings ) {
146 if ( !is_array( $settings ) ) {
147 $settings = [ 'value' => $settings ];
148 }
149 $val = $settings['value'];
150 $flags = array_diff_key( $settings, [ 'value' => true ] );
151 if ( !$val ) {
152 // Policy isn't enabled, so no need to display it
153 continue;
154 }
155
156 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) );
157
158 if ( is_numeric( $val ) ) {
159 $msg->numParams( $val );
160 }
161
162 $flagMsgs = [];
163 foreach ( array_filter( $flags ) as $flag => $value ) {
164 $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
165 $flagMsg->params( $value );
166 $flagMsgs[] = $flagMsg;
167 }
168 if ( $flagMsgs ) {
169 $ret[] = $this->msg(
170 'passwordpolicies-policy-displaywithflags',
171 $msg,
172 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
173 $this->getLanguage()->commaList( $flagMsgs )
174 )->parse();
175 } else {
176 $ret[] = $this->msg(
177 'passwordpolicies-policy-display',
178 $msg,
179 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
180 )->parse();
181 }
182 }
183 if ( $ret === [] ) {
184 return '';
185 } else {
186 return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
187 }
188 }
189
190 protected function getGroupName() {
191 return 'users';
192 }
193}
194
199class_alias( SpecialPasswordPolicies::class, 'SpecialPasswordPolicies' );
const NS_PROJECT
Definition Defines.php:69
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition Html.php:240
static closeElement( $element)
Returns "</$element>".
Definition Html.php:304
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition Html.php:216
A class containing constants representing the names of configuration variables.
const PasswordPolicy
Name constant for the PasswordPolicy setting, for use with Config::get()
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
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(UserGroupManager $userGroupManager)
Represents a title within MediaWiki.
Definition Title.php:78
Manage user group memberships.
Represents the membership of one user in one user group.
Module of static functions for generating XML.
Definition Xml.php:37