-
Notifications
You must be signed in to change notification settings - Fork 25
/
HashingModelTrait.php
255 lines (226 loc) · 5.53 KB
/
HashingModelTrait.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace Esensi\Model\Traits;
use Esensi\Model\Observers\HashingModelObserver;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Facades\Hash;
/**
* Trait that implements the Hashing Model Interface.
*
*/
trait HashingModelTrait
{
/**
* Whether the model is hashing or not.
*
* @var bool
*/
protected $hashing = true;
/**
* The Hasher to use for hashing.
*
* @var Illuminate\Contracts\Hashing\Hasher
*/
protected $hasher;
/**
* Boot the trait's observers.
*/
public static function bootHashingModelTrait()
{
static::observe(new HashingModelObserver());
}
/**
* Get the hashable attributes.
*
* @return array
*/
public function getHashable()
{
return $this->hashable ?: [];
}
/**
* Set the hashable attributes.
*
* @param array $attributes to hash
*/
public function setHashable(array $attributes)
{
$this->hashable = $attributes;
}
/**
* Add an attribute to the hashable array.
*
* @example addHashable( string $attribute, ... )
* @param string $attribute to hash
*/
public function addHashable($attribute)
{
$this->mergeHashable(func_get_args());
}
/**
* Remove an attribute from the hashable array.
*
* @example addHashable( string $attribute, ... )
* @param string $attribute to hash
*/
public function removeHashable($attribute)
{
$this->hashable = array_diff($this->hashable, func_get_args());
}
/**
* Merge an array of attributes with the hashable array.
*
* @param array $attributes to hash
*/
public function mergeHashable(array $attributes)
{
$this->hashable = array_merge($this->hashable, $attributes);
}
/**
* Returns whether or not the model will hash
* attributes before saving.
*
* @return bool
*/
public function getHashing()
{
return $this->hashing;
}
/**
* Set whether or not the model will hash attributes
* before saving.
*
* @param bool
*/
public function setHashing($value)
{
$this->hashing = (bool) $value;
}
/**
* Set the Hasher to use for hashing.
*
* @return Illuminate\Contracts\Hashing\Hasher
*/
public function getHasher()
{
return $this->hasher ?: Hash::getFacadeRoot();
}
/**
* Set the Hasher to use for hashing.
*
* @param Illuminate\Contracts\Hashing\Hasher $hasher
*/
public function setHasher(Hasher $hasher)
{
$this->hasher = $hasher;
}
/**
* Returns whether the attribute is hashable.
*
* @param string $attribute name
* @return bool
*/
public function isHashable($attribute)
{
return $this->getHashing()
&& in_array($attribute, $this->getHashable());
}
/**
* Returns whether the attribute is hashed.
*
* @param string $attribute name
* @return bool
*/
public function isHashed($attribute)
{
if (! array_key_exists($attribute, $this->attributes)) {
return false;
}
$info = password_get_info($this->attributes[$attribute]);
return (bool) ($info['algo'] !== 0);
}
/**
* Hash attributes that should be hashed.
*/
public function hashAttributes()
{
foreach ($this->getHashable() as $attribute) {
$this->setHashingAttribute($attribute, $this->getAttribute($attribute));
}
}
/**
* Return a hashed string for the value.
*
* @param string $value
* @return string
*/
public function hash($value)
{
return $this->getHasher()
->make($value);
}
/**
* Return whether a plain value matches a hashed value.
*
* @param string $value
* @param string $hash to compare to
*
* @return bool
*/
public function checkHash($value, $hash)
{
return $this->getHasher()
->check($value, $hash);
}
/**
* Set a hashed value for a hashable attribute.
*
* @param string $attribute name
* @param string $value to hash
*/
public function setHashingAttribute($attribute, $value)
{
// Set the value which is presumably plain text
$this->attributes[$attribute] = $value;
// Do the hashing if it needs it
if (! empty($value) && ($this->isDirty($attribute) || ! $this->isHashed($attribute))) {
$this->attributes[$attribute] = $this->hash($value);
}
}
/**
* Save with hashing even if hashing is disabled.
*
* @return bool
*/
public function saveWithHashing()
{
// Turn hashing on
return $this->setHashingAndSave(true);
}
/**
* Save without hashing even if hashing is enabled.
*
* @return bool
*/
public function saveWithoutHashing()
{
// Turn hashing off
return $this->setHashingAndSave(false);
}
/**
* Set hashing state and then save and then reset it.
*
* @param bool $hash
* @return bool
*/
protected function setHashingAndSave($hash)
{
// Set hashing state
$hashing = $this->getHashing();
$this->setHashing($hash);
// Save the model
$result = $this->save();
// Reset hashing back to it's previous state
$this->setHashing($hashing);
return $result;
}
}