fix(customer): allow deleting customers locally if the account is not activated.
This commit is contained in:
parent
d5cb4c7d78
commit
0ec5ab7f8c
3 changed files with 28 additions and 80 deletions
|
|
@ -104,7 +104,7 @@ class CrmUserProvider extends EloquentUserProvider implements UserProviderContra
|
|||
// * Ne devrait pas survenir, $crm->login throws ApiError::authFailed()
|
||||
return $crm->login($credentials['email'], $credentials['password']);
|
||||
} catch (ApiErrorException $e) {
|
||||
if (!$model->hashcode or !CrmCustomer::find($model->hashcode)) {
|
||||
if ($e->getApiError()->equals(ApiError::modelNotFound)) {
|
||||
$model->forceDelete();
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,70 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
||||
|
|
@ -79,8 +79,10 @@ class Customer extends Authenticatable implements MustVerifyEmail
|
|||
static::updating(function (self $model) {
|
||||
self::assertEmailIsUnique($model->id, $model->email);
|
||||
});
|
||||
static::deleting(function () {
|
||||
throw new \RuntimeException('This cannot be done à cause de MesBr');
|
||||
static::deleting(function (self $model) {
|
||||
if ($model->existsInCrmAndIsActivated()) {
|
||||
throw new \RuntimeException('This cannot be done à cause de MesBr');
|
||||
}
|
||||
});
|
||||
static::deleted(function (self $model) {
|
||||
if ($model->forceDeleting) {
|
||||
|
|
@ -234,14 +236,16 @@ class Customer extends Authenticatable implements MustVerifyEmail
|
|||
$this->notify(new ResetPassword($token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the customer exists in CRM.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function existsInCrmAndIsActivated(): bool
|
||||
{
|
||||
$crmCustomer = $this->getCrmCustomer();
|
||||
|
||||
return $crmCustomer && $crmCustomer->status;
|
||||
}
|
||||
|
||||
public function existsInCrm(): bool
|
||||
{
|
||||
return $this->hashcode and CrmCustomer::find($this->hashcode);
|
||||
return $this->getCrmCustomer() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -258,7 +262,7 @@ class Customer extends Authenticatable implements MustVerifyEmail
|
|||
}
|
||||
/** @var static|null $loggedCustomer */
|
||||
$loggedCustomer = \auth('frontend')->user();
|
||||
$isLogged = \optional($loggedCustomer)->id === $this->id and !empty($this->id);
|
||||
$isLogged = \optional($loggedCustomer)->id === $this->id && !empty($this->id);
|
||||
if ($isLogged) {
|
||||
\auth('frontend')->logout();
|
||||
}
|
||||
|
|
@ -268,6 +272,20 @@ class Customer extends Authenticatable implements MustVerifyEmail
|
|||
]);
|
||||
}
|
||||
|
||||
private function getCrmCustomer(): ?CrmCustomer
|
||||
{
|
||||
if (!$this->hashcode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return CrmCustomer::find($this->hashcode);
|
||||
} catch (CrmException | ApiErrorException $e) {
|
||||
report($e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// * --- END AUTH ---
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue