-
app/Listeners/FailedLoginListener.php
Open in GitHubuse App\Models\FailedLoginAttempt; use App\Models\Group; use App\Notifications\FailedLogin; class FailedLoginListener { public function handle($event) { $bannedGroup = \cache()->rememberForever('banned_group', fn () => Group::where('slug', '=', 'banned')->pluck('id')); if (\property_exists($event, 'user') && $event->user !== null && $event->user instanceof \Illuminate\Database\Eloquent\Model && $event->user->group_id !== $bannedGroup[0]) { FailedLoginAttempt::record( $event->user, \request()->input('username'), \request()->ip() ); $event->user->notify(new FailedLogin( \request()->ip() )); } } }
-
app/Notifications/FailedLogin.php
Open in GitHubuse Carbon\Carbon; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class FailedLogin extends Notification { public function __construct(public $ip) { } public function via($notifiable) { return ['mail']; } public function toArray($notifiable) { return ['ip' => $this->ip, 'time' => Carbon::now()]; } public function toMail($notifiable) { return (new MailMessage())->error()->subject(\trans('email.fail-login-subject'))->greeting(\trans('email.fail-login-greeting'))->line(\trans('email.fail-login-line1'))->line(\trans('email.fail-login-line2', ['ip' => $this->ip, 'host' => \gethostbyaddr($this->ip), 'time' => Carbon::now()])); } }