MediaWiki  master
SpecialPasswordPolicies.php
Go to the documentation of this file.
1 <?php
28 
37 
39  private $userGroupManager;
40 
44  public function __construct( UserGroupManager $userGroupManager ) {
45  parent::__construct( 'PasswordPolicies' );
46  $this->userGroupManager = $userGroupManager;
47  }
48 
53  public function execute( $par ) {
54  $this->setHeaders();
55  $this->outputHeader();
56 
57  $out = $this->getOutput();
58  $out->addModuleStyles( 'mediawiki.special' );
59 
60  // TODO: Have specific user documentation page for this feature
61  $this->addHelpLink( 'Manual:$wgPasswordPolicy' );
62 
63  $out->addHTML(
64  Xml::openElement( 'table', [ 'class' => 'wikitable mw-passwordpolicies-table' ] ) .
65  '<tr>' .
66  Xml::element( 'th', null, $this->msg( 'passwordpolicies-group' )->text() ) .
67  Xml::element( 'th', null, $this->msg( 'passwordpolicies-policies' )->text() ) .
68  '</tr>'
69  );
70 
71  $config = $this->getConfig();
72  $policies = $config->get( MainConfigNames::PasswordPolicy );
73 
74  $implicitGroups = $this->userGroupManager->listAllImplicitGroups();
75  $allGroups = array_merge(
76  $this->userGroupManager->listAllGroups(),
77  $implicitGroups
78  );
79  asort( $allGroups );
80 
81  $linkRenderer = $this->getLinkRenderer();
82  $lang = $this->getLanguage();
83 
84  foreach ( $allGroups as $group ) {
85  if ( $group == '*' ) {
86  continue;
87  }
88 
89  $groupnameLocalized = $lang->getGroupName( $group );
90 
91  $grouppageLocalizedTitle = UserGroupMembership::getGroupPage( $group )
92  ?: Title::makeTitle( NS_PROJECT, $group );
93 
94  $grouppage = $linkRenderer->makeLink(
95  $grouppageLocalizedTitle,
96  $groupnameLocalized
97  );
98 
99  if ( $group === 'user' ) {
100  // Link to Special:listusers for implicit group 'user'
101  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
102  SpecialPage::getTitleFor( 'Listusers' ),
103  $this->msg( 'listgrouprights-members' )->text()
104  );
105  } elseif ( !in_array( $group, $implicitGroups ) ) {
106  $grouplink = '<br />' . $linkRenderer->makeKnownLink(
107  SpecialPage::getTitleFor( 'Listusers' ),
108  $this->msg( 'listgrouprights-members' )->text(),
109  [],
110  [ 'group' => $group ]
111  );
112  } else {
113  // No link to Special:listusers for other implicit groups as they are unlistable
114  $grouplink = '';
115  }
116 
117  $out->addHTML( Html::rawElement( 'tr', [ 'id' => Sanitizer::escapeIdForAttribute( $group ) ], "
118  <td>$grouppage$grouplink</td>
119  <td>" . $this->formatPolicies( $policies, $group ) . '</td>
120  '
121  ) );
122 
123  }
124 
125  $out->addHTML( Xml::closeElement( 'table' ) );
126  }
127 
136  private function formatPolicies( $policies, $group ) {
138  $policies['policies'],
139  [ $group ],
140  $policies['policies']['default']
141  );
142 
143  $ret = [];
144  foreach ( $groupPolicies as $gp => $settings ) {
145  if ( !is_array( $settings ) ) {
146  $settings = [ 'value' => $settings ];
147  }
148  $val = $settings['value'];
149  $flags = array_diff_key( $settings, [ 'value' => true ] );
150  if ( !$val ) {
151  // Policy isn't enabled, so no need to display it
152  continue;
153  }
154  $msg = $this->msg( 'passwordpolicies-policy-' . strtolower( $gp ) )->numParams( $val );
155  $flagMsgs = [];
156  foreach ( array_filter( $flags ) as $flag => $value ) {
157  $flagMsg = $this->msg( 'passwordpolicies-policyflag-' . strtolower( $flag ) );
158  $flagMsg->params( $value );
159  $flagMsgs[] = $flagMsg;
160  }
161  if ( $flagMsgs ) {
162  $ret[] = $this->msg(
163  'passwordpolicies-policy-displaywithflags',
164  $msg,
165  '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>',
166  $this->getLanguage()->commaList( $flagMsgs )
167  )->parse();
168  } else {
169  $ret[] = $this->msg(
170  'passwordpolicies-policy-display',
171  $msg,
172  '<span class="mw-passwordpolicies-policy-name">' . $gp . '</span>'
173  )->parse();
174  }
175  }
176  if ( $ret === [] ) {
177  return '';
178  } else {
179  return '<ul><li>' . implode( "</li>\n<li>", $ret ) . '</li></ul>';
180  }
181  }
182 
183  protected function getGroupName() {
184  return 'users';
185  }
186 }
const NS_PROJECT
Definition: Defines.php:68
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition: Title.php:82
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:946
Parent class for all special pages.
Definition: SpecialPage.php:45
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
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,...
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
This special page lists the defined password policies for user groups.
__construct(UserGroupManager $userGroupManager)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $par)
Show the special page.
static getGroupPage( $group)
Gets the title of a page describing a particular user group.
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.
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:122
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:113
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:44
if(!isset( $args[0])) $lang