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\Di; |
14
|
|
|
use Phalcon\Session\Manager; |
15
|
|
|
use Zemit\Models\Base\AbstractSession; |
16
|
|
|
use Phalcon\Mvc\Model\Behavior\Timestampable; |
17
|
|
|
use Phalcon\Security; |
18
|
|
|
use Phalcon\Validation\Validator\Date; |
19
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
20
|
|
|
use Phalcon\Validation\Validator\Uniqueness; |
21
|
|
|
use Phalcon\Validation\Validator\StringLength\Max; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Session |
25
|
|
|
* |
26
|
|
|
* @property User $User |
27
|
|
|
* @property User $UserAs |
28
|
|
|
* |
29
|
|
|
* @method User getUser($params = null) |
30
|
|
|
* @method User getUserAs($params = null) |
31
|
|
|
* |
32
|
|
|
* @package Zemit\Models |
33
|
|
|
*/ |
34
|
|
|
class Session extends AbstractSession |
35
|
|
|
{ |
36
|
|
|
protected $deleted = self::NO; |
37
|
|
|
|
38
|
|
|
public function initialize() |
39
|
|
|
{ |
40
|
|
|
parent::initialize(); |
41
|
|
|
|
42
|
|
|
/** @var Security $security */ |
43
|
|
|
$security = $this->getDI()->get('security'); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->belongsTo('asUserId', User::class, 'id', ['alias' => 'UserAsEntity']); |
46
|
|
|
|
47
|
|
|
// refresh date |
48
|
|
|
$this->addBehavior(new Timestampable([ |
49
|
|
|
'beforeValidation' => [ |
50
|
|
|
'field' => 'date', |
51
|
|
|
'format' => 'Y-m-d H:i:s', |
52
|
|
|
], |
53
|
|
|
])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function validation() |
57
|
|
|
{ |
58
|
|
|
$validator = $this->genericValidation(); |
59
|
|
|
|
60
|
|
|
$validator->add('key', new PresenceOf(['message' => $this->_('required')])); |
61
|
|
|
$validator->add('key', new Uniqueness(['message' => $this->_('not-unique')])); |
62
|
|
|
$validator->add('key', new Max(['max' => 60, 'message' => $this->_('length-exceeded')])); |
63
|
|
|
|
64
|
|
|
$validator->add('token', new PresenceOf(['message' => $this->_('required')])); |
65
|
|
|
$validator->add('token', new Max(['max' => 128, 'message' => $this->_('length-exceeded')])); |
66
|
|
|
|
67
|
|
|
$validator->add('date', new PresenceOf(['message' => $this->_('required')])); |
68
|
|
|
$validator->add('date', new Date(['format' => 'Y-m-d H:i:s', 'message' => $this->_('date-not-valid')])); |
69
|
|
|
|
70
|
|
|
return $this->validate($validator); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function save(): bool { |
74
|
|
|
return self::_isSessionAdapter()? $this->_saveIntoSession() : parent::save(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function update(): bool { |
78
|
|
|
return self::_isSessionAdapter()? $this->_saveIntoSession() : parent::update(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function create(): bool { |
82
|
|
|
return self::_isSessionAdapter()? $this->_saveIntoSession() : parent::create(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function delete(): bool { |
86
|
|
|
return self::_isSessionAdapter()? $this->_removeFromSession() : parent::delete(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
static public function findFirstByKey($key) { |
|
|
|
|
90
|
|
|
$session = Di::getDefault()->get('session'); |
91
|
|
|
if (self::_isSessionAdapter()) { |
92
|
|
|
if ($session->has('zemit-session-' . $key)) { |
93
|
|
|
$sessionStore = $session->get('zemit-session-' . $key); |
94
|
|
|
$model = new self(); |
95
|
|
|
$model->setKey($sessionStore['key']); |
96
|
|
|
$model->setToken($sessionStore['token']); |
97
|
|
|
$model->setDate($sessionStore['date'] ?? date('Y-m-d H:i:s')); |
98
|
|
|
$model->setUserId($sessionStore['userId'] ?? null); |
99
|
|
|
$model->setAsUserId($sessionStore['asUserId'] ?? null); |
100
|
|
|
return $model; |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
return parent::findFirstByKey($key); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check if we should save into the database or the session |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
static public function _isSessionAdapter() { |
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Save the key into the session |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function _saveIntoSession() { |
120
|
|
|
/** @var Manager $session */ |
121
|
|
|
$session = $this->getDI()->get('session'); |
122
|
|
|
$session->set('zemit-session-' . $this->getKey(), $this->toArray()); |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Save the key into the session |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function _removeFromSession() { |
131
|
|
|
/** @var Manager $session */ |
132
|
|
|
$session = $this->getDI()->get('session'); |
133
|
|
|
$session->remove('zemit-session-' . $this->getKey()); |
134
|
|
|
return true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|