From 868f4a9dfbd928f63ca7dee4c17b19fee693cc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 30 Jun 2022 17:51:15 +0200 Subject: [PATCH] =?UTF-8?q?Ticket=20n=C2=B0=20962=20Fix=20unhandled=20case?= =?UTF-8?q?=20user=20is=20not=20admin=20on=20accounts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Credit/AccountController.php | 18 +++++++++++++++++- app/Models/User.php | 16 +++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Credit/AccountController.php b/app/Http/Controllers/Credit/AccountController.php index c5f8531..9cb2488 100644 --- a/app/Http/Controllers/Credit/AccountController.php +++ b/app/Http/Controllers/Credit/AccountController.php @@ -38,9 +38,25 @@ class AccountController extends Controller */ public function indexQuery(Builder $query): Builder { + /** @var \App\Models\User */ + $user = auth()->user(); + + + if (!$user->isAdmin()) { + $userBrands = $user->brands(); + if (!$userBrands->count()) { + \redirect()->route('ticket.index')->withErrors(['warning' => trans( + // phpcs:ignore Generic.Files.LineLength.TooLong + 'Pour utiliser cette fonctionnalité vous devez appartenir à une marque, merci d\'en faire la demande à :mail', + ['mail' => collect(config('mail.contactsList.serviceweb'))->implode(' ')] + )])->throwResponse(); + } + return $query->whereIn('brand_id', $userBrands->pluck('id')); + } + /** @var \App\Models\Brand|null $brand */ if (!($brand = request()->brand)) { - throw new \ErrorException('Cannot get brand'); + abort(404, 'Cannot get brand'); } return $query->where('brand_id', '=', $brand->id); diff --git a/app/Models/User.php b/app/Models/User.php index 6d7ab39..40c0bf7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -49,7 +49,11 @@ class User extends Authenticatable */ protected $crudValueColumn = 'id'; - // stores unhashed pwd + /** + * stores unhashed pwd + * + * @var string|null + */ public $tmpPass = null; /** @@ -249,6 +253,16 @@ class User extends Authenticatable // * METHODS + /** + * get User brands + * + * @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand> + */ + public function brands() + { + return $this->sites->pluck('brand')->filter(); + } + /** * Send User creation confirm mail *