| @@ 10-50 (lines=41) @@ | ||
| 7 | use Route; |
|
| 8 | use Illuminate\Contracts\Auth\Factory as Auth; |
|
| 9 | ||
| 10 | class ModelAuthorize |
|
| 11 | { |
|
| 12 | const DELIMITER = '|'; |
|
| 13 | ||
| 14 | protected $auth; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Create a new middleware instance. |
|
| 18 | * |
|
| 19 | * @param \Illuminate\Contracts\Auth\Factory $auth |
|
| 20 | */ |
|
| 21 | public function __construct(Auth $auth) |
|
| 22 | { |
|
| 23 | $this->auth = $auth; |
|
| 24 | } |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @param \Illuminate\Http\Request $request |
|
| 28 | * @param Closure $next |
|
| 29 | * @param string $permissions |
|
| 30 | * |
|
| 31 | * @return mixed |
|
| 32 | * @throws \Illuminate\Auth\Access\AuthorizationException |
|
| 33 | */ |
|
| 34 | public function handle($request, Closure $next, $permissions = '') |
|
| 35 | { |
|
| 36 | if (empty($permissions)) { |
|
| 37 | throw new AuthorizationException(); |
|
| 38 | } |
|
| 39 | ||
| 40 | if (!is_array($permissions)) { |
|
| 41 | $permissions = explode(self::DELIMITER, $permissions); |
|
| 42 | } |
|
| 43 | ||
| 44 | if ($this->auth->guest() || !$request->route('model')->getPermissions()->can($permissions)) { |
|
| 45 | throw new AuthorizationException(); |
|
| 46 | } |
|
| 47 | ||
| 48 | return $next($request); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||
| @@ 10-50 (lines=41) @@ | ||
| 7 | use Route; |
|
| 8 | use Illuminate\Contracts\Auth\Factory as Auth; |
|
| 9 | ||
| 10 | class RouteAuthorize |
|
| 11 | { |
|
| 12 | const DELIMITER = '|'; |
|
| 13 | ||
| 14 | protected $auth; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Create a new middleware instance. |
|
| 18 | * |
|
| 19 | * @param \Illuminate\Contracts\Auth\Factory $auth |
|
| 20 | */ |
|
| 21 | public function __construct(Auth $auth) |
|
| 22 | { |
|
| 23 | $this->auth = $auth; |
|
| 24 | } |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @param \Illuminate\Http\Request $request |
|
| 28 | * @param Closure $next |
|
| 29 | * @param string $permissions |
|
| 30 | * |
|
| 31 | * @return mixed |
|
| 32 | * @throws \Illuminate\Auth\Access\AuthorizationException |
|
| 33 | */ |
|
| 34 | public function handle($request, Closure $next, $permissions = '') |
|
| 35 | { |
|
| 36 | if (empty($permissions)) { |
|
| 37 | $permissions = [Route::currentRouteName()]; |
|
| 38 | } |
|
| 39 | ||
| 40 | if (!is_array($permissions)) { |
|
| 41 | $permissions = explode(self::DELIMITER, $permissions); |
|
| 42 | } |
|
| 43 | ||
| 44 | if ($this->auth->guest() || !$request->user()->can($permissions)) { |
|
| 45 | throw new AuthorizationException(); |
|
| 46 | } |
|
| 47 | ||
| 48 | return $next($request); |
|
| 49 | } |
|
| 50 | } |
|
| 51 | ||