Debian Codegraph @

includes/specials/SpecialPasswordPolicies.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Specials;
22
23 use MediaWiki\Html\Html;
24 use MediaWiki\MainConfigNames;
25 use MediaWiki\Parser\Sanitizer;
26 use MediaWiki\Password\UserPasswordPolicy;
27 use MediaWiki\SpecialPage\SpecialPage;
28 use MediaWiki\Title\Title;
29 use MediaWiki\User\UserGroupManager;
30 use MediaWiki\User\UserGroupMembership;
31 use MediaWiki\Xml\Xml;
32
33 /**
34 * This special page lists the defined password policies for user groups.
35 *
36 * See also @ref $wgPasswordPolicy.
37 *
38 * @ingroup SpecialPage
39 * @since 1.32
40 */
41 class SpecialPasswordPolicies extends SpecialPage {
42
43 private UserGroupManager $userGroupManager;
44
45 /**
46 * @param UserGroupManager $userGroupManager
47 */
48 public function __construct( UserGroupManager $userGroupManager ) {
49 parent::__construct( 'PasswordPolicies' );
50 $this->userGroupManager = $userGroupManager;
51 }
52
53 /**
54 * Show the special page
55 * @param string|null $par
56 */
57 public function execute( $par ) {
58 $this->setHeaders();
59 $this->outputHeader();
60
61 $out = $this->getOutput();
62 $out->addModuleStyles( 'mediawiki.special' );
63
64 // TODO: Have specific user documentation page for this feature
65 $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
66
67 $out->addHTML(
68 Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
69 '<tr>' .
70 Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
71 Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
72 '</tr>'
73 );
74
75 $config = $this->getConfig();
76 $policies = $config->get( MainConfigNames::PasswordPolicy );
77
78 $implicitGroups = $this->userGroupManager->listAllImplicitGroups();
79 $allGroups = array_merge(
80 $this->userGroupManager->listAllGroups(),
81 $implicitGroups
82 );
83 asort( $allGroups );
84
85 $linkRenderer = $this->getLinkRenderer();
86 $lang = $this->getLanguage();
87
88 foreach ( $allGroups as $group ) {
89 if ( $group == '*' ) {
90 continue;
91 }
92
93 $groupnameLocalized = $lang->getGroupName( $group );
94
95 $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
96 ?: Title::makeTitle( NS_PROJECT, $group );
97
98 $grouppage = $linkRenderer->makeLink(
99 $grouppageLocalizedTitle,
100 $groupnameLocalized
101 );
102
103 if ( $group === 'user' ) {
104 // Link to Special:listusers for implicit group 'user'
105 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
106 SpecialPage::getTitleFor( 'Listusers' ),
107 $this->msg( 'listgrouprights-members' )->text()
108 );
109 } elseif ( !in_array( $group, $implicitGroups ) ) {
110 $grouplink = '<br />' . $linkRenderer->makeKnownLink(
111 SpecialPage::getTitleFor( 'Listusers' ),
112 $this->msg( 'listgrouprights-members' )->text(),
113 [],
114 [ 'group' => $group ]
115 );
116 } else {
117 // No link to Special:listusers for other implicit groups as they are unlistable
118 $grouplink = '';
119 }
120
121 $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
122 <td>$grouppage$grouplink</td>
123 <td>" . $this->formatPolicies( $policies, $group ) . '</td>
124 '
125 ) );
126
127 }
128
129 $out->addHTML( Xml::closeElement( 'table' ) );
130 }
131
132 /**
133 * Create a HTML list of password policies for $group
134 *
135 * @param array $policies Original $wgPasswordPolicy array
136 * @param string $group Group to format password policies for
137 *
138 * @return string HTML list of all applied password policies
139 */
140 private function formatPolicies( $policies, $group ) {
141 $groupPolicies = UserPasswordPolicy::getPoliciesForGroups(
142 $policies['policies'],
143 [ $group ],
144 $policies['policies']['default']
145 );
146
147 $ret = [];
148 foreach ( $groupPolicies as $gp => $settings ) {
149 if ( !is_array( $settings ) ) {
150 $settings = [ 'value' => $settings ];
151 }
152 $val = $settings['value'];
153 $flags = array_diff_key( $settings, [ 'value' => true ] );
154 if ( !$val ) {
155 // Policy isn't enabled, so no need to display it
156 continue;
157 }
158
159 $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) );
160
161 if ( is_numeric( $val ) ) {
162 $msg->numParams( $val );
163 }
164
165 $flagMsgs = [];
166 foreach ( array_filter( $flags ) as $flag => $value ) {
167 $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
168 $flagMsg->params( $value );
169 $flagMsgs[] = $flagMsg;
170 }
171 if ( $flagMsgs ) {
172 $ret[] = $this->msg(
173 'passwordpolicies-policy-displaywithflags',
174 $msg,
175 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
176 $this->getLanguage()->commaList( $flagMsgs )
177 )->parse();
178 } else {
179 $ret[] = $this->msg(
180 'passwordpolicies-policy-display',
181 $msg,
182 '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
183 )->parse();
184 }
185 }
186 if ( $ret === [] ) {
187 return '';
188 } else {
189 return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
190 }
191 }
192
193 protected function getGroupName() {
194 return 'users';
195 }
196 }
197
198 /**
199 * Retain the old class name for backwards compatibility.
200 * @deprecated since 1.41
201 */
202 class_alias( SpecialPasswordPolicies::class, 'SpecialPasswordPolicies' );