SpecialPasswordPolicies.php 5.5 KB

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