/
User.php
124 lines (112 loc) · 3.08 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace App\Model\Entity;
use Authentication\IdentityInterface as AuthenticationIdentity;
use Authentication\PasswordHasher\DefaultPasswordHasher;
use Authorization\AuthorizationService;
use Authorization\IdentityInterface as AuthorizationIdentity;
use Cake\ORM\Entity;
/**
* User Entity
*
* @property int $id
* @property string $username
* @property string $first_name
* @property string $last_name
* @property string $password
* @property string $email
* @property string|null $api_key
* @property string|null $api_key_plain
* @property string|null $token
* @property \Cake\I18n\FrozenTime|null $token_expires
* @property bool $is_superuser
* @property string|null $role
* @property bool $active
* @property \Cake\I18n\FrozenTime $created
* @property \Cake\I18n\FrozenTime $modified
* @property \Cake\I18n\FrozenTime|null $deleted
* @property int|null $created_by
* @property int|null $modified_by
*/
class User extends Entity implements AuthorizationIdentity, AuthenticationIdentity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'username' => true,
'first_name' => true,
'last_name' => true,
'password' => true,
'email' => true,
'api_key' => true,
'api_key_plain' => true,
'token' => true,
'token_expires' => true,
'is_superuser' => true,
'role' => true,
'active' => true,
'created' => true,
'modified' => true,
'deleted' => true,
'created_by' => true,
'modified_by' => true
];
/**
* Fields that are excluded from JSON versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password',
'token'
];
protected function _setPassword($value)
{
if (strlen($value)) {
$hasher = new DefaultPasswordHasher();
return $hasher->hash($value);
}
}
/**
* Authentication\IdentityInterface method
*/
public function getIdentifier()
{
return $this->id;
}
/**
* Authentication\IdentityInterface method
*/
public function getOriginalData()
{
return $this;
}
/**
* Authorization\IdentityInterface method
*/
public function can($action, $resource)
{
return $this->authorization->can($this, $action, $resource);
}
/**
* Authorization\IdentityInterface method
*/
public function applyScope($action, $resource)
{
return $this->authorization->applyScope($this, $action, $resource);
}
/**
* Setter to be used by the middleware.
*/
public function setAuthorization(AuthorizationService $service)
{
$this->authorization = $service;
return $this;
}
}