diff --git a/app/Auth/CrmUserProvider.php b/app/Auth/CrmUserProvider.php index af103ab..4500f5c 100644 --- a/app/Auth/CrmUserProvider.php +++ b/app/Auth/CrmUserProvider.php @@ -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; } diff --git a/app/Auth/ReactivateCustomerProvider.php b/app/Auth/ReactivateCustomerProvider.php deleted file mode 100644 index b200b08..0000000 --- a/app/Auth/ReactivateCustomerProvider.php +++ /dev/null @@ -1,70 +0,0 @@ -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; - } -} diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 3c674ee..f4f8072 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -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 --- /**