Refatoracao para alterar metodos para classe de servico

refatoracoes-metodos
Pablo 10 months ago
parent d46b3d765c
commit b2ec0f7d7e

@ -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
{

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

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

@ -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;
}
}

Loading…
Cancel
Save