SpecialListGroupRights.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * Implements Special:Listgrouprights
  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 all defined user groups and the associated rights.
  26. * See also @ref $wgGroupPermissions.
  27. *
  28. * @ingroup SpecialPage
  29. * @author Petr Kadlec <mormegil@centrum.cz>
  30. */
  31. class SpecialListGroupRights extends SpecialPage {
  32. public function __construct() {
  33. parent::__construct( 'Listgrouprights' );
  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. $this->addHelpLink( 'Help:User_rights_and_groups' );
  45. $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
  46. $out->addHTML(
  47. Xml::openElement( 'table', [ 'class' => 'wikitable mw-listgrouprights-table' ] ) .
  48. '<tr>' .
  49. Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
  50. Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
  51. '</tr>'
  52. );
  53. $config = $this->getConfig();
  54. $groupPermissions = $config->get( 'GroupPermissions' );
  55. $revokePermissions = $config->get( 'RevokePermissions' );
  56. $addGroups = $config->get( 'AddGroups' );
  57. $removeGroups = $config->get( 'RemoveGroups' );
  58. $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
  59. $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
  60. $allGroups = array_unique( array_merge(
  61. array_keys( $groupPermissions ),
  62. array_keys( $revokePermissions ),
  63. array_keys( $addGroups ),
  64. array_keys( $removeGroups ),
  65. array_keys( $groupsAddToSelf ),
  66. array_keys( $groupsRemoveFromSelf )
  67. ) );
  68. asort( $allGroups );
  69. $linkRenderer = $this->getLinkRenderer();
  70. foreach ( $allGroups as $group ) {
  71. $permissions = $groupPermissions[$group] ?? [];
  72. $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
  73. ? 'all'
  74. : $group;
  75. $groupnameLocalized = UserGroupMembership::getGroupName( $groupname );
  76. $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $groupname )
  77. ?: Title::newFromText( MediaWikiServices::getInstance()->getNamespaceInfo()->
  78. getCanonicalName( NS_PROJECT ) . ':' . $groupname );
  79. if ( $group == '*' || !$grouppageLocalizedTitle ) {
  80. // Do not make a link for the generic * group or group with invalid group page
  81. $grouppage = htmlspecialchars( $groupnameLocalized );
  82. } else {
  83. $grouppage = $linkRenderer->makeLink(
  84. $grouppageLocalizedTitle,
  85. $groupnameLocalized
  86. );
  87. }
  88. if ( $group === 'user' ) {
  89. // Link to Special:listusers for implicit group 'user'
  90. $grouplink = '<br />' . $linkRenderer->makeKnownLink(
  91. SpecialPage::getTitleFor( 'Listusers' ),
  92. $this->msg( 'listgrouprights-members' )->text()
  93. );
  94. } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
  95. $grouplink = '<br />' . $linkRenderer->makeKnownLink(
  96. SpecialPage::getTitleFor( 'Listusers' ),
  97. $this->msg( 'listgrouprights-members' )->text(),
  98. [],
  99. [ 'group' => $group ]
  100. );
  101. } else {
  102. // No link to Special:listusers for other implicit groups as they are unlistable
  103. $grouplink = '';
  104. }
  105. $revoke = $revokePermissions[$group] ?? [];
  106. $addgroups = $addGroups[$group] ?? [];
  107. $removegroups = $removeGroups[$group] ?? [];
  108. $addgroupsSelf = $groupsAddToSelf[$group] ?? [];
  109. $removegroupsSelf = $groupsRemoveFromSelf[$group] ?? [];
  110. $id = $group == '*' ? false : Sanitizer::escapeIdForAttribute( $group );
  111. $out->addHTML( Html::rawElement( 'tr', [ 'id' => $id ], "
  112. <td>$grouppage$grouplink</td>
  113. <td>" .
  114. $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
  115. $addgroupsSelf, $removegroupsSelf ) .
  116. '</td>
  117. '
  118. ) );
  119. }
  120. $out->addHTML( Xml::closeElement( 'table' ) );
  121. $this->outputNamespaceProtectionInfo();
  122. }
  123. private function outputNamespaceProtectionInfo() {
  124. $out = $this->getOutput();
  125. $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
  126. if ( count( $namespaceProtection ) == 0 ) {
  127. return;
  128. }
  129. $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->text();
  130. $out->addHTML(
  131. Html::rawElement( 'h2', [], Html::element( 'span', [
  132. 'class' => 'mw-headline',
  133. 'id' => substr( Parser::guessSectionNameFromStrippedText( $header ), 1 )
  134. ], $header ) ) .
  135. Xml::openElement( 'table', [ 'class' => 'wikitable' ] ) .
  136. Html::element(
  137. 'th',
  138. [],
  139. $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
  140. ) .
  141. Html::element(
  142. 'th',
  143. [],
  144. $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
  145. )
  146. );
  147. $linkRenderer = $this->getLinkRenderer();
  148. ksort( $namespaceProtection );
  149. $validNamespaces =
  150. MediaWikiServices::getInstance()->getNamespaceInfo()->getValidNamespaces();
  151. $contLang = MediaWikiServices::getInstance()->getContentLanguage();
  152. foreach ( $namespaceProtection as $namespace => $rights ) {
  153. if ( !in_array( $namespace, $validNamespaces ) ) {
  154. continue;
  155. }
  156. if ( $namespace == NS_MAIN ) {
  157. $namespaceText = $this->msg( 'blanknamespace' )->text();
  158. } else {
  159. $namespaceText = $contLang->convertNamespace( $namespace );
  160. }
  161. $out->addHTML(
  162. Xml::openElement( 'tr' ) .
  163. Html::rawElement(
  164. 'td',
  165. [],
  166. $linkRenderer->makeLink(
  167. SpecialPage::getTitleFor( 'Allpages' ),
  168. $namespaceText,
  169. [],
  170. [ 'namespace' => $namespace ]
  171. )
  172. ) .
  173. Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
  174. );
  175. if ( !is_array( $rights ) ) {
  176. $rights = [ $rights ];
  177. }
  178. foreach ( $rights as $right ) {
  179. $out->addHTML(
  180. Html::rawElement( 'li', [], $this->msg(
  181. 'listgrouprights-right-display',
  182. User::getRightDescription( $right ),
  183. Html::element(
  184. 'span',
  185. [ 'class' => 'mw-listgrouprights-right-name' ],
  186. $right
  187. )
  188. )->parse() )
  189. );
  190. }
  191. $out->addHTML(
  192. Xml::closeElement( 'ul' ) .
  193. Xml::closeElement( 'td' ) .
  194. Xml::closeElement( 'tr' )
  195. );
  196. }
  197. $out->addHTML( Xml::closeElement( 'table' ) );
  198. }
  199. /**
  200. * Create a user-readable list of permissions from the given array.
  201. *
  202. * @param array $permissions Array of permission => bool (from $wgGroupPermissions items)
  203. * @param array $revoke Array of permission => bool (from $wgRevokePermissions items)
  204. * @param array $add Array of groups this group is allowed to add or true
  205. * @param array $remove Array of groups this group is allowed to remove or true
  206. * @param array $addSelf Array of groups this group is allowed to add to self or true
  207. * @param array $removeSelf Array of group this group is allowed to remove from self or true
  208. * @return string HTML list of all granted permissions
  209. */
  210. private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
  211. $r = [];
  212. foreach ( $permissions as $permission => $granted ) {
  213. // show as granted only if it isn't revoked to prevent duplicate display of permissions
  214. if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
  215. $r[] = $this->msg( 'listgrouprights-right-display',
  216. User::getRightDescription( $permission ),
  217. '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
  218. )->parse();
  219. }
  220. }
  221. foreach ( $revoke as $permission => $revoked ) {
  222. if ( $revoked ) {
  223. $r[] = $this->msg( 'listgrouprights-right-revoked',
  224. User::getRightDescription( $permission ),
  225. '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
  226. )->parse();
  227. }
  228. }
  229. sort( $r );
  230. $lang = $this->getLanguage();
  231. $allGroups = User::getAllGroups();
  232. $changeGroups = [
  233. 'addgroup' => $add,
  234. 'removegroup' => $remove,
  235. 'addgroup-self' => $addSelf,
  236. 'removegroup-self' => $removeSelf
  237. ];
  238. foreach ( $changeGroups as $messageKey => $changeGroup ) {
  239. // @phan-suppress-next-line PhanTypeComparisonFromArray
  240. if ( $changeGroup === true ) {
  241. // For grep: listgrouprights-addgroup-all, listgrouprights-removegroup-all,
  242. // listgrouprights-addgroup-self-all, listgrouprights-removegroup-self-all
  243. $r[] = $this->msg( 'listgrouprights-' . $messageKey . '-all' )->escaped();
  244. } elseif ( is_array( $changeGroup ) ) {
  245. $changeGroup = array_intersect( array_values( array_unique( $changeGroup ) ), $allGroups );
  246. if ( count( $changeGroup ) ) {
  247. $groupLinks = [];
  248. foreach ( $changeGroup as $group ) {
  249. $groupLinks[] = UserGroupMembership::getLink( $group, $this->getContext(), 'wiki' );
  250. }
  251. // For grep: listgrouprights-addgroup, listgrouprights-removegroup,
  252. // listgrouprights-addgroup-self, listgrouprights-removegroup-self
  253. $r[] = $this->msg( 'listgrouprights-' . $messageKey,
  254. $lang->listToText( $groupLinks ), count( $changeGroup ) )->parse();
  255. }
  256. }
  257. }
  258. if ( empty( $r ) ) {
  259. return '';
  260. } else {
  261. return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
  262. }
  263. }
  264. protected function getGroupName() {
  265. return 'users';
  266. }
  267. }