MediaWiki  master
SpecialPasswordPolicies.php
Go to the documentation of this file.
1 <?php
32 
34  private $nsInfo;
35 
39  public function __construct( NamespaceInfo $nsInfo ) {
40  parent::__construct( 'PasswordPolicies' );
41 
42  $this->nsInfo = $nsInfo;
43  }
44 
49  public function execute( $par ) {
50  $this->setHeaders();
51  $this->outputHeader();
52 
53  $out = $this->getOutput();
54  $out->addModuleStyles( 'mediawiki.special' );
55 
56  // TODO: Have specific user documentation page for this feature
57  $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
58 
59  $out->addHTML(
60  Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
61  '<tr>' .
62  Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
63  Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
64  '</tr>'
65  );
66 
67  $config = $this->getConfig();
68  $policies = $config->get( 'PasswordPolicy' );
69 
70  $groupPermissions = $config->get( 'GroupPermissions' );
71  $revokePermissions = $config->get( 'RevokePermissions' );
72  $addGroups = $config->get( 'AddGroups' );
73  $removeGroups = $config->get( 'RemoveGroups' );
74  $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
75  $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
76  $allGroups = array_unique( array_merge(
77  array_keys( $groupPermissions ),
78  array_keys( $revokePermissions ),
79  array_keys( $addGroups ),
80  array_keys( $removeGroups ),
81  array_keys( $groupsAddToSelf ),
82  array_keys( $groupsRemoveFromSelf )
83  ) );
84  asort( $allGroups );
85 
86  $linkRenderer = $this->getLinkRenderer();
87 
88  foreach ( $allGroups as $group ) {
89  if ( $group == '*' ) {
90  continue;
91  }
92 
93  $groupnameLocalized = UserGroupMembership::getGroupName( $group );
94 
95  $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
97  $this->nsInfo->getCanonicalName( NS_PROJECT ) . ':' . $group
98  );
99 
100  $grouppage = $linkRenderer->makeLink(
101  $grouppageLocalizedTitle,
102  $groupnameLocalized
103  );
104 
105  if ( $group === 'user' ) {
106  // Link to Special:listusers for implicit group 'user'
107  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
108  SpecialPage::getTitleFor( 'Listusers' ),
109  $this->msg( 'listgrouprights-members' )->text()
110  );
111  } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
112  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
113  SpecialPage::getTitleFor( 'Listusers' ),
114  $this->msg( 'listgrouprights-members' )->text(),
115  [],
116  [ 'group' => $group ]
117  );
118  } else {
119  // No link to Special:listusers for other implicit groups as they are unlistable
120  $grouplink = '';
121  }
122 
123  $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
124  <td>$grouppage$grouplink</td>
125  <td>" . $this->formatPolicies( $policies, $group ) . '</td>
126  '
127  ) );
128 
129  }
130 
131  $out->addHTML( Xml::closeElement( 'table' ) );
132  }
133 
142  private function formatPolicies( $policies, $group ) {
144  $policies['policies'],
145  [ $group ],
146  $policies['policies']['default']
147  );
148 
149  $ret = [];
150  foreach ( $groupPolicies as $gp => $settings ) {
151  if ( !is_array( $settings ) ) {
152  $settings = [ 'value' => $settings ];
153  }
154  $val = $settings['value'];
155  $flags = array_diff_key( $settings, [ 'value' => true ] );
156  if ( !$val ) {
157  // Policy isn't enabled, so no need to display it
158  continue;
159  }
160  $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) )->numParams( $val );
161  $flagMsgs = [];
162  foreach ( array_filter( $flags ) as $flag => $value ) {
163  $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
164  $flagMsg->params( $value );
165  $flagMsgs[] = $flagMsg;
166  }
167  if ( $flagMsgs ) {
168  $ret[] = $this->msg(
169  'passwordpolicies-policy-displaywithflags',
170  $msg,
171  '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
172  $this->getLanguage()->commaList( $flagMsgs )
173  )->parse();
174  } else {
175  $ret[] = $this->msg(
176  'passwordpolicies-policy-display',
177  $msg,
178  '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
179  )->parse();
180  }
181  }
182  if ( $ret === [] ) {
183  return '';
184  } else {
185  return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
186  }
187  }
188 
189  protected function getGroupName() {
190  return 'users';
191  }
192 }
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:75
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:831
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:328
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:747
SpecialPasswordPolicies\formatPolicies
formatPolicies( $policies, $group)
Create a HTML list of password policies for $group.
Definition: SpecialPasswordPolicies.php:142
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:815
UserGroupMembership\getGroupName
static getGroupName( $group)
Gets the localized friendly name for a group, if it exists.
Definition: UserGroupMembership.php:186
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:96
SpecialPasswordPolicies\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialPasswordPolicies.php:189
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:777
UserGroupMembership\getGroupPage
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
Definition: UserGroupMembership.php:211
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:193
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:108
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:867
SpecialPasswordPolicies\$nsInfo
NamespaceInfo $nsInfo
Definition: SpecialPasswordPolicies.php:34
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:797
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:73
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:575
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:41
SpecialPasswordPolicies\__construct
__construct(NamespaceInfo $nsInfo)
Definition: SpecialPasswordPolicies.php:39
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:948
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:117
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:49
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
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:666