2018-07-31 04:11:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
use Illuminate\Http\Request;
|
2018-07-31 04:11:00 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Closure;
|
|
|
|
|
2021-12-22 22:19:40 +00:00
|
|
|
/**
|
|
|
|
* Enable us to use the role during middleware authorisation
|
|
|
|
*/
|
2018-07-31 04:11:00 +00:00
|
|
|
class Role
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
public function handle(Request $request, Closure $next, string $role)
|
2018-07-31 04:11:00 +00:00
|
|
|
{
|
|
|
|
if ($role AND ! Auth::user())
|
2021-09-29 06:20:22 +00:00
|
|
|
abort(403,'Not Authenticated');
|
2018-07-31 04:11:00 +00:00
|
|
|
|
|
|
|
switch ($role) {
|
|
|
|
case 'wholesaler':
|
|
|
|
if (Auth::user()->role() == $role)
|
|
|
|
return $next($request);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'reseller':
|
|
|
|
if (in_array(Auth::user()->role(),['wholesaler','reseller']))
|
|
|
|
return $next($request);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(404,'User doesnt have role?');
|
|
|
|
}
|
|
|
|
}
|