Completed
Pull Request — master (#361)
by
unknown
03:25
created

SessionHistory::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Model;
13
14
use Da\User\Module;
15
use Da\User\Query\SessionHistoryQuery;
16
use Da\User\Traits\ModuleAwareTrait;
17
use Yii;
18
use yii\behaviors\TimestampBehavior;
19
use yii\db\ActiveRecord;
20
use yii\db\ActiveQuery;
21
22
/**
23
 * @property int $user_id
24
 * @property string $session_id
25
 * @property string $user_agent
26
 * @property string $ip
27
 * @property int $created_at
28
 * @property int $updated_at
29
 *
30
 * @property User $user
31
 * @property bool $isActive
32
 *
33
 * Dependencies:
34
 * @property-read Module $module
35
 */
36
class SessionHistory extends ActiveRecord
37
{
38
    use ModuleAwareTrait;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public static function tableName()
44
    {
45
        return '{{%session_history}}';
46
    }
47
48
    /** @inheritdoc */
49
    public function behaviors()
50
    {
51
        return [
52
            [
53
                'class' => TimestampBehavior::class,
54
                'updatedAtAttribute' => false,
55
            ]
56
        ];
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function attributeLabels()
63
    {
64
        return [
65
            'user_id' => Yii::t('usuario', 'User ID'),
66
            'session_id' => Yii::t('usuario', 'Session ID'),
67
            'user_agent' => Yii::t('usuario', 'User agent'),
68
            'ip' => Yii::t('usuario', 'IP'),
69
            'created_at' => Yii::t('usuario', 'Created at'),
70
            'updated_at' => Yii::t('usuario', 'Last activity'),
71
        ];
72
    }
73
74
    /**
75
     * @return bool Whether the session is an active or not.
76
     */
77
    public function getIsActive()
78
    {
79
        return isset($this->session_id) && $this->updated_at + $this->getModule()->rememberLoginLifespan > time();
80
    }
81
82
    /**
83
     * @return ActiveQuery
84
     */
85
    public function getUser()
86
    {
87
        return $this->hasOne($this->module->classMap['User'], ['id' => 'user_id']);
88
    }
89
90
    /** @inheritdoc */
91
    public function beforeSave($insert)
92
    {
93
        if ($insert && empty($this->session_id)) {
94
            $this->setAttribute('session_id', Yii::$app->session->getId());
95
        }
96
97
        return parent::beforeSave($insert);
98
    }
99
100
    /** @inheritdoc */
101
    public static function primaryKey()
102
    {
103
        return ['user_id', 'session_id'];
104
    }
105
106
    public static function find()
107
    {
108
        return new SessionHistoryQuery(static::class);
109
    }
110
}
111