From 368902ae10d6cf3d30ba4e9318b610db0fdf7d6e Mon Sep 17 00:00:00 2001 From: Fabrizio Gortani Date: Thu, 12 Dec 2024 11:35:17 +0100 Subject: [PATCH] fix: handle updating and creating properly --- src/PasswordHistory.php | 4 +--- src/Traits/HasPasswordHistory.php | 10 +++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/PasswordHistory.php b/src/PasswordHistory.php index df5ec53..bf639d7 100755 --- a/src/PasswordHistory.php +++ b/src/PasswordHistory.php @@ -25,9 +25,7 @@ private function removeModelOldestHash(Model $model): void public function hasPasswordInHistory(Model $model, string $newPassword): bool { - $listOfPasswords = PasswordHash::query() - ->whereHasMorph('model', $model::class) - ->get(); + $listOfPasswords = PasswordHash::byModel($model)->get(); foreach ($listOfPasswords as $password) { $hash = $password->getAttribute('hash'); diff --git a/src/Traits/HasPasswordHistory.php b/src/Traits/HasPasswordHistory.php index 9ca1aad..0247602 100644 --- a/src/Traits/HasPasswordHistory.php +++ b/src/Traits/HasPasswordHistory.php @@ -20,7 +20,7 @@ protected static function bootHasPasswordHistory(): void self::handleCreating($model); }); - static::updated(function ($model) { + static::updating(function ($model) { self::handleUpdating($model); }); } @@ -64,6 +64,10 @@ protected function savePasswordInHistory(string $newPassword, bool $explicit = t private static function handleCreating(Model $model): void { + if (is_null(self::$plain_text_password)) { + return; + } + $model->savePasswordInHistory(self::$plain_text_password, false); } @@ -73,6 +77,10 @@ private static function handleUpdating(Model $model): void return; } + if (is_null(self::$plain_text_password)) { + return; + } + $model->savePasswordInHistory(self::$plain_text_password, false); } }