Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 // TODO: Have specific user documentation page for this feature
48 $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
49
50 $out->addHTML(
51 Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
52 '<tr>' .
53 Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
54 Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
55 '</tr>'
56 );
57
58 $config = $this->getConfig();
59 $policies = $config->get( 'PasswordPolicy' );
60
61 $groupPermissions = $config->get( 'GroupPermissions' );
62 $revokePermissions = $config->get( 'RevokePermissions' );
63 $addGroups = $config->get( 'AddGroups' );
64 $removeGroups = $config->get( 'RemoveGroups' );
65 $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
66 $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
67 $allGroups = array_unique( array_merge(
68 array_keys( $groupPermissions ),
69 array_keys( $revokePermissions ),
70 array_keys( $addGroups ),
71 array_keys( $removeGroups ),
72 array_keys( $groupsAddToSelf ),
73 array_keys( $groupsRemoveFromSelf )
74 ) );
75 asort( $allGroups );
76
77 $linkRenderer = $this->getLinkRenderer();
78
79 foreach ( $allGroups as $group ) {
80 if ( $group == '*' ) {
81 continue;
82 }
83
84 $groupnameLocalized = UserGroupMembership::getGroupName( $group );
85
86 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
87 ?: Title::newFromText( MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $group );
88
89 $grouppage = $linkRenderer->makeLink(
90 $grouppageLocalizedTitle,
91 $groupnameLocalized
92 );
93
94 if ( $group === 'user' ) {
95 // Link to Special:listusers for implicit group 'user'
96 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
97 SpecialPage::getTitleFor( 'Listusers' ),
98 $this->msg( 'listgrouprights-members' )->text()
99 );
100 } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
101 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
102 SpecialPage::getTitleFor( 'Listusers' ),
103 $this->msg( 'listgrouprights-members' )->text(),
104 [],
105 [ 'group' => $group ]
106 );
107 } else {
108 // No link to Special:listusers for other implicit groups as they are unlistable
109 $grouplink = '';
110 }
111
112 $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
113 <td>$grouppage$grouplink</td>
114 <td>" . $this->formatPolicies( $policies, $group ) . '</td>
115 '
116 ) );
117
118 }
119
120 $out->addHTML( Xml::closeElement( 'table' ) );
121 }
122
123 /**
124 * Create a HTML list of password policies for $group
125 *
126 * @param array $policies Original $wgPasswordPolicy array
127 * @param array $group Group to format password policies for
128 *
129 * @return string HTML list of all applied password policies
130 */
131 private function formatPolicies( $policies, $group ) {
132 $groupPolicies = UserPasswordPolicy::getPoliciesForGroups(
133 $policies['policies'],
134 [ $group ],
135 $policies['policies']['default']
136 );
137
138 $ret = [];
139 foreach ( $groupPolicies as $gp => $settings ) {
140 if ( !is_array( $settings ) ) {
141 $settings = [ 'value' => $settings ];
142 }
143 $val = $settings['value'];
144 $flags = array_diff_key( $settings, [ 'value' => true ] );
145 if ( !$val ) {
146 // Policy isn't enabled, so no need to display it
147 continue;
148 }
149 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) )->numParams( $val );
150 $flagMsgs = [];
151 foreach ( array_filter( $flags ) as $flag => $value ) {
152 $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
153 $flagMsg->params( $value );
154 $flagMsgs[] = $flagMsg;
155 }
156 if ( $flagMsgs ) {
157 $ret[] = $this->msg(
158 'passwordpolicies-policy-displaywithflags',
159 $msg,
160 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
161 $this->getLanguage()->commaList( $flagMsgs )
162 )->parse();
163 } else {
164 $ret[] = $this->msg(
165 'passwordpolicies-policy-display',
166 $msg,
167 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
168 )->parse();
169 }
170 }
171 if ( $ret === [] ) {
172 return '';
173 } else {
174 return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
175 }
176 }
177
178 protected function getGroupName() {
179 return 'users';
180 }
181 }