Add Special:PasswordPolicies
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordPolicies.php
1 <?php
2 /**
3 * Implements Special:PasswordPolicies
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * This special page lists the defined password policies for user groups.
26 * See also @ref $wgPasswordPolicy.
27 *
28 * @ingroup SpecialPage
29 * @since 1.32
30 */
31 class SpecialPasswordPolicies extends SpecialPage {
32 public function __construct() {
33 parent::__construct( 'PasswordPolicies' );
34 }
35
36 /**
37 * Show the special page
38 * @param string|null $par
39 */
40 public function execute( $par ) {
41 $this->setHeaders();
42 $this->outputHeader();
43
44 $out = $this->getOutput();
45 $out->addModuleStyles( 'mediawiki.special' );
46
47 $this->addHelpLink( 'Help:Password policies' );
48
49 $out->addHTML(
50 Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
51 '<tr>' .
52 Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
53 Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
54 '</tr>'
55 );
56
57 $config = $this->getConfig();
58 $policies = $config->get( 'PasswordPolicy' );
59
60 $groupPermissions = $config->get( 'GroupPermissions' );
61 $revokePermissions = $config->get( 'RevokePermissions' );
62 $addGroups = $config->get( 'AddGroups' );
63 $removeGroups = $config->get( 'RemoveGroups' );
64 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
65 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
66 $allGroups = array_unique( array_merge(
67 array_keys( $groupPermissions ),
68 array_keys( $revokePermissions ),
69 array_keys( $addGroups ),
70 array_keys( $removeGroups ),
71 array_keys( $groupsAddToSelf ),
72 array_keys( $groupsRemoveFromSelf )
73 ) );
74 asort( $allGroups );
75
76 $linkRenderer = $this->getLinkRenderer();
77
78 foreach ( $allGroups as $group ) {
79 if ( $group == '*' ) {
80 continue;
81 }
82
83 $groupnameLocalized = UserGroupMembership::getGroupName( $group );
84
85 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
86 ?: Title::newFromText( MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $group );
87
88 $grouppage = $linkRenderer->makeLink(
89 $grouppageLocalizedTitle,
90 $groupnameLocalized
91 );
92
93 if ( $group === 'user' ) {
94 // Link to Special:listusers for implicit group 'user'
95 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
96 SpecialPage::getTitleFor( 'Listusers' ),
97 $this->msg( 'listgrouprights-members' )->text()
98 );
99 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
100 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
101 SpecialPage::getTitleFor( 'Listusers' ),
102 $this->msg( 'listgrouprights-members' )->text(),
103 [],
104 [ 'group' => $group ]
105 );
106 } else {
107 // No link to Special:listusers for other implicit groups as they are unlistable
108 $grouplink = '';
109 }
110
111 $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
112 <td>$grouppage$grouplink</td>
113 <td>" . $this->formatPolicies( $policies, $group ) . '</td>
114 '
115 ) );
116
117 }
118
119 $out->addHTML( Xml::closeElement( 'table' ) );
120 }
121
122 /**
123 * Create a HTML list of password policies for $group
124 *
125 * @param array $policies Original $wgPasswordPolicy array
126 * @param array $group Group to format password policies for
127 *
128 * @return string HTML list of all applied password policies
129 */
130 private function formatPolicies( $policies, $group ) {
131 $groupPolicies = UserPasswordPolicy::getPoliciesForGroups(
132 $policies['policies'],
133 [ $group ],
134 $policies['policies']['default']
135 );
136
137 $ret = [];
138 foreach ( $groupPolicies as $gp => $val ) {
139 if ( $val === false ) {
140 // Policy isn't enabled, so no need to dislpay it
141 continue;
142 } elseif ( $val === true ) {
143 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) );
144 } else {
145 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) )->numParams( $val );
146 }
147 $ret[] = $this->msg(
148 'passwordpolicies-policy-display',
149 $msg,
150 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
151 )->parse();
152 }
153 if ( !count( $ret ) ) {
154 return '';
155 } else {
156 return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
157 }
158 }
159
160 protected function getGroupName() {
161 return 'users';
162 }
163 }