Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit ac5eae4

Browse files
committed
Improving autohasher
! autohasher wouldn't work when outside of Laravel + autohashes by default any 'password' field, when autohash is enabled
1 parent 2b2989c commit ac5eae4

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"illuminate/support": "~5.1",
2424
"illuminate/database": "~5.1",
2525
"illuminate/validation": "~5.1",
26-
"illuminate/events": "~5.1"
26+
"illuminate/events": "~5.1",
27+
"illuminate/hashing": "~5.1"
2728
},
2829
"autoload": {
2930
"psr-4": {

src/Ardent/Ardent.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Capsule\Manager as DatabaseCapsule;
66
use Illuminate\Database\Eloquent\Relations\BelongsTo;
77
use Illuminate\Events\Dispatcher;
8+
use Illuminate\Hashing\BcryptHasher;
89
use Illuminate\Support\MessageBag;
910
use Illuminate\Support\Facades\Input;
1011
use Illuminate\Support\Facades\Hash;
@@ -114,7 +115,7 @@ abstract class Ardent extends Model {
114115
*
115116
* @var array
116117
*/
117-
public static $passwordAttributes = array();
118+
public static $passwordAttributes = array('password');
118119

119120
/**
120121
* If set to true, the model will automatically replace all plain-text passwords
@@ -125,11 +126,11 @@ abstract class Ardent extends Model {
125126
public $autoHashPasswordAttributes = false;
126127

127128
/**
128-
* If set to true will try to instantiate the validator as if it was outside Laravel.
129+
* If set to true will try to instantiate other components as if it was outside Laravel.
129130
*
130131
* @var bool
131132
*/
132-
protected static $externalValidator = false;
133+
protected static $external = false;
133134

134135
/**
135136
* A Validation Factory instance, to be used by standalone Ardent instances with the Translator.
@@ -138,6 +139,14 @@ abstract class Ardent extends Model {
138139
*/
139140
protected static $validationFactory;
140141

142+
/**
143+
* An instance of a Hasher object, to be used by standalone Ardent instances. Will be null if not external.
144+
*
145+
* @var \Illuminate\Contracts\Hashing\Hasher
146+
* @see LaravelArdent\Ardent\Ardent::configureAsExternal()
147+
*/
148+
public static $hasher;
149+
141150
/**
142151
* Can be used to ease declaration of relationships in Ardent models.
143152
* Follows closely the behavior of the relation methods used by Eloquent, but packing them into an indexed array
@@ -505,9 +514,11 @@ public static function configureAsExternal(array $connection, $lang = 'en') {
505514
dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.$lang.
506515
DIRECTORY_SEPARATOR.'validation.php', $lang);
507516

508-
self::$externalValidator = true;
517+
self::$external = true;
509518
self::$validationFactory = new ValidationFactory($translator);
510519
self::$validationFactory->setPresenceVerifier(new DatabasePresenceVerifier($db->getDatabaseManager()));
520+
521+
self::$hasher = new BcryptHasher();
511522
}
512523

513524
/**
@@ -522,11 +533,9 @@ public static function configureAsExternal(array $connection, $lang = 'en') {
522533
* @see Ardent::$externalValidator
523534
*/
524535
protected static function makeValidator($data, $rules, $customMessages, $customAttributes) {
525-
if (self::$externalValidator) {
526-
return self::$validationFactory->make($data, $rules, $customMessages, $customAttributes);
527-
} else {
528-
return Validator::make($data, $rules, $customMessages, $customAttributes);
529-
}
536+
return self::$external?
537+
self::$validationFactory->make($data, $rules, $customMessages, $customAttributes) :
538+
Validator::make($data, $rules, $customMessages, $customAttributes);
530539
}
531540

532541
/**
@@ -770,6 +779,15 @@ public function errors() {
770779
return $this->validationErrors;
771780
}
772781

782+
/**
783+
* Hashes the password, working without the Hash facade if this is an instance outside of Laravel.
784+
* @param $value
785+
* @return string
786+
*/
787+
protected function hashPassword($value) {
788+
return self::$external? self::$hasher->make($value) : Hash::make($value);
789+
}
790+
773791
/**
774792
* Automatically replaces all plain-text password attributes (listed in $passwordAttributes)
775793
* with hash checksum.
@@ -789,7 +807,7 @@ protected function hashPasswordAttributes(array $attributes = array(), array $pa
789807

790808
if (in_array($key, $passwordAttributes) && !is_null($value)) {
791809
if ($value != $this->getOriginal($key)) {
792-
$result[$key] = Hash::make($value);
810+
$result[$key] = $this->hashPassword($value);
793811
}
794812
} else {
795813
$result[$key] = $value;

0 commit comments

Comments
 (0)