forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.php
More file actions
112 lines (102 loc) · 2.94 KB
/
Copy pathUser.php
File metadata and controls
112 lines (102 loc) · 2.94 KB
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
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Users\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;
/**
* User Entity.
*/
class User extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* @var array
*/
protected $_accessible = [
'username' => true,
'email' => true,
'password' => true,
'confirm_password' => true,
'first_name' => true,
'last_name' => true,
'token' => true,
'token_expires' => true,
'api_token' => true,
'activation_date' => true,
//tos is a boolean, coming from the "accept the terms of service" checkbox but it is not stored onto database
'tos' => true,
'tos_date' => true,
'active' => true,
'social_accounts' => true,
'current_password' => true
];
/**
* @param string $password password that will be set.
* @return bool|string
*/
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
/**
* @param string $password password that will be confirm.
* @return bool|string
*/
protected function _setConfirmPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
/**
* Checks if a password is correctly hashed
* @param string $password password that will be check.
* @param string $hashedPassword hash used to check password.
* @return bool
*/
public function checkPassword($password, $hashedPassword)
{
return (new DefaultPasswordHasher)->check($password, $hashedPassword);
}
/**
* Returns if the token has already expired
*
* @return bool
*/
public function tokenExpired()
{
return empty($this->token_expires) || strtotime($this->token_expires) < strtotime("now");
}
/**
* Getter for user avatar
* @return string|null avatar
*/
protected function _getAvatar()
{
$avatar = null;
if (!empty($this->_properties['social_accounts'][0])) {
$avatar = $this->_properties['social_accounts'][0]['avatar'];
}
return $avatar;
}
/**
* Generate token_expires and token in a user
* @param string $tokenExpiration new token_expires user.
*
* @return void
*/
public function updateToken($tokenExpiration)
{
$expires = new DateTime();
$expires->modify("+ $tokenExpiration secs");
$this->token_expires = $expires;
$this->token = str_replace('-', '', Text::uuid());
}
}