Files
Virtnet.bond/extensions/Others/Security/Models/LoginAttempt.php
2025-11-30 11:57:22 +01:00

70 lines
1.8 KiB
PHP

<?php
namespace Paymenter\Extensions\Others\Security\Models;
use Illuminate\Database\Eloquent\Model;
class LoginAttempt extends Model
{
protected $table = 'ext_security_login_attempts';
protected $fillable = [
'ip_address',
'email',
'successful',
'user_agent',
'country',
'notes',
'attempted_at',
];
protected $casts = [
'successful' => 'boolean',
'attempted_at' => 'datetime',
];
/**
* Record a login attempt
*/
public static function recordAttempt(
string $ipAddress,
?string $email = null,
bool $successful = false,
?string $userAgent = null,
?string $country = null,
?string $notes = null
): self {
return static::create([
'ip_address' => $ipAddress,
'email' => $email,
'successful' => $successful,
'user_agent' => $userAgent,
'country' => $country,
'notes' => $notes,
'attempted_at' => now(),
]);
}
/**
* Get recent failed attempts for an IP
*/
public static function getRecentFailedAttempts(string $ipAddress, int $minutes = 60): int
{
return static::where('ip_address', $ipAddress)
->where('successful', false)
->where('attempted_at', '>', now()->subMinutes($minutes))
->count();
}
/**
* Get recent failed attempts for an email
*/
public static function getRecentFailedAttemptsForEmail(string $email, int $minutes = 60): int
{
return static::where('email', $email)
->where('successful', false)
->where('attempted_at', '>', now()->subMinutes($minutes))
->count();
}
}