From 1e72f977e5b653c856933c33f839af8b77f057e2 Mon Sep 17 00:00:00 2001 From: Uibar Ion-Cristian Date: Sat, 20 Sep 2014 17:27:47 +0300 Subject: [PATCH 1/2] Added ability to auto-hash elements. If the attribute name ends with _hash it will be automaticaly hashed. It is useful for passwords when beeing inserted in DB. A different implementation might be possible but this one suits me best since my fields are like: "password_hash", "token_hash" and so on. --- src/Way/Database/Model.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Way/Database/Model.php b/src/Way/Database/Model.php index 41da3c9..e147128 100644 --- a/src/Way/Database/Model.php +++ b/src/Way/Database/Model.php @@ -2,6 +2,7 @@ use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Validation\Validator; +use Illuminate\Hashing\BcryptHasher as Hash; class Model extends Eloquent { @@ -31,13 +32,14 @@ class Model extends Eloquent { * * @var Illuminate\Validation\Validators */ - protected $validator; + protected $validator, $hasher; - public function __construct(array $attributes = array(), Validator $validator = null) + public function __construct(array $attributes = array(), Validator $validator = null, Hash $hasher = null) { parent::__construct($attributes); $this->validator = $validator ?: \App::make('validator'); + $this->hasher = $hasher ?: \App::make('hash'); } /** @@ -49,7 +51,9 @@ protected static function boot() static::saving(function($model) { - return $model->validate(); + // Returning true would prevent other event listeners from firing + + return $model->validate() ? null : false; }); } @@ -62,6 +66,11 @@ public function validate() if ($v->passes()) { + foreach ($this->attributes as $key => $value) { + if ($this->endsWith($key, '_hash')) + $this->attributes[$key] = $this->hasher->make($value); + } + return true; } @@ -96,4 +105,14 @@ public function hasErrors() return ! empty($this->errors); } + protected static function endsWith($haystack, $needle) + { + $length = strlen($needle); + if ($length == 0) { + return true; + } + + return (substr($haystack, -$length) === $needle); + } + } From 3fb27776a7d9b899184e841017dae2171aded84b Mon Sep 17 00:00:00 2001 From: Uibar Ion-Cristian Date: Sun, 21 Sep 2014 00:16:58 +0300 Subject: [PATCH 2/2] Regex insted of function and removed pull. Updated to use regex insted of function and removed the pull of another user so you can pull his fix insted of mine for events not triggering. --- src/Way/Database/Model.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Way/Database/Model.php b/src/Way/Database/Model.php index e147128..a1ca8f0 100644 --- a/src/Way/Database/Model.php +++ b/src/Way/Database/Model.php @@ -51,9 +51,7 @@ protected static function boot() static::saving(function($model) { - // Returning true would prevent other event listeners from firing - - return $model->validate() ? null : false; + return $model->validate(); }); } @@ -66,8 +64,9 @@ public function validate() if ($v->passes()) { + // Hashes the attribute value if name ends with _hash foreach ($this->attributes as $key => $value) { - if ($this->endsWith($key, '_hash')) + if (preg_match("/.*(_hash)$/", $key)) $this->attributes[$key] = $this->hasher->make($value); } @@ -105,14 +104,4 @@ public function hasErrors() return ! empty($this->errors); } - protected static function endsWith($haystack, $needle) - { - $length = strlen($needle); - if ($length == 0) { - return true; - } - - return (substr($haystack, -$length) === $needle); - } - }