From fcf3b659197413766665be728537cebf0f70af0a Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Wed, 16 Nov 2016 22:09:21 -0500 Subject: [PATCH 1/3] Offer an "Attribute Casting" of encrypt This would allow the user to easily encrypt and decrypt a field by setting it to that `cast` For example ``` protected $casts = [ 'secret' => 'encrypt', 'data' => 'array' ]; ``` --- src/Illuminate/Database/Eloquent/Model.php | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 0b6681cc6564..a0e724dd3dd2 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2793,6 +2793,16 @@ protected function isJsonCastable($key) return $this->hasCast($key, ['array', 'json', 'object', 'collection']); } + /** + * Determine whether a value should be encrypted. + * + * @param string $key + * @return bool + */ + protected function isEncryptCastable($key) { + return $this->hasCast($key, ['encrypt']); + } + /** * Get the type of cast for a model attribute. * @@ -2842,6 +2852,8 @@ protected function castAttribute($key, $value) return $this->asDateTime($value); case 'timestamp': return $this->asTimeStamp($value); + case 'encrypt': + return decrypt($value); default: return $value; } @@ -2872,6 +2884,10 @@ public function setAttribute($key, $value) $value = $this->fromDateTime($value); } + if($this->isEncryptCastable($key) && ! is_null($value)) { + $value = $this->asEncrypted($value); + } + if ($this->isJsonCastable($key) && ! is_null($value)) { $value = $this->asJson($value); } @@ -3045,7 +3061,7 @@ protected function asJson($value) { return json_encode($value); } - + /** * Decode the given JSON back into an array or object. * @@ -3058,6 +3074,17 @@ public function fromJson($value, $asObject = false) return json_decode($value, ! $asObject); } + /** + * Encrypt to given value + * + * @param mixed $value + * @return string + */ + protected function asEncrypted($value) + { + return encrypt($value); + } + /** * Clone the model into a new, non-existing instance. * From d23a6b3a714d6d258ca609cb723a8d2559926a0f Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Wed, 16 Nov 2016 22:13:30 -0500 Subject: [PATCH 2/3] fix formatting --- src/Illuminate/Database/Eloquent/Model.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index a0e724dd3dd2..0b272751b064 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2799,7 +2799,8 @@ protected function isJsonCastable($key) * @param string $key * @return bool */ - protected function isEncryptCastable($key) { + protected function isEncryptCastable($key) + { return $this->hasCast($key, ['encrypt']); } @@ -2853,7 +2854,7 @@ protected function castAttribute($key, $value) case 'timestamp': return $this->asTimeStamp($value); case 'encrypt': - return decrypt($value); + return decrypt($value); default: return $value; } @@ -2884,10 +2885,10 @@ public function setAttribute($key, $value) $value = $this->fromDateTime($value); } - if($this->isEncryptCastable($key) && ! is_null($value)) { + if ($this->isEncryptCastable($key) && ! is_null($value)) { $value = $this->asEncrypted($value); } - + if ($this->isJsonCastable($key) && ! is_null($value)) { $value = $this->asJson($value); } @@ -3061,7 +3062,7 @@ protected function asJson($value) { return json_encode($value); } - + /** * Decode the given JSON back into an array or object. * @@ -3075,7 +3076,7 @@ public function fromJson($value, $asObject = false) } /** - * Encrypt to given value + * Encrypt to given value. * * @param mixed $value * @return string @@ -3084,7 +3085,7 @@ protected function asEncrypted($value) { return encrypt($value); } - + /** * Clone the model into a new, non-existing instance. * From 278f1363ca64834648df4a65fd29550697dd59c2 Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Wed, 16 Nov 2016 22:14:35 -0500 Subject: [PATCH 3/3] more formatting --- src/Illuminate/Database/Eloquent/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 0b272751b064..a39fc0eb09a6 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2799,11 +2799,11 @@ protected function isJsonCastable($key) * @param string $key * @return bool */ - protected function isEncryptCastable($key) + protected function isEncryptCastable($key) { return $this->hasCast($key, ['encrypt']); } - + /** * Get the type of cast for a model attribute. *