|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Model; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Finder\UserFinder; |
|
6
|
|
|
use Respect\Validation\Validator; |
|
7
|
|
|
use Ronanchilvers\Orm\Model; |
|
8
|
|
|
use Ronanchilvers\Orm\Traits\HasValidationTrait; |
|
9
|
|
|
use Ronanchilvers\Utility\Str; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Model representing a project |
|
13
|
|
|
* |
|
14
|
|
|
* @property int id |
|
15
|
|
|
* @property string name |
|
16
|
|
|
* @property string email |
|
17
|
|
|
* @property string password |
|
18
|
|
|
* @property string status |
|
19
|
|
|
* @property null|string preferences |
|
20
|
|
|
* @property null|\Carbon\Carbon created |
|
21
|
|
|
* @property null|\Carbon\Carbon updated |
|
22
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class User extends Model |
|
25
|
|
|
{ |
|
26
|
|
|
const STATUS_ACTIVE = 'active'; |
|
27
|
|
|
const STATUS_INACTIVE = 'inactive'; |
|
28
|
|
|
|
|
29
|
|
|
use HasValidationTrait; |
|
30
|
|
|
|
|
31
|
|
|
static protected $finder = UserFinder::class; |
|
32
|
|
|
static protected $columnPrefix = 'user'; |
|
33
|
|
|
|
|
34
|
|
|
protected $data = [ |
|
35
|
|
|
'user_status' => 'active', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function boot() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->addType('array', 'preferences'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function setupValidation() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->registerRules([ |
|
52
|
|
|
'name' => Validator::notEmpty(), |
|
53
|
|
|
'email' => Validator::notEmpty()->email(), |
|
54
|
|
|
'password' => Validator::notEmpty(), |
|
55
|
|
|
'status' => Validator::notEmpty()->in([static::STATUS_INACTIVE, static::STATUS_ACTIVE]), |
|
56
|
|
|
]); |
|
57
|
|
|
$this->registerRules([ |
|
58
|
|
|
'password' => Validator::notEmpty(), |
|
59
|
|
|
], 'security'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get a preference by key |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $key |
|
66
|
|
|
* @param mixed $default |
|
67
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
68
|
|
|
*/ |
|
69
|
|
|
public function preference($key, $default = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$preferences = $this->preferences; |
|
72
|
|
|
if (is_null($preferences)) { |
|
73
|
|
|
return $default; |
|
74
|
|
|
} |
|
75
|
|
|
if (!isset($preferences[$key])) { |
|
76
|
|
|
return $default; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $preferences[$key]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set a preference value for the user |
|
84
|
|
|
* |
|
85
|
|
|
* @param $key |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setPreference($key, $value) |
|
90
|
|
|
{ |
|
91
|
|
|
$preferences = $this->preferences; |
|
92
|
|
|
$preferences[$key] = $value; |
|
93
|
|
|
$this->preferences = $preferences; |
|
94
|
|
|
|
|
95
|
|
|
return $this->save(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Verify a password against this user |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $password |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
104
|
|
|
*/ |
|
105
|
|
|
public function verify($password) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->isLoaded()) { |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return password_verify($password, $this->password); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Verify and set a new password |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $old |
|
118
|
|
|
* @param string $new |
|
119
|
|
|
* @param string $confirm |
|
120
|
|
|
* @return bool |
|
121
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setNewPassword(string $old, string $new, string $confirm) |
|
124
|
|
|
{ |
|
125
|
|
|
foreach (['old', 'new', 'confirm'] as $var) { |
|
126
|
|
|
$$var = trim($$var); |
|
127
|
|
|
} |
|
128
|
|
|
if (!$this->verify($old)) { |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (empty($new)) { |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($new !== $confirm) { |
|
137
|
|
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->password = password_hash($new, PASSWORD_DEFAULT); |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|