tests: Remove unused TableCleanupTest class
[lhc/web/wiklou.git] / includes / password / PasswordPolicyChecks.php
1 <?php
2 /**
3 * Password policy checks
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 * Functions to check passwords against a policy requirement
25 * @since 1.26
26 */
27 class PasswordPolicyChecks {
28
29 /**
30 * Check password is longer than minimum, not fatal
31 * @param int $policyVal minimal length
32 * @param User $user
33 * @param string $password
34 * @return Status error if $password is shorter than $policyVal
35 */
36 public static function checkMinimalPasswordLength( $policyVal, User $user, $password ) {
37 $status = Status::newGood();
38 if ( $policyVal > strlen( $password ) ) {
39 $status->error( 'passwordtooshort', $policyVal );
40 }
41 return $status;
42 }
43
44 /**
45 * Check password is longer than minimum, fatal
46 * @param int $policyVal minimal length
47 * @param User $user
48 * @param string $password
49 * @return Status fatal if $password is shorter than $policyVal
50 */
51 public static function checkMinimumPasswordLengthToLogin( $policyVal, User $user, $password ) {
52 $status = Status::newGood();
53 if ( $policyVal > strlen( $password ) ) {
54 $status->fatal( 'passwordtooshort', $policyVal );
55 }
56 return $status;
57 }
58
59 /**
60 * Check password is shorter than maximum, fatal
61 * @param int $policyVal maximum length
62 * @param User $user
63 * @param string $password
64 * @return Status fatal if $password is shorter than $policyVal
65 */
66 public static function checkMaximalPasswordLength( $policyVal, User $user, $password ) {
67 $status = Status::newGood();
68 if ( $policyVal < strlen( $password ) ) {
69 $status->fatal( 'passwordtoolong', $policyVal );
70 }
71 return $status;
72 }
73
74 /**
75 * Check if username and password match
76 * @param bool $policyVal true to force compliance.
77 * @param User $user
78 * @param string $password
79 * @return Status error if username and password match, and policy is true
80 */
81 public static function checkPasswordCannotMatchUsername( $policyVal, User $user, $password ) {
82 global $wgContLang;
83 $status = Status::newGood();
84 $username = $user->getName();
85 if ( $policyVal && $wgContLang->lc( $password ) === $wgContLang->lc( $username ) ) {
86 $status->error( 'password-name-match' );
87 }
88 return $status;
89 }
90
91 /**
92 * Check if username and password are on a blacklist
93 * @param bool $policyVal true to force compliance.
94 * @param User $user
95 * @param string $password
96 * @return Status error if username and password match, and policy is true
97 */
98 public static function checkPasswordCannotMatchBlacklist( $policyVal, User $user, $password ) {
99 static $blockedLogins = array(
100 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
101 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
102 );
103
104 $status = Status::newGood();
105 $username = $user->getName();
106 if ( $policyVal
107 && isset( $blockedLogins[$username] )
108 && $password == $blockedLogins[$username]
109 ) {
110 $status->error( 'password-login-forbidden' );
111 }
112 return $status;
113 }
114
115 }