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