73 lines
2.3 KiB
PHP
73 lines
2.3 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): ?\Illuminate\Contracts\Auth\Authenticatable
|
||
|
|
{
|
||
|
|
// * 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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// @phpstan-ignore-next-line
|
||
|
|
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): bool
|
||
|
|
{
|
||
|
|
/** @var \App\Models\Customer $customer */
|
||
|
|
if ($customer instanceof Customer) {
|
||
|
|
throw new \ErrorException('Cannot authenticate any thing else than ' . Customer::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
// @phpstan-ignore-next-line
|
||
|
|
$plain = $credentials['password'];
|
||
|
|
|
||
|
|
/** @var \App\Models\Customer $customer */
|
||
|
|
if ($this->hasher->check($plain, $customer->getAuthPassword())) {
|
||
|
|
// * Restore customer
|
||
|
|
return boolval($customer->restore());
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|