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