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