Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / includes / password / UserPasswordPolicy.php
1 <?php
2 /**
3 * Password policy checking for a user
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Check if a user's password complies with any password policies that apply to that
25 * user, based on the user's group membership.
26 * @since 1.26
27 */
28 class UserPasswordPolicy {
29
30 /**
31 * @var array
32 */
33 private $policies;
34
35 /**
36 * Mapping of statements to the function that will test the password for compliance. The
37 * checking functions take the policy value, the user, and password, and return a Status
38 * object indicating compliance.
39 * @var array
40 */
41 private $policyCheckFunctions;
42
43 /**
44 * @param array $policies
45 * @param array $checks mapping statement to its checking function. Checking functions are
46 * called with the policy value for this user, the user object, and the password to check.
47 */
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'
60 );
61 }
62 $this->policyCheckFunctions[$statement] = $check;
63 }
64 }
65
66 /**
67 * Check if a passwords meets the effective password policy for a User.
68 * @param User $user who's policy we are checking
69 * @param string $password the password to check
70 * @return Status error to indicate the password didn't meet the policy, or fatal to
71 * indicate the user shouldn't be allowed to login.
72 */
73 public function checkUserPassword( User $user, $password ) {
74 $effectivePolicy = $this->getPoliciesForUser( $user );
75 $status = Status::newGood();
76
77 foreach ( $effectivePolicy as $policy => $value ) {
78 if ( !isset( $this->policyCheckFunctions[$policy] ) ) {
79 throw new DomainException( 'Invalid password policy config' );
80 }
81 $status->merge(
82 call_user_func(
83 $this->policyCheckFunctions[$policy],
84 $value,
85 $user,
86 $password
87 )
88 );
89 }
90
91 return $status;
92 }
93
94 /**
95 * Get the policy for a user, based on their group membership. Public so
96 * UI elements can access and inform the user.
97 * @param User $user
98 * @return array the effective policy for $user
99 */
100 public function getPoliciesForUser( User $user ) {
101 $effectivePolicy = self::getPoliciesForGroups(
102 $this->policies,
103 $user->getEffectiveGroups(),
104 $this->policies['default']
105 );
106
107 Hooks::run( 'PasswordPoliciesForUser', array( $user, &$effectivePolicy ) );
108
109 return $effectivePolicy;
110 }
111
112 /**
113 * Utility function to get the effective policy from a list of policies, based
114 * on a list of groups.
115 * @param array $policies list of policies to consider
116 * @param array $userGroups the groups from which we calculate the effective policy
117 * @param array $defaultPolicy the default policy to start from
118 * @return array effective policy
119 */
120 public static function getPoliciesForGroups( array $policies, array $userGroups,
121 array $defaultPolicy
122 ) {
123 $effectivePolicy = $defaultPolicy;
124 foreach ( $policies as $group => $policy ) {
125 if ( in_array( $group, $userGroups ) ) {
126 $effectivePolicy = self::maxOfPolicies(
127 $effectivePolicy,
128 $policies[$group]
129 );
130 }
131 }
132
133 return $effectivePolicy;
134 }
135
136 /**
137 * Utility function to get a policy that is the most restrictive of $p1 and $p2. For
138 * simplicity, we setup the policy values so the maximum value is always more restrictive.
139 * @param array $p1
140 * @param array $p2
141 * @return array containing the more restrictive values of $p1 and $p2
142 */
143 public static function maxOfPolicies( array $p1, array $p2 ) {
144 $ret = array();
145 $keys = array_merge( array_keys( $p1 ), array_keys( $p2 ) );
146 foreach ( $keys as $key ) {
147 if ( !isset( $p1[$key] ) ) {
148 $ret[$key] = $p2[$key];
149 } elseif ( !isset( $p2[$key] ) ) {
150 $ret[$key] = $p1[$key];
151 } else {
152 $ret[$key] = max( $p1[$key], $p2[$key] );
153 }
154 }
155 return $ret;
156 }
157
158 }