1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Models; |
12
|
|
|
|
13
|
|
|
use Zemit\Models\Base\AbstractSession; |
14
|
|
|
use Phalcon\Di; |
15
|
|
|
use Phalcon\Db\Column; |
16
|
|
|
use Phalcon\Mvc\ModelInterface; |
17
|
|
|
use Phalcon\Session\ManagerInterface; |
18
|
|
|
use Phalcon\Mvc\Model\Behavior\Timestampable; |
19
|
|
|
use Phalcon\Validation\Validator\Date; |
20
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
21
|
|
|
use Phalcon\Validation\Validator\Uniqueness; |
22
|
|
|
use Phalcon\Validation\Validator\StringLength\Max; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @property User $User |
26
|
|
|
* @property User $UserAs |
27
|
|
|
* |
28
|
|
|
* @method User getUser(?array $params = null) |
29
|
|
|
* @method User getUserAs(?array $params = null) |
30
|
|
|
*/ |
31
|
|
|
class Session extends AbstractSession implements SessionInterface |
32
|
|
|
{ |
33
|
|
|
protected $deleted = self::NO; |
34
|
|
|
|
35
|
|
|
public function initialize(): void |
36
|
|
|
{ |
37
|
|
|
parent::initialize(); |
38
|
|
|
|
39
|
|
|
$this->belongsTo('asUserId', User::class, 'id', ['alias' => 'UserAsEntity']); |
40
|
|
|
|
41
|
|
|
// refresh date |
42
|
|
|
$this->addBehavior(new Timestampable([ |
43
|
|
|
'beforeValidation' => [ |
44
|
|
|
'field' => 'date', |
45
|
|
|
'format' => 'Y-m-d H:i:s', |
46
|
|
|
], |
47
|
|
|
])); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function validation(): bool |
51
|
|
|
{ |
52
|
|
|
$validator = $this->genericValidation(); |
53
|
|
|
|
54
|
|
|
$validator->add('key', new PresenceOf(['message' => $this->_('required')])); |
55
|
|
|
$validator->add('key', new Uniqueness(['message' => $this->_('not-unique')])); |
56
|
|
|
$validator->add('key', new Max(['max' => 60, 'message' => $this->_('length-exceeded')])); |
57
|
|
|
|
58
|
|
|
$validator->add('token', new PresenceOf(['message' => $this->_('required')])); |
59
|
|
|
$validator->add('token', new Max(['max' => 128, 'message' => $this->_('length-exceeded')])); |
60
|
|
|
|
61
|
|
|
$validator->add('date', new PresenceOf(['message' => $this->_('required')])); |
62
|
|
|
$validator->add('date', new Date(['format' => 'Y-m-d H:i:s', 'message' => $this->_('date-not-valid')])); |
63
|
|
|
|
64
|
|
|
return $this->validate($validator); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function save(): bool |
68
|
|
|
{ |
69
|
|
|
return self::isSessionAdapter() |
70
|
|
|
? $this->saveIntoSession() |
71
|
|
|
: parent::save(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function update(): bool |
75
|
|
|
{ |
76
|
|
|
return self::isSessionAdapter() |
77
|
|
|
? $this->saveIntoSession() |
78
|
|
|
: parent::update(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function create(): bool |
82
|
|
|
{ |
83
|
|
|
return self::isSessionAdapter() |
84
|
|
|
? $this->saveIntoSession() |
85
|
|
|
: parent::create(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function delete(): bool |
89
|
|
|
{ |
90
|
|
|
return self::isSessionAdapter() |
91
|
|
|
? $this->removeFromSession() |
92
|
|
|
: parent::delete(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function findFirstByKey(?string $key = null): ?ModelInterface |
96
|
|
|
{ |
97
|
|
|
if (empty($key)) { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// query the session manager adapter |
102
|
|
|
if (self::isSessionAdapter()) { |
103
|
|
|
$session = self::getSessionManager(); |
104
|
|
|
if ($session->has('zemit-session-' . $key)) { |
105
|
|
|
$sessionStore = $session->get('zemit-session-' . $key); |
106
|
|
|
$model = new self(); |
107
|
|
|
$model->setKey($sessionStore['key']); |
108
|
|
|
$model->setToken($sessionStore['token']); |
109
|
|
|
$model->setDate($sessionStore['date'] ?? date('Y-m-d H:i:s')); |
110
|
|
|
$model->setUserId($sessionStore['userId'] ?? null); |
111
|
|
|
$model->setAsUserId($sessionStore['asUserId'] ?? null); |
112
|
|
|
return $model; |
113
|
|
|
} |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// query the database |
118
|
|
|
return parent::findFirst([ |
119
|
|
|
'key = :key:', |
120
|
|
|
'bind' => ['key' => $key], |
121
|
|
|
'bindTypes' => ['key' => Column::BIND_PARAM_STR], |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function isSessionAdapter(): bool |
126
|
|
|
{ |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function saveIntoSession(): bool |
131
|
|
|
{ |
132
|
|
|
$session = $this->getSessionManager(); |
133
|
|
|
$session->set('zemit-session-' . $this->getKey(), $this->toArray()); |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function removeFromSession(): bool |
138
|
|
|
{ |
139
|
|
|
$session = $this->getSessionManager(); |
140
|
|
|
$session->remove('zemit-session-' . $this->getKey()); |
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public static function getSessionManager(): ManagerInterface |
145
|
|
|
{ |
146
|
|
|
return Di::getDefault()->get('session'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|