Skip to content

Commit

Permalink
fix: handle updating and creating properly
Browse files Browse the repository at this point in the history
  • Loading branch information
beliven-fabrizio-gortani committed Dec 12, 2024
1 parent 7f637c0 commit 368902a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/PasswordHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
10 changes: 9 additions & 1 deletion src/Traits/HasPasswordHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected static function bootHasPasswordHistory(): void
self::handleCreating($model);
});

static::updated(function ($model) {
static::updating(function ($model) {
self::handleUpdating($model);
});
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
}

0 comments on commit 368902a

Please sign in to comment.