Advertisement
widana

middleware

Jul 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Middleware;
  4.  
  5. use App;
  6. use Session;
  7. use Closure;
  8. use Exception;
  9. use ReflectionException;
  10.  
  11. class ValidRequest {
  12.  
  13.     public function handle($request, Closure $next) {
  14.  
  15.         try {
  16.  
  17.             $object = $request->all();
  18.  
  19.             $this->checkKeyExists($object, "service");
  20.             $this->checkKeyExists($object, "taskName");
  21.             $this->checkKeyExists($object, "payload");
  22.  
  23.             $this->checkAuthorizedTaskUser($object["taskName"]);
  24.  
  25.             $this->checkClassExists($object["service"]);
  26.  
  27.             return $next($request);
  28.  
  29.         } catch (Exception $ex) {
  30.             return response()->json([
  31.                 "error_message" => $ex->getMessage(),
  32.                 "status" => _RESPONSE_FAIL,
  33.                 "status_code" => $ex->getCode()
  34.             ]);
  35.         }
  36.  
  37.     }
  38.  
  39.     private function checkKeyExists($object, $key) {
  40.  
  41.         if(!array_key_exists($key, $object)){
  42.             throw new Exception("Key $key not found");
  43.         }
  44.     }
  45.  
  46.     private function checkAuthorizedTaskUser($taskName) {
  47.         if($taskName != 'system') {
  48.             if(!in_array($taskName, Session::get(_PLEAF_CURRENT_ROLE))) {
  49.                 throw new Exception("You are not eligible to access");
  50.             }
  51.         }
  52.  
  53.     }
  54.  
  55.     private function checkClassExists($service) {
  56.  
  57.         try {
  58.  
  59.             App::make($service);
  60.  
  61.         } catch (ReflectionException $ex) {
  62.             throw new Exception("Cannot find $service not found");
  63.         }
  64.  
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement