/** * Checks if there are not too many login attempts using specified username in the specified number of seconds until now. * @param string $username * @param integer $count_limit number of login attempts * @param integer $time_limit number of seconds * @return boolean */ public static function hasTooManyFailedAttempts($username, $count_limit = 5, $time_limit = 1800) { $since = new DateTime(); $since->sub(new DateInterval("PT{$time_limit}S")); $subquery = UserLoginAttempt::model()->dbConnection->createCommand()->select('is_successful')->from(UserLoginAttempt::model()->tableName())->where('username = :username AND performed_on > :since')->order('performed_on DESC')->limit($count_limit)->getText(); return $count_limit <= (int) UserLoginAttempt::model()->dbConnection->createCommand()->select('COUNT(NOT is_successful OR NULL)')->from("({$subquery}) AS t")->queryScalar(array(':username' => $username, ':since' => $since->format('Y-m-d H:i:s'))); }