MediaWiki  master
SpecialPasswordPolicies.php
Go to the documentation of this file.
1 <?php
32 
33  public function __construct() {
34  parent::__construct( 'PasswordPolicies' );
35  }
36 
41  public function execute( $par ) {
42  $this->setHeaders();
43  $this->outputHeader();
44 
45  $out = $this->getOutput();
46  $out->addModuleStyles( 'mediawiki.special' );
47 
48  // TODO: Have specific user documentation page for this feature
49  $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
50 
51  $out->addHTML(
52  Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
53  '<tr>' .
54  Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
55  Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
56  '</tr>'
57  );
58 
59  $config = $this->getConfig();
60  $policies = $config->get( 'PasswordPolicy' );
61 
62  $groupPermissions = $config->get( 'GroupPermissions' );
63  $revokePermissions = $config->get( 'RevokePermissions' );
64  $addGroups = $config->get( 'AddGroups' );
65  $removeGroups = $config->get( 'RemoveGroups' );
66  $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
67  $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
68  $allGroups = array_unique( array_merge(
69  array_keys( $groupPermissions ),
70  array_keys( $revokePermissions ),
71  array_keys( $addGroups ),
72  array_keys( $removeGroups ),
73  array_keys( $groupsAddToSelf ),
74  array_keys( $groupsRemoveFromSelf )
75  ) );
76  asort( $allGroups );
77 
78  $linkRenderer = $this->getLinkRenderer();
79 
80  foreach ( $allGroups as $group ) {
81  if ( $group == '*' ) {
82  continue;
83  }
84 
85  $groupnameLocalized = UserGroupMembership::getGroupName( $group );
86 
87  $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
88  ?: Title::makeTitle( NS_PROJECT, $group );
89 
90  $grouppage = $linkRenderer->makeLink(
91  $grouppageLocalizedTitle,
92  $groupnameLocalized
93  );
94 
95  if ( $group === 'user' ) {
96  // Link to Special:listusers for implicit group 'user'
97  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
98  SpecialPage::getTitleFor( 'Listusers' ),
99  $this->msg( 'listgrouprights-members' )->text()
100  );
101  } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
102  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
103  SpecialPage::getTitleFor( 'Listusers' ),
104  $this->msg( 'listgrouprights-members' )->text(),
105  [],
106  [ 'group' => $group ]
107  );
108  } else {
109  // No link to Special:listusers for other implicit groups as they are unlistable
110  $grouplink = '';
111  }
112 
113  $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
114  <td>$grouppage$grouplink</td>
115  <td>" . $this->formatPolicies( $policies, $group ) . '</td>
116  '
117  ) );
118 
119  }
120 
121  $out->addHTML( Xml::closeElement( 'table' ) );
122  }
123 
132  private function formatPolicies( $policies, $group ) {
134  $policies['policies'],
135  [ $group ],
136  $policies['policies']['default']
137  );
138 
139  $ret = [];
140  foreach ( $groupPolicies as $gp => $settings ) {
141  if ( !is_array( $settings ) ) {
142  $settings = [ 'value' => $settings ];
143  }
144  $val = $settings['value'];
145  $flags = array_diff_key( $settings, [ 'value' => true ] );
146  if ( !$val ) {
147  // Policy isn't enabled, so no need to display it
148  continue;
149  }
150  $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) )->numParams( $val );
151  $flagMsgs = [];
152  foreach ( array_filter( $flags ) as $flag => $value ) {
153  $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
154  $flagMsg->params( $value );
155  $flagMsgs[] = $flagMsg;
156  }
157  if ( $flagMsgs ) {
158  $ret[] = $this->msg(
159  'passwordpolicies-policy-displaywithflags',
160  $msg,
161  '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
162  $this->getLanguage()->commaList( $flagMsgs )
163  )->parse();
164  } else {
165  $ret[] = $this->msg(
166  'passwordpolicies-policy-display',
167  $msg,
168  '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
169  )->parse();
170  }
171  }
172  if ( $ret === [] ) {
173  return '';
174  } else {
175  return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
176  }
177  }
178 
179  protected function getGroupName() {
180  return 'users';
181  }
182 }
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:80
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:911
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:789
SpecialPasswordPolicies\formatPolicies
formatPolicies( $policies, $group)
Create a HTML list of password policies for $group.
Definition: SpecialPasswordPolicies.php:132
Sanitizer\escapeIdForAttribute
static escapeIdForAttribute( $id, $mode=self::ID_PRIMARY)
Given a section name or other user-generated or otherwise unsafe string, escapes it to be a valid HTM...
Definition: Sanitizer.php:811
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:187
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:107
SpecialPasswordPolicies\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPasswordPolicies.php:179
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:829
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:212
UserPasswordPolicy\getPoliciesForGroups
static getPoliciesForGroups(array $policies, array $userGroups, array $defaultPolicy)
Utility function to get the effective policy from a list of policies, based on a list of groups.
Definition: UserPasswordPolicy.php:204
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:110
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:947
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:877
SpecialPasswordPolicies\__construct
__construct()
Definition: SpecialPasswordPolicies.php:33
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:68
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:617
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:680
SpecialPasswordPolicies
This special page lists the defined password policies for user groups.
Definition: SpecialPasswordPolicies.php:31
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:43
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1027
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:119
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
SpecialPasswordPolicies\execute
execute( $par)
Show the special page.
Definition: SpecialPasswordPolicies.php:41
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:708