hamedmehryar/laravel-session-tracker

此包为 Laravel 应用程序提供会话跟踪功能、多会话管理和用户设备管理功能。

1.0.0 2017-01-17 04:59 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:52:54 UTC


README

此包为 Laravel 应用程序提供会话跟踪功能、多会话管理和用户设备管理功能。

功能

  • 会话管理
  • 会话日志
  • 用户多会话
  • 请求日志
  • 用户设备

安装(Laravel 5.x)

在 composer.json 中

"require": {
    "hamedmehryar/laravel-session-tracker" "1.0.0"
}

运行

composer update

将服务提供者添加到 config/app.php 文件下的 providers

'providers' => [
    Hamedmehryar\SessionTracker\SessionTrackerServiceProvider::class,
]

将 SessionTracker 别名添加到 config/app.php 文件下的 aliases

    'aliases' => [
        'SessionTracker' => 'Hamedmehryar\SessionTracker\SessionTrackerFacade',
    ]

更新配置文件以引用您的登录和登出路由名称

config/sessionTracker.php

迁移数据库

php artisan migrate

将特质添加到您的用户模型中

use Hamedmehryar\SessionTracker\Traits\SessionTrackerUserTrait;

class User extends Model {
	use SessionTrackerUserTrait;
}

在 kernel.php 文件中添加 DeviceCheck 中间件

protected $middleware = [
		'Hamedmehryar\SessionTracker\Middleware\DeviceCheck',
	];

在您的 routes.php 文件中,您应该为要跟踪的路由添加 'session' 中间件

Route::group(['middleware'=>'session'], 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

作者