-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathElocrypt.php
238 lines (219 loc) · 5.91 KB
/
Elocrypt.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
<?php
/**
* Trait Elocrypt.
*/
namespace Delatbabel\Elocrypt;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
/**
* Trait Elocrypt.
*
* Automatically encrypt and decrypt Laravel 5 Eloquent values
*
* ### Example
*
* <code>
* use Delatbabel\Elocrypt\Elocrypt;
*
* class User extends Eloquent {
*
* use Elocrypt;
*
* protected $encrypts = [
* 'address_line_1', 'first_name', 'last_name', 'postcode'
* ];
* }
* </code>
*
* ### Summary of Methods in Illuminate\Database\Eloquent\Model
*
* This surveys the major methods in the Laravel Model class as of
* Laravel v 5.1.12 and checks to see how those models set attributes
* and hence how they are affected by this trait.
*
* * __construct -- calls fill()
* * fill() -- calls setAttribute() which has been overridden.
* * hydrate() -- TBD
* * create() -- calls constructor and hence fill()
* * firstOrCreate -- calls constructor
* * firstOrNew -- calls constructor
* * updateOrCreate -- calls fill()
* * update() -- calls fill()
* * toArray() -- calls attributesToArray()
* * jsonSerialize() -- calls toArray()
* * toJson() -- calls toArray()
* * attributesToArray() -- has been over-ridden here.
* * getAttribute -- calls getAttributeValue()
* * getAttributeValue -- calls getAttributeFromArray()
* * getAttributeFromArray -- calls getArrayableAttributes
* * getArrayableAttributes -- has been over-ridden here.
* * setAttribute -- has been over-ridden here.
* * getAttributes -- has been over-ridden here.
*
* @see Illuminate\Support\Facades\Crypt
* @see Illuminate\Contracts\Encryption\Encrypter
* @see Illuminate\Encryption\Encrypter
* @link http://laravel.com/docs/5.1/eloquent
*/
trait Elocrypt
{
//
// Methods below here are native to the trait.
//
/**
* Get the configuration setting for the prefix used to determine if a string is encrypted.
*
* @return string
*/
protected function getElocryptPrefix()
{
return Config::has('elocrypt.prefix') ? Config::get('elocrypt.prefix') : '__ELOCRYPT__:';
}
/**
* Determine whether an attribute should be encrypted.
*
* @param string $key
*
* @return bool
*/
protected function shouldEncrypt($key)
{
$encrypt = isset($this->encrypts) ? $this->encrypts : $this->encryptable;
return in_array($key, $encrypt);
}
/**
* Determine whether a string has already been encrypted.
*
* @param mixed $value
*
* @return bool
*/
protected function isEncrypted($value)
{
return strpos((string) $value, $this->getElocryptPrefix()) === 0;
}
/**
* Return the encrypted value of an attribute's value.
*
* This has been exposed as a public method because it is of some
* use when searching.
*
* @param string $value
*
* @return string
*/
public function encryptedAttribute($value)
{
return $this->getElocryptPrefix() . Crypt::encrypt($value);
}
/**
* Return the decrypted value of an attribute's encrypted value.
*
* This has been exposed as a public method because it is of some
* use when searching.
*
* @param string $value
*
* @return string
*/
public function decryptedAttribute($value)
{
return Crypt::decrypt(str_replace($this->getElocryptPrefix(), '', $value));
}
/**
* Encrypt a stored attribute.
*
* @param string $key
*
* @return void
*/
protected function doEncryptAttribute($key)
{
if ($this->shouldEncrypt($key) && ! $this->isEncrypted($this->attributes[$key])) {
try {
$this->attributes[$key] = $this->encryptedAttribute($this->attributes[$key]);
} catch (EncryptException $e) {
}
}
}
/**
* Decrypt an attribute if required.
*
* @param string $key
* @param mixed $value
*
* @return mixed
*/
protected function doDecryptAttribute($key, $value)
{
if ($this->shouldEncrypt($key) && $this->isEncrypted($value)) {
try {
return $this->decryptedAttribute($value);
} catch (DecryptException $e) {
}
}
return $value;
}
/**
* Decrypt each attribute in the array as required.
*
* @param array $attributes
*
* @return array
*/
public function doDecryptAttributes($attributes)
{
foreach ($attributes as $key => $value) {
$attributes[$key] = $this->doDecryptAttribute($key, $value);
}
return $attributes;
}
//
// Methods below here override methods within the base Laravel/Illuminate/Eloquent
// model class and may need adjusting for later releases of Laravel.
//
/**
* Set a given attribute on the model.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function setAttribute($key, $value)
{
parent::setAttribute($key, $value);
$this->doEncryptAttribute($key);
}
/**
* Get an attribute from the $attributes array.
*
* @param string $key
*
* @return mixed
*/
protected function getAttributeFromArray($key)
{
return $this->doDecryptAttribute($key, parent::getAttributeFromArray($key));
}
/**
* Get an attribute array of all arrayable attributes.
*
* @return array
*/
protected function getArrayableAttributes()
{
return $this->doDecryptAttributes(parent::getArrayableAttributes());
}
/**
* Get all of the current attributes on the model.
*
* @return array
*/
public function getAttributes()
{
return $this->doDecryptAttributes(parent::getAttributes());
}
}