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\AbstractUser; |
14
|
|
|
use Phalcon\Security; |
15
|
|
|
use Phalcon\Validation\Validator\Between; |
16
|
|
|
use Phalcon\Validation\Validator\Confirmation; |
17
|
|
|
use Phalcon\Validation\Validator\Date; |
18
|
|
|
use Phalcon\Validation\Validator\Email; |
|
|
|
|
19
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
20
|
|
|
use Phalcon\Validation\Validator\StringLength\Max; |
21
|
|
|
use Phalcon\Validation\Validator\Uniqueness; |
22
|
|
|
use Phalcon\Validation\Validator\InclusionIn; |
23
|
|
|
|
24
|
|
|
use Zemit\Identity; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class User |
28
|
|
|
* |
29
|
|
|
* @property UserGroup[] $GroupNode |
30
|
|
|
* @property Group[] $GroupList |
31
|
|
|
* @property UserRole[] $RoleNode |
32
|
|
|
* @property Role[] $RoleList |
33
|
|
|
* @property UserType[] $TypeNode |
34
|
|
|
* @property Type[] $TypeList |
35
|
|
|
* @property File[] $FileList |
36
|
|
|
* @property Identity $Identity |
37
|
|
|
* |
38
|
|
|
* @method UserGroup[] getGroupNode($params = null) |
39
|
|
|
* @method Group[] getGroupList($params = null) |
40
|
|
|
* @method UserRole[] getRoleNode($params = null) |
41
|
|
|
* @method Role[] getRoleList($params = null) |
42
|
|
|
* @method UserType[] getTypeNode($params = null) |
43
|
|
|
* @method Type[] getTypeList($params = null) |
44
|
|
|
* @method File[] getFileList($params = null) |
45
|
|
|
* |
46
|
|
|
* @package Zemit\Models |
47
|
|
|
*/ |
48
|
|
|
class User extends AbstractUser |
49
|
|
|
{ |
50
|
|
|
protected $language = self::LANG_FR; |
51
|
|
|
protected $deleted = self::NO; |
52
|
|
|
|
53
|
|
|
public function initialize() |
54
|
|
|
{ |
55
|
|
|
parent::initialize(); |
56
|
|
|
|
57
|
|
|
$this->hasMany('id', File::Class, 'userId', ['alias' => 'FileList']); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->hasMany('id', UserGroup::class, 'userId', ['alias' => 'GroupNode']); |
60
|
|
|
$this->hasManyToMany('id', UserGroup::Class, 'userId', |
|
|
|
|
61
|
|
|
'groupId', Group::class, 'id', ['alias' => 'GroupList']); |
62
|
|
|
|
63
|
|
|
$this->hasMany('id', UserRole::class, 'userId', ['alias' => 'RoleNode']); |
64
|
|
|
$this->hasManyToMany('id', UserRole::Class, 'userId', |
|
|
|
|
65
|
|
|
'roleId', Role::class, 'id', ['alias' => 'RoleList']); |
66
|
|
|
|
67
|
|
|
$this->hasMany('id', UserType::class, 'userId', ['alias' => 'TypeNode']); |
68
|
|
|
$this->hasManyToMany('id', UserType::Class, 'userId', |
|
|
|
|
69
|
|
|
'typeId', Type::class, 'id', ['alias' => 'TypeList']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function validation() |
73
|
|
|
{ |
74
|
|
|
$validator = $this->genericValidation(); |
75
|
|
|
|
76
|
|
|
$validator->add('username', new PresenceOf(['message' => $this->_('username') . ': ' . $this->_('required')])); |
77
|
|
|
$validator->add('email', new Max(['max' => 120, 'message' => $this->_('email') . ': ' . $this->_('length-exceeded')])); |
78
|
|
|
|
79
|
|
|
$validator->add('firstName', new PresenceOf(['message' => $this->_('first-name') . ': ' . $this->_('required')])); |
80
|
|
|
$validator->add('firstName', new Max(['max' => 60, 'message' => $this->_('first-name') . ': ' . $this->_('length-exceeded')])); |
81
|
|
|
|
82
|
|
|
$validator->add('lastName', new PresenceOf(['message' => $this->_('last-name') . ': ' . $this->_('required')])); |
83
|
|
|
$validator->add('lastName', new Max(['max' => 60, 'message' => $this->_('last-name') . ': ' . $this->_('length-exceeded')])); |
84
|
|
|
|
85
|
|
|
$validator->add('email', new PresenceOf(['message' => $this->_('email') . ': ' . $this->_('required')])); |
86
|
|
|
$validator->add('email', new Email(['message' => $this->_('email') . ': ' . 'email-not-valid'])); |
87
|
|
|
$validator->add('email', new Uniqueness(['message' => $this->_('email') . ': ' . $this->_('not-unique')])); |
88
|
|
|
$validator->add('email', new Max(['max' => 191, 'message' => $this->_('email') . ': ' . $this->_('length-exceeded')])); |
89
|
|
|
|
90
|
|
|
$validator->add('gender', new Between(["minimum" => 0, "maximum" => 1, 'message' => $this->_('boolean-not-valid')])); |
91
|
|
|
|
92
|
|
|
if ($this->getDob()) { |
93
|
|
|
$validator->add('dob', new Date(['format' => self::DATE_FORMAT, 'message' => $this->_('date-not-valid')])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$validator->add(['phone', 'phone2', 'cellphone', 'fax'], new Max(['max' => 60, 'message' => $this->_('length-exceeded')])); |
97
|
|
|
|
98
|
|
|
$validator->add('token', new Max(['max' => 120, 'message' => $this->_('length-exceeded')])); |
99
|
|
|
|
100
|
|
|
// Password |
101
|
|
|
if (!$this->hasSnapshotData() || $this->hasChanged('password')) { |
102
|
|
|
$validator->add(['password', 'passwordConfirm'], new Max(['max' => 255, 'message' => 'Le mot de passe ne doit pas dépasser :max caractères'])); |
103
|
|
|
$validator->add('passwordConfirm', new Confirmation([ |
104
|
|
|
'message' => 'La mot de passe et la confirmation doivent être identique', |
105
|
|
|
'with' => 'password', |
106
|
|
|
])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->validate($validator); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Prepare save after validation |
114
|
|
|
*/ |
115
|
|
|
public function beforeSave() |
116
|
|
|
{ |
117
|
|
|
$this->preparePassword(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Salt & hash the passwordConfirm field into password |
122
|
|
|
*/ |
123
|
|
|
public function preparePassword(): void |
124
|
|
|
{ |
125
|
|
|
$password = $this->getPassword(); |
126
|
|
|
$passwordConfirm = $this->getPasswordConfirm(); |
127
|
|
|
if (!empty($passwordConfirm) && $password === $passwordConfirm) { |
128
|
|
|
$this->setPassword($this->hash($passwordConfirm)); |
129
|
|
|
} |
130
|
|
|
$this->setPasswordConfirm(null); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string|null $password |
135
|
|
|
* |
136
|
|
|
* @return bool If the hash is valid or not |
137
|
|
|
*/ |
138
|
|
|
public function checkPassword(string $password = null): bool |
139
|
|
|
{ |
140
|
|
|
return $password ? $this->checkHash($this->getPassword(), $password) : false; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: