ldc-gen-game-web/app/Auth/ReactivateCustomerProvider.php
noah.lancien 218a3a7891 phpstan
2024-01-31 09:03:18 +00:00

70 lines
2.2 KiB
PHP

<?php
namespace App\Auth;
use App\Models\Customer;
use Illuminate\Support\Str;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
/**
* Overload EloquentUserProvider to handle
* Soft deleted Customers
*/
class ReactivateCustomerProvider extends EloquentUserProvider implements UserProvider
{
/**
* Retrieve a customer by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
* @throws \ErrorException If cannot connect to LDAP.
*/
public function retrieveByCredentials(array $credentials)
{
// * Make changes
if (empty($credentials)) {
return null;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a customer, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (!Str::contains($key, 'password')) {
$query->where($key, $value);
}
}
/** @var \App\Models\Customer|null $query */
return $query->withTrashed()->first();
}
/**
* Validate a customer against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $customer
* @param array $credentials
* @return boolean
* @throws \ErrorException If model provided is not a Customer or connection to server fails.
*/
public function validateCredentials(Authenticatable $customer, array $credentials)
{
/** @var \App\Models\Customer $customer */
if ($customer instanceof Customer) {
throw new \ErrorException('Cannot authenticate any thing else than ' . Customer::class);
}
/** @var \App\Models\Customer $customer */
// @phpstan-ignore-next-line
if ($this->hasher->check($credentials['password'], $customer->getAuthPassword())) {
// * Restore customer
return boolval($customer->restore());
}
return false;
}
}