MediaWiki REL1_28
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
74 public function checkUserPassword( User $user, $password, $purpose = 'login' ) {
75 $effectivePolicy = $this->getPoliciesForUser( $user, $purpose );
76 return $this->checkPolicies(
77 $user,
78 $password,
79 $effectivePolicy,
80 $this->policyCheckFunctions
81 );
82 }
83
94 public function checkUserPasswordForGroups( User $user, $password, array $groups ) {
95 $effectivePolicy = self::getPoliciesForGroups(
96 $this->policies,
97 $groups,
98 $this->policies['default']
99 );
100 return $this->checkPolicies(
101 $user,
102 $password,
103 $effectivePolicy,
104 $this->policyCheckFunctions
105 );
106 }
107
115 private function checkPolicies( User $user, $password, $policies, $policyCheckFunctions ) {
116 $status = Status::newGood();
117 foreach ( $policies as $policy => $value ) {
118 if ( !isset( $policyCheckFunctions[$policy] ) ) {
119 throw new DomainException( "Invalid password policy config. No check defined for '$policy'." );
120 }
121 $status->merge(
122 call_user_func(
123 $policyCheckFunctions[$policy],
124 $value,
125 $user,
126 $password
127 )
128 );
129 }
130 return $status;
131 }
132
140 public function getPoliciesForUser( User $user, $purpose = 'login' ) {
141 $effectivePolicy = $this->policies['default'];
142 if ( $purpose !== 'create' ) {
143 $effectivePolicy = self::getPoliciesForGroups(
144 $this->policies,
145 $user->getEffectiveGroups(),
146 $this->policies['default']
147 );
148 }
149
150 Hooks::run( 'PasswordPoliciesForUser', [ $user, &$effectivePolicy, $purpose ] );
151
152 return $effectivePolicy;
153 }
154
163 public static function getPoliciesForGroups( array $policies, array $userGroups,
164 array $defaultPolicy
165 ) {
166 $effectivePolicy = $defaultPolicy;
167 foreach ( $policies as $group => $policy ) {
168 if ( in_array( $group, $userGroups ) ) {
169 $effectivePolicy = self::maxOfPolicies(
170 $effectivePolicy,
171 $policies[$group]
172 );
173 }
174 }
175
176 return $effectivePolicy;
177 }
178
186 public static function maxOfPolicies( array $p1, array $p2 ) {
187 $ret = [];
188 $keys = array_merge( array_keys( $p1 ), array_keys( $p2 ) );
189 foreach ( $keys as $key ) {
190 if ( !isset( $p1[$key] ) ) {
191 $ret[$key] = $p2[$key];
192 } elseif ( !isset( $p2[$key] ) ) {
193 $ret[$key] = $p1[$key];
194 } else {
195 $ret[$key] = max( $p1[$key], $p2[$key] );
196 }
197 }
198 return $ret;
199 }
200
201}
Check if a user's password complies with any password policies that apply to that user,...
checkUserPassword(User $user, $password, $purpose='login')
Check if a passwords meets the effective password policy for a User.
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.
getPoliciesForUser(User $user, $purpose='login')
Get the policy for a user, based on their group membership.
__construct(array $policies, array $checks)
checkPolicies(User $user, $password, $policies, $policyCheckFunctions)
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...
static maxOfPolicies(array $p1, array $p2)
Utility function to get a policy that is the most restrictive of $p1 and $p2.
array $policyCheckFunctions
Mapping of statements to the function that will test the password for compliance.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
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
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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
the array() calling protocol came about after MediaWiki 1.4rc1.
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 local account $user
Definition hooks.txt:249
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:1949
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:37