Ticket n° 962 Fix unhandled case user is not admin on accounts

This commit is contained in:
Jérémy Munsch 2022-06-30 17:51:15 +02:00
parent 16dcb72800
commit 868f4a9dfb
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
2 changed files with 32 additions and 2 deletions

View file

@ -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);

View file

@ -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
*