henrik561 / laravel-session-tracker
此包为laravel应用提供会话跟踪功能、多会话管理和用户设备管理特性。
dev-main
2024-09-04 05:24 UTC
Requires
- php: >=5.4.0
- jenssegers/agent: ^2.3
- laravel/framework: >5.0
- nesbot/carbon: ^2.0
This package is not auto-updated.
Last update: 2024-09-11 14:13:42 UTC
README
此包为laravel应用提供会话跟踪功能、多会话管理和用户设备管理特性。
特性
- 会话管理
- 会话日志
- 用户多会话
- 请求日志
- 用户设备
安装(Laravel 5.x)
在composer.json中
"require": {
"henrik561/laravel-session-tracker" "1.0.0"
}
运行
composer update
将服务提供者添加到config/app.php
文件下的providers
'providers' => [
henrik561\SessionTracker\SessionTrackerServiceProvider::class,
]
将SessionTracker别名添加到config/app.php
文件下的aliases
'aliases' => [
'SessionTracker' => 'henrik561\SessionTracker\SessionTrackerFacade',
]
通过以下方式发布配置文件:
php artisan vendor:publish --tag="henrik561-session-tracker-config"
更新配置文件以引用您的登录和登出路由名称
config/sessionTracker.php
通过以下方式发布迁移文件:
php artisan vendor:publish --tag="henrik561-session-tracker-migrations"
迁移您的数据库
php artisan migrate
将特性添加到您的用户模型中
use henrik561\SessionTracker\Traits\SessionTrackerUserTrait;
class User extends Model {
use SessionTrackerUserTrait;
}
在kernel.php文件中添加DeviceCheck中间件
protected $middleware = [
'henrik561\SessionTracker\Middleware\DeviceCheck',
];
在您的routes.php文件中,您应该为需要跟踪的路由添加'session.tracker'中间件
Route::group(['middleware'=>'session.tracker'], function(){
Route::get('your-route', 'YourController@yourAction');
});
用法
从您的用户模型中
$user->sessions(); //returns all the sessions of the user form the begining of usage of the package
$user->activeSessions(); //returns all currently active sessions for the user. (User may be logged in with same credentials from different devices)
$user->activeSessions(true); //return all active sessions for the user except the current session.
$user->getFreshestSession(); //Returns the most recent session of the user
$user->devices(); //Returns the collection of users saved trusted devices.
$user->devicesUids(); //Returns array of users saved trusted devices Ids.
从SessionTrackerFacade中
SessionTracker::startSession(); //Creates a new session for current user
SessionTracker::endSession(); //End the current session for the current user
SessionTracker::endSession(true); //End the current session for the current user and forgets the session
SessionTracker::renewSession(); //Restarts the ended session which is not forgotten for the current user. Usefull for restarting the session after locking it for inactivity
SessionTracker::refreshSession($request); //Keeps the session alive for each request. Useful in middleware
SessionTracker::logSession($request); //Logs the current request for the current session. (request logs stored in sessiontracker_session_requests table)
SessionTracker::isSessionInactive(); //Checks if the session is inactive. Determines the inactiveness by subtracting the **delay between last activity and current time** from the **inactivity_seconds** in sessionTracker config file.
SessionTracker::isSessionInactive($user); //Checks if the session for a specific user is inactive. Determines the inactiveness by subtracting the **delay between last activity and current time** from the **inactivity_seconds** in sessionTracker config file.
SessionTracker::blockSession($sessionId); //Blocks (ends and forgets) the current session for the user. (Useful if the user wants to controll all her sessions and block a specific session in a specific location)
SessionTracker::sessionRequests($sessionId); //Returns all requests done by a specific session
SessionTracker::isSessionBlocked(); //Checks if current user does not have an active session
SessionTracker::lockSessionByCode(); //Locks a session by a security code to be unlocked by that code and returns the code. (Usefull for two-step authentication implementation)
SessionTracker::securityCode(); //Returns the security code (hash) for the locked session.
SessionTracker::isSessionLocked(); //Checks if the current session is locked by a security code.
SessionTracker::unlockSessionByCode($code); //Unlocks the locked session by passing the security code. (returns -1 if the code is invalid, -2 if it's expired and 0 if success)
SessionTracker::isUserDevice(); //Returns true if the current device is trusted by the user
SessionTracker::deleteDevice($id); //Deletes a trusted device for the user
SessionTracker::addUserDevice(); //Add the current device as trusted by the user
SessionTracker::forgotSession(); //Checks if the session if forgotten
SessionTracker::sessionId(); //Returns the sessionId for the current session
SessionTracker::deleteSession(); //Deletes and forgets the current session
SessionTracker::refreshSecurityCode(); //Renews the security code by which the current session is locked