class-login-attempts.php
175 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the plugin login attempts |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage login-attempts |
| 7 | */ |
| 8 | |
| 9 | namespace WP2FA\Admin\Controllers; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 12 | |
| 13 | /** |
| 14 | * Login Attempts class |
| 15 | */ |
| 16 | if ( ! class_exists( '\WP2FA\Admin\Controllers\Login_Attempts' ) ) { |
| 17 | |
| 18 | /** |
| 19 | * Responsible for the login attempts |
| 20 | * |
| 21 | * @since 2.0.0 |
| 22 | */ |
| 23 | class Login_Attempts { |
| 24 | |
| 25 | /** |
| 26 | * Holds the name of the meta key |
| 27 | * |
| 28 | * @var string |
| 29 | * |
| 30 | * @since 2.0.0 |
| 31 | */ |
| 32 | private $meta_key = WP_2FA_PREFIX . 'login-attempts'; |
| 33 | |
| 34 | /** |
| 35 | * Holds the number of allowed attempts to login |
| 36 | * |
| 37 | * @var integer |
| 38 | * |
| 39 | * @since 2.0.0 |
| 40 | */ |
| 41 | private $number_of_allowed_attempts = 3; |
| 42 | |
| 43 | /** |
| 44 | * Default constructor |
| 45 | * |
| 46 | * @param string $meta_key - The meta key name. |
| 47 | * @param integer $attempts - Number of the allowed login attempts. |
| 48 | * |
| 49 | * @since 2.0.0 |
| 50 | */ |
| 51 | public function __construct( string $meta_key = '', int $attempts = 0 ) { |
| 52 | if ( '' !== trim( $meta_key ) ) { |
| 53 | $this->meta_key = $meta_key; |
| 54 | } |
| 55 | if ( 0 !== $attempts ) { |
| 56 | $this->number_of_allowed_attempts = $attempts; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Increasing login attempts for User |
| 62 | * |
| 63 | * @since 2.0.0 |
| 64 | * |
| 65 | * @param \WP_User $user - the WP User. |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function increase_login_attempts( \WP_User $user ): void { |
| 70 | $attempts = $this->get_login_attempts( $user ); |
| 71 | if ( '' === $attempts ) { |
| 72 | $attempts = 0; |
| 73 | } |
| 74 | \update_user_meta( $user->ID, $this->meta_key, ++$attempts ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the number of unsuccessful attempts for the User |
| 79 | * |
| 80 | * @since 2.0.0 |
| 81 | * |
| 82 | * @param \WP_User $user - the WP User. |
| 83 | * |
| 84 | * @return integer |
| 85 | */ |
| 86 | public function get_login_attempts( \WP_User $user ): int { |
| 87 | return (int) \get_user_meta( $user->ID, $this->meta_key, true ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Clearing login attempts for User |
| 92 | * |
| 93 | * @since 2.0.0 |
| 94 | * |
| 95 | * @param \WP_User $user - the WP User. |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | public function clear_login_attempts( \WP_User $user ): void { |
| 100 | \delete_user_meta( $user->ID, $this->meta_key ); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Returns the number of allowed login attempts |
| 106 | * |
| 107 | * @return integer |
| 108 | * |
| 109 | * @since 2.0.0 |
| 110 | */ |
| 111 | public function get_allowed_login_attempts(): int { |
| 112 | return $this->number_of_allowed_attempts; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets the number of allowed attempts |
| 117 | * |
| 118 | * @param integer $number - The number of the allowed attempts. |
| 119 | * |
| 120 | * @return integer |
| 121 | * |
| 122 | * @since 2.0.0 |
| 123 | */ |
| 124 | public function set_number_of_login_attempts( int $number ): int { |
| 125 | $this->number_of_allowed_attempts = $number; |
| 126 | |
| 127 | return $this->number_of_allowed_attempts; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the name of the meta key holding the login attempts for the user |
| 132 | * |
| 133 | * @return string |
| 134 | * |
| 135 | * @since 2.0.0 |
| 136 | */ |
| 137 | public function get_meta_key(): string { |
| 138 | |
| 139 | return $this->meta_key; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Sets the login attempts meta key |
| 144 | * |
| 145 | * @param string $meta_key - The name of the meta. |
| 146 | * |
| 147 | * @return string |
| 148 | * |
| 149 | * @since 2.0.0 |
| 150 | */ |
| 151 | public function set_meta_key( string $meta_key ): string { |
| 152 | $this->meta_key = $meta_key; |
| 153 | |
| 154 | return $this->meta_key; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Checks the number of login attempts |
| 159 | * |
| 160 | * @param \WP_User $user - The user we have to check for. |
| 161 | * |
| 162 | * @return boolean |
| 163 | * |
| 164 | * @since 2.0.0 |
| 165 | */ |
| 166 | public function check_number_of_attempts( \WP_User $user ):bool { |
| 167 | if ( $this->get_allowed_login_attempts() < $this->get_login_attempts( $user ) ) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 |