MediaWiki 1.44.0
SpecialPasswordPolicies.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
31
41
42 private UserGroupManager $userGroupManager;
43
44 public function __construct( UserGroupManager $userGroupManager ) {
45 parent::__construct( 'PasswordPolicies' );
46 $this->userGroupManager = $userGroupManager;
47 }
48
53 public function execute( $par ) {
54 $this->setHeaders();
55 $this->outputHeader();
56
57 $out = $this->getOutput();
58 $out->addModuleStyles( 'mediawiki.special' );
59
60 // TODO: Have specific user documentation page for this feature
61 $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
62
63 $out->addHTML(
64 Html::openElement( 'table', [ 'class' => [ 'wikitable', 'mw-passwordpolicies-table' ] ] ) .
65 '<tr>' .
66 Html::element( 'th', [], $this->msg( 'passwordpolicies-group' )->text() ) .
67 Html::element( 'th', [], $this->msg( 'passwordpolicies-policies' )->text() ) .
68 '</tr>'
69 );
70
71 $config = $this->getConfig();
72 $policies = $config->get( MainConfigNames::PasswordPolicy );
73
74 $implicitGroups = $this->userGroupManager->listAllImplicitGroups();
75 $allGroups = array_merge(
76 $this->userGroupManager->listAllGroups(),
77 $implicitGroups
78 );
79 asort( $allGroups );
80
81 $linkRenderer = $this->getLinkRenderer();
82 $lang = $this->getLanguage();
83
84 foreach ( $allGroups as $group ) {
85 if ( $group == '*' ) {
86 continue;
87 }
88
89 $groupnameLocalized = $lang->getGroupName( $group );
90
91 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
92 ?: Title::makeTitle( NS_PROJECT, $group );
93
94 $grouppage = $linkRenderer->makeLink(
95 $grouppageLocalizedTitle,
96 $groupnameLocalized
97 );
98
99 if ( $group === 'user' ) {
100 // Link to Special:listusers for implicit group 'user'
101 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
102 SpecialPage::getTitleFor( 'Listusers' ),
103 $this->msg( 'listgrouprights-members' )->text()
104 );
105 } elseif ( !in_array( $group, $implicitGroups ) ) {
106 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
107 SpecialPage::getTitleFor( 'Listusers' ),
108 $this->msg( 'listgrouprights-members' )->text(),
109 [],
110 [ 'group' => $group ]
111 );
112 } else {
113 // No link to Special:listusers for other implicit groups as they are unlistable
114 $grouplink = '';
115 }
116
117 $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
118 <td>$grouppage$grouplink</td>
119 <td>" . $this->formatPolicies( $policies, $group ) . '</td>
120 '
121 ) );
122
123 }
124
125 $out->addHTML( Html::closeElement( 'table' ) );
126 }
127
136 private function formatPolicies( $policies, $group ) {
137 $groupPolicies = UserPasswordPolicy::getPoliciesForGroups(
138 $policies['policies'],
139 [ $group ],
140 $policies['policies']['default']
141 );
142
143 $ret = [];
144 foreach ( $groupPolicies as $gp => $settings ) {
145 if ( !is_array( $settings ) ) {
146 $settings = [ 'value' => $settings ];
147 }
148 $val = $settings['value'];
149 $flags = array_diff_key( $settings, [ 'value' => true ] );
150 if ( !$val ) {
151 // Policy isn't enabled, so no need to display it
152 continue;
153 }
154
155 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) );
156
157 if ( is_numeric( $val ) ) {
158 $msg->numParams( $val );
159 }
160
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:69
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
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.
element(SerializerNode $parent, SerializerNode $node, $contents)