3 * Implements Special:PasswordPolicies
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
27 * This special page lists the defined password policies for user groups.
28 * See also @ref $wgPasswordPolicy.
30 * @ingroup SpecialPage
33 class SpecialPasswordPolicies
extends SpecialPage
{
34 public function __construct() {
35 parent
::__construct( 'PasswordPolicies' );
39 * Show the special page
40 * @param string|null $par
42 public function execute( $par ) {
44 $this->outputHeader();
46 $out = $this->getOutput();
47 $out->addModuleStyles( 'mediawiki.special' );
49 // TODO: Have specific user documentation page for this feature
50 $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
53 Xml
::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
55 Xml
::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
56 Xml
::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
60 $config = $this->getConfig();
61 $policies = $config->get( 'PasswordPolicy' );
63 $groupPermissions = $config->get( 'GroupPermissions' );
64 $revokePermissions = $config->get( 'RevokePermissions' );
65 $addGroups = $config->get( 'AddGroups' );
66 $removeGroups = $config->get( 'RemoveGroups' );
67 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
68 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
69 $allGroups = array_unique( array_merge(
70 array_keys( $groupPermissions ),
71 array_keys( $revokePermissions ),
72 array_keys( $addGroups ),
73 array_keys( $removeGroups ),
74 array_keys( $groupsAddToSelf ),
75 array_keys( $groupsRemoveFromSelf )
79 $linkRenderer = $this->getLinkRenderer();
81 foreach ( $allGroups as $group ) {
82 if ( $group == '*' ) {
86 $groupnameLocalized = UserGroupMembership
::getGroupName( $group );
88 $grouppageLocalizedTitle = UserGroupMembership
::getGroupPage( $group )
89 ?
: Title
::newFromText( MediaWikiServices
::getInstance()->getNamespaceInfo()->
90 getCanonicalName( NS_PROJECT
) . ':' . $group );
92 $grouppage = $linkRenderer->makeLink(
93 $grouppageLocalizedTitle,
97 if ( $group === 'user' ) {
98 // Link to Special:listusers for implicit group 'user'
99 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
100 SpecialPage
::getTitleFor( 'Listusers' ),
101 $this->msg( 'listgrouprights-members' )->text()
103 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
104 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
105 SpecialPage
::getTitleFor( 'Listusers' ),
106 $this->msg( 'listgrouprights-members' )->text(),
108 [ 'group' => $group ]
111 // No link to Special:listusers for other implicit groups as they are unlistable
115 $out->addHTML( Html
::rawElement( 'tr', [ 'id' => Sanitizer
::escapeIdForAttribute( $group ) ], "
116 <td>$grouppage$grouplink</td>
117 <td>" . $this->formatPolicies( $policies, $group ) . '</td>
123 $out->addHTML( Xml
::closeElement( 'table' ) );
127 * Create a HTML list of password policies for $group
129 * @param array $policies Original $wgPasswordPolicy array
130 * @param string $group Group to format password policies for
132 * @return string HTML list of all applied password policies
134 private function formatPolicies( $policies, $group ) {
135 $groupPolicies = UserPasswordPolicy
::getPoliciesForGroups(
136 $policies['policies'],
138 $policies['policies']['default']
142 foreach ( $groupPolicies as $gp => $settings ) {
143 if ( !is_array( $settings ) ) {
144 $settings = [ 'value' => $settings ];
146 $val = $settings['value'];
147 $flags = array_diff_key( $settings, [ 'value' => true ] );
149 // Policy isn't enabled, so no need to display it
152 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) )->numParams( $val );
154 foreach ( array_filter( $flags ) as $flag => $value ) {
155 $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
156 $flagMsg->params( $value );
157 $flagMsgs[] = $flagMsg;
161 'passwordpolicies-policy-displaywithflags',
163 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
164 $this->getLanguage()->commaList( $flagMsgs )
168 'passwordpolicies-policy-display',
170 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
177 return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
181 protected function getGroupName() {