1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Community |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Community |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Community". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Community\Model\Entity; |
17
|
|
|
|
18
|
|
|
use Cake\Routing\Router; |
19
|
|
|
use Cake\I18n\FrozenTime; |
20
|
|
|
use Core\ORM\Entity\Entity; |
21
|
|
|
use Cake\Auth\DefaultPasswordHasher; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class User |
25
|
|
|
* |
26
|
|
|
* @package Community\Model\Entity |
27
|
|
|
* |
28
|
|
|
* @property int $id |
29
|
|
|
* @property int $group_id |
30
|
|
|
* @property Group $group |
31
|
|
|
* @property string $login |
32
|
|
|
* @property string $name |
33
|
|
|
* @property string $slug |
34
|
|
|
* @property string $email |
35
|
|
|
* @property string $password |
36
|
|
|
* @property string $token |
37
|
|
|
* @property bool $status |
38
|
|
|
* @property FrozenTime $last_login |
39
|
|
|
* @property FrozenTime $last_action |
40
|
|
|
* @property FrozenTime $modified |
41
|
|
|
* @property FrozenTime $created |
42
|
|
|
* @property string activation_url Virtual field. |
43
|
|
|
*/ |
44
|
|
|
class User extends Entity |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List of computed or virtual fields that **should** be included in JSON or array |
49
|
|
|
* representations of this Entity. If a field is present in both _hidden and _virtual |
50
|
|
|
* the field will **not** be in the array/json versions of the entity. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $_virtual = [ |
55
|
|
|
'activation_url', |
56
|
|
|
'setup_password_url' |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get current user edit profile url. |
61
|
|
|
* |
62
|
|
|
* @param bool $backend Backend or frontend point. |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getEditUrl($backend = false) |
66
|
|
|
{ |
67
|
|
|
$url = [ |
68
|
|
|
'action' => 'edit', |
69
|
|
|
'controller' => 'Users', |
70
|
|
|
'plugin' => 'Community' |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
if ($backend) { |
74
|
|
|
$url['prefix'] = 'admin'; |
75
|
|
|
$url[] = $this->id; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return Router::url($url); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set virtual field activation_url. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
protected function _getActivationUrl() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return Router::url([ |
89
|
|
|
'controller' => 'Users', |
90
|
|
|
'action' => 'activate', |
91
|
|
|
'plugin' => 'Community', |
92
|
|
|
$this->id, |
93
|
|
|
$this->token |
94
|
|
|
], true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* set virtual field setup_password_url |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
protected function _getSetupPasswordUrl() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return Router::url([ |
105
|
|
|
'controller' => 'Users', |
106
|
|
|
'plugin' => 'Community', |
107
|
|
|
'action' => 'setupPassword', |
108
|
|
|
$this->id, |
109
|
|
|
$this->token |
110
|
|
|
], true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Setup password. |
115
|
|
|
* |
116
|
|
|
* @param string $password User password for create hashed. |
117
|
|
|
* @return null|string |
118
|
|
|
*/ |
119
|
|
|
protected function _setPassword($password) |
120
|
|
|
{ |
121
|
|
|
if ($password !== null) { |
122
|
|
|
return (new DefaultPasswordHasher())->hash($password); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.