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