yadahan / laravel-authentication-log
Laravel Authentication Log 为 Laravel 提供认证日志和通知功能。
Requires
- php: ^7.2.5|^8.0
- illuminate/auth: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/bus: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/console: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/contracts: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/database: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/http: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/notifications: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
- illuminate/support: ^7.30.4|^8.40.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0|^7.0|^8.0
- phpunit/phpunit: ^8.4|^9.3.3|^10.0
Suggests
- guzzlehttp/guzzle: Required to use the Slack transport (~6.0)
- nexmo/client: Required to use the Nexmo transport (~1.0).
README
安装
Laravel Authentication Log 需要 Laravel 5.5 或更高版本,以及 PHP 7.0+。
您可以使用 Composer 将 Laravel Authentication Log 安装到您的 Laravel 项目中
composer require yadahan/laravel-authentication-log
配置
安装 Laravel Authentication Log 后,使用 vendor:publish
Artisan 命令发布其配置、迁移和视图
php artisan vendor:publish --provider="Yadahan\AuthenticationLog\AuthenticationLogServiceProvider"
接下来,您需要迁移您的数据库。Laravel Authentication Log 迁移将创建您的应用程序存储认证日志所需的表
php artisan migrate
最后,将 AuthenticationLogable
和 Notifiable
特性添加到您的可认证模型中(默认为 App\User
模型)。这些特性提供各种方法,允许您获取常见的认证日志数据,例如最后登录时间、最后登录 IP 地址,并设置通知用户从新设备登录的通道
use Illuminate\Notifications\Notifiable; use Yadahan\AuthenticationLog\AuthenticationLogable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, AuthenticationLogable; }
基本用法
获取用户的全部认证日志
User::find(1)->authentications;
获取用户最后登录信息
User::find(1)->lastLoginAt(); User::find(1)->lastLoginIp();
获取用户之前的登录时间及 IP 地址(忽略当前登录)
auth()->user()->previousLoginAt(); auth()->user()->previousLoginIp();
通知新设备登录
通知可以通过 mail
、nexmo
和 slack
通道发送。默认通过电子邮件通知。
您可以通过定义 notifyAuthenticationLogVia
方法来决定通知通过哪些通道发送
/** * The Authentication Log notifications delivery channels. * * @return array */ public function notifyAuthenticationLogVia() { return ['nexmo', 'mail', 'slack']; }
当然,您可以通过将 config/authentication-log.php
配置文件中的 notify
选项设置为 false
来禁用通知
'notify' => env('AUTHENTICATION_LOG_NOTIFY', false),
清除旧日志
您可以使用 authentication-log:clear
Artisan 命令清除旧的认证日志记录
php artisan authentication-log:clear
将删除在您的 config/authentication-log.php
中 older
选项指定的天数之前创建的记录
'older' => 365,
贡献
感谢您考虑为 Laravel Authentication Log 做出贡献。
许可
Laravel Authentication Log 是开源软件,许可协议为 MIT 协议。