diff --git a/src/app/Facades/PasswordHistoryManager.php b/src/app/Facades/PasswordHistoryManager.php index 09fcf0e..0617c13 100644 --- a/src/app/Facades/PasswordHistoryManager.php +++ b/src/app/Facades/PasswordHistoryManager.php @@ -5,9 +5,11 @@ namespace Pablo\Ae3auth\app\Facades; use Imanghafoori\SmartFacades\Facade; /** - * @method static inHistory(int|string $userId, string $password): bool - * @method static logNewPassword(int|string $userId, string $password): bool - * @method static logPassword($user): bool + * @method inHistory(int|string $userId, string $password): bool + * @method logNewPassword(int|string $userId, string $password): bool + * @method logPassword($user): bool + * @method updateExpireColumn($user): void + * @method makePasswordAvailable($user): void */ class PasswordHistoryManager extends Facade { diff --git a/src/app/Observers/UserObserver.php b/src/app/Observers/UserObserver.php index ca7e3d6..9947337 100644 --- a/src/app/Observers/UserObserver.php +++ b/src/app/Observers/UserObserver.php @@ -3,9 +3,16 @@ namespace Pablo\Ae3auth\app\Observers; use Pablo\Ae3auth\app\Facades\PasswordHistoryManager; +use Pablo\Ae3auth\app\Services\PasswordHistoryService; class UserObserver { + public function __construct( + private readonly PasswordHistoryService $passwordHistoryService + ) + { + } + /** * @param $user * @return void @@ -15,8 +22,8 @@ class UserObserver PasswordHistoryManager::logPassword($user); $passwordCol = config('ae3auth-config.user.password_column'); if ($user->isDirty($passwordCol)) { - $this->updateExpireColumn($user); - $this->makePasswordAvailable($user); + $this->passwordHistoryService->updateExpireColumn($user); + $this->passwordHistoryService->makePasswordAvailable($user); } } @@ -36,25 +43,6 @@ class UserObserver */ public function creating($user): void { - $this->updateExpireColumn($user); - } - - /** - * @param $user - * @return void - */ - private function updateExpireColumn($user): void - { - $expiresAtColumn = config('ae3auth-config.user.expires_password_column_name'); - $user->$expiresAtColumn = now()->addDays(config('ae3auth-config.password_expires_in')); - } - - /** - * @return void - */ - private function makePasswordAvailable($user): void - { - $forceChangePasswordColumn = config('ae3auth-config.user.force_change_column_name'); - $user->$forceChangePasswordColumn = false; + $this->passwordHistoryService->updateExpireColumn($user); } } diff --git a/src/app/Repositories/PasswordHistoryRepository.php b/src/app/Repositories/PasswordHistoryRepository.php index 341024a..f97443c 100644 --- a/src/app/Repositories/PasswordHistoryRepository.php +++ b/src/app/Repositories/PasswordHistoryRepository.php @@ -11,7 +11,7 @@ class PasswordHistoryRepository * @param int|string $userId * @return mixed */ - public function passwords(int|string $userId): mixed + public function getPasswords(int|string $userId): mixed { return PasswordHistory::where('user_id', $userId)->get(); } @@ -35,7 +35,7 @@ class PasswordHistoryRepository * @param $userId * @return mixed */ - public function oldestPassword($userId): mixed + public function findOldestPassword($userId): mixed { return PasswordHistory::where('user_id', $userId)->orderBy('last_used_at', 'asc')->first(); } @@ -44,4 +44,13 @@ class PasswordHistoryRepository { PasswordHistory::destroy($id); } + + /** + * @param int|string $userId + * @return int + */ + public function countPasswords(int|string $userId): int + { + return PasswordHistory::where('user_id', $userId)->count(); + } } diff --git a/src/app/Services/PasswordHistoryService.php b/src/app/Services/PasswordHistoryService.php index 3eec2c3..cb1efea 100644 --- a/src/app/Services/PasswordHistoryService.php +++ b/src/app/Services/PasswordHistoryService.php @@ -20,7 +20,7 @@ class PasswordHistoryService implements Contracts\PasswordHistoryServiceContract */ public function inHistory(int|string $userId, string $password): bool { - $passwords = $this->passwordHistoryRepository->passwords($userId); + $passwords = $this->passwordHistoryRepository->getPasswords($userId); foreach ($passwords as $history) { if (Hash::check($password, $history->password)) { return false; @@ -36,8 +36,8 @@ class PasswordHistoryService implements Contracts\PasswordHistoryServiceContract */ public function logPassword($user): void { - $passwords = $this->passwordHistoryRepository->passwords($user->id); - if (count($passwords) >= config('ae3auth-config.max_stored_passwords')) { + $countPasswords = $this->passwordHistoryRepository->countPasswords($user->id); + if ($countPasswords >= config('ae3auth-config.max_stored_passwords')) { $this->removeOldestPassword($user->id); } $passwordCol = config('ae3auth-config.user.password_column'); @@ -60,9 +60,28 @@ class PasswordHistoryService implements Contracts\PasswordHistoryServiceContract */ public function removeOldestPassword($userId): void { - $oldest = $this->passwordHistoryRepository->oldestPassword($userId); + $oldest = $this->passwordHistoryRepository->findOldestPassword($userId); if ($oldest) { $this->passwordHistoryRepository->destroy($oldest->id); } } + + /** + * @param $user + * @return void + */ + public function updateExpireColumn($user): void + { + $expiresAtColumn = config('ae3auth-config.user.expires_password_column_name'); + $user->$expiresAtColumn = now()->addDays(config('ae3auth-config.password_expires_in')); + } + + /** + * @return void + */ + public function makePasswordAvailable($user): void + { + $forceChangePasswordColumn = config('ae3auth-config.user.force_change_column_name'); + $user->$forceChangePasswordColumn = false; + } }