MediaWiki  1.29.2
UserPasswordPolicy.php
Go to the documentation of this file.
1 <?php
29 
33  private $policies;
34 
42 
48  public function __construct( array $policies, array $checks ) {
49  if ( !isset( $policies['default'] ) ) {
50  throw new InvalidArgumentException(
51  'Must include a \'default\' password policy'
52  );
53  }
54  $this->policies = $policies;
55 
56  foreach ( $checks as $statement => $check ) {
57  if ( !is_callable( $check ) ) {
58  throw new InvalidArgumentException(
59  "Policy check functions must be callable. '$statement' isn't callable."
60  );
61  }
62  $this->policyCheckFunctions[$statement] = $check;
63  }
64  }
65 
73  public function checkUserPassword( User $user, $password ) {
74  $effectivePolicy = $this->getPoliciesForUser( $user );
75  return $this->checkPolicies(
76  $user,
77  $password,
78  $effectivePolicy,
79  $this->policyCheckFunctions
80  );
81  }
82 
93  public function checkUserPasswordForGroups( User $user, $password, array $groups ) {
94  $effectivePolicy = self::getPoliciesForGroups(
95  $this->policies,
96  $groups,
97  $this->policies['default']
98  );
99  return $this->checkPolicies(
100  $user,
101  $password,
102  $effectivePolicy,
103  $this->policyCheckFunctions
104  );
105  }
106 
114  private function checkPolicies( User $user, $password, $policies, $policyCheckFunctions ) {
116  foreach ( $policies as $policy => $value ) {
117  if ( !isset( $policyCheckFunctions[$policy] ) ) {
118  throw new DomainException( "Invalid password policy config. No check defined for '$policy'." );
119  }
120  $status->merge(
121  call_user_func(
122  $policyCheckFunctions[$policy],
123  $value,
124  $user,
125  $password
126  )
127  );
128  }
129  return $status;
130  }
131 
138  public function getPoliciesForUser( User $user ) {
139  $effectivePolicy = self::getPoliciesForGroups(
140  $this->policies,
141  $user->getEffectiveGroups(),
142  $this->policies['default']
143  );
144 
145  Hooks::run( 'PasswordPoliciesForUser', [ $user, &$effectivePolicy ] );
146 
147  return $effectivePolicy;
148  }
149 
158  public static function getPoliciesForGroups( array $policies, array $userGroups,
159  array $defaultPolicy
160  ) {
161  $effectivePolicy = $defaultPolicy;
162  foreach ( $policies as $group => $policy ) {
163  if ( in_array( $group, $userGroups ) ) {
164  $effectivePolicy = self::maxOfPolicies(
165  $effectivePolicy,
166  $policies[$group]
167  );
168  }
169  }
170 
171  return $effectivePolicy;
172  }
173 
181  public static function maxOfPolicies( array $p1, array $p2 ) {
182  $ret = [];
183  $keys = array_merge( array_keys( $p1 ), array_keys( $p2 ) );
184  foreach ( $keys as $key ) {
185  if ( !isset( $p1[$key] ) ) {
186  $ret[$key] = $p2[$key];
187  } elseif ( !isset( $p2[$key] ) ) {
188  $ret[$key] = $p1[$key];
189  } else {
190  $ret[$key] = max( $p1[$key], $p2[$key] );
191  }
192  }
193  return $ret;
194  }
195 
196 }
UserPasswordPolicy\maxOfPolicies
static maxOfPolicies(array $p1, array $p2)
Utility function to get a policy that is the most restrictive of $p1 and $p2.
Definition: UserPasswordPolicy.php:181
UserPasswordPolicy\getPoliciesForUser
getPoliciesForUser(User $user)
Get the policy for a user, based on their group membership.
Definition: UserPasswordPolicy.php:138
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
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:158
UserPasswordPolicy\checkUserPassword
checkUserPassword(User $user, $password)
Check if a passwords meets the effective password policy for a User.
Definition: UserPasswordPolicy.php:73
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
UserPasswordPolicy\checkPolicies
checkPolicies(User $user, $password, $policies, $policyCheckFunctions)
Definition: UserPasswordPolicy.php:114
UserPasswordPolicy\__construct
__construct(array $policies, array $checks)
Definition: UserPasswordPolicy.php:48
UserPasswordPolicy
Check if a user's password complies with any password policies that apply to that user,...
Definition: UserPasswordPolicy.php:28
$value
$value
Definition: styleTest.css.php:45
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
UserPasswordPolicy\checkUserPasswordForGroups
checkUserPasswordForGroups(User $user, $password, array $groups)
Check if a passwords meets the effective password policy for a User, using a set of groups they may o...
Definition: UserPasswordPolicy.php:93
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1956
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:65
UserPasswordPolicy\$policies
array $policies
Definition: UserPasswordPolicy.php:33
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
array
the array() calling protocol came about after MediaWiki 1.4rc1.
UserPasswordPolicy\$policyCheckFunctions
array $policyCheckFunctions
Mapping of statements to the function that will test the password for compliance.
Definition: UserPasswordPolicy.php:41