Add session history

This commit is contained in:
maranqz
2019-11-14 20:55:18 +03:00
parent 8dea2d8078
commit be2b495c9e
26 changed files with 1639 additions and 4 deletions

View File

@ -0,0 +1,158 @@
<?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Da\User\Query;
use Da\User\Traits\ModuleAwareTrait;
use yii\web\Session;
use Yii;
class SessionHistoryCondition
{
use ModuleAwareTrait;
private $session;
public function __construct(Session $session)
{
$this->session = $session;
}
public function unbindSession()
{
return ['session_id' => null];
}
public function bySession($sessionId)
{
return ['session_id' => $sessionId];
}
public function byUser($userId)
{
return [
'user_id' => $userId,
];
}
public function byUserSession($userId, $sessionId)
{
return [
'user_id' => $userId,
'session_id' => $sessionId,
];
}
public function inactive($userId = null)
{
$where = [
'AND',
['session_id' => null]
];
if (isset($userId)) {
$where[] = $this->byUser($userId);
}
return $where;
}
public function expired($userId = null)
{
$where = [
'AND',
['<', 'updated_at', $this->getExpiredTime()]
];
if (isset($userId)) {
$where[] = $this->byUser($userId);
}
return $where;
}
public function expiredInactive($userId = null)
{
return [
'OR',
$this->expired($userId),
$this->inactive($userId),
];
}
public function shouldDeleteBefore($updatedAt, $userId)
{
$condition = ['<', 'updated_at', $updatedAt];
if ($updatedAt > $this->getExpiredTime()) {
$condition = [
'OR',
[
'AND',
$this->inactive(),
$condition,
],
$this->expired()
];
}
return [
'AND',
$this->byUser($userId),
$condition,
];
}
/**
* @return int
*/
public function getExpiredTime()
{
$module = $this->getModule();
$time = time() - max($module->rememberLoginLifespan, $this->session->getTimeout());
if (false === $module->hasTimeoutSessionHistory()) {
return $time;
}
return $time - $module->timeoutSessionHistory;
}
public function inactiveData()
{
return [
'session_id' => null,
];
}
/**
* @return array
*/
public function currentUserData()
{
return [
'user_id' => Yii::$app->user->id,
'session_id' => Yii::$app->session->getId(),
'user_agent' => Yii::$app->request->userAgent,
'ip' => Yii::$app->request->userIP,
];
}
/**
* @return array
*/
public function currentUserCondition()
{
return [
'user_id' => Yii::$app->user->id,
'session_id' => Yii::$app->session->getId(),
'user_agent' => Yii::$app->request->userAgent,
];
}
}

View File

@ -0,0 +1,82 @@
<?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Da\User\Query;
use Da\User\Traits\ModuleAwareTrait;
use yii\db\ActiveQuery;
use Yii;
class SessionHistoryQuery extends ActiveQuery
{
use ModuleAwareTrait;
public function whereUserId($userId)
{
return $this->andWhere($this->getCondition()->byUser($userId));
}
public function whereActive()
{
return $this->andWhere(['IS NOT', 'session_id', null]);
}
public function whereInActive($userId)
{
return $this->andWhere($this->getCondition()->inactive($userId));
}
public function whereExpired($userId)
{
return $this->andWhere($this->getCondition()->expired($userId));
}
public function whereExpiredInActive($userId)
{
return $this->andWhere($this->getCondition()->expiredInactive($userId));
}
public function selectSessionId()
{
return $this->select(['session_id']);
}
public function whereUserSession($userId, $sessionId)
{
return $this->andWhere($this->getCondition()->byUserSession(
$userId,
$sessionId
));
}
public function whereCurrentUser()
{
return $this->andWhere($this->getCondition()->currentUserCondition());
}
public function oldestUpdatedTimeActiveSession($userId)
{
return $this->whereExpiredInActive($userId)
->select(['updated_at'])
->limit(1)
->offset($this->getModule()->numberSessionHistory)
->orderBy(['updated_at' => SORT_DESC])->scalar();
}
/**
* @return SessionHistoryCondition
*/
protected function getCondition()
{
return Yii::$container->get(SessionHistoryCondition::class);
}
}