Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Pathao\Kanban\Middleware;
  4. use Closure;
  5. use Illuminate\Support\Facades\Redis;
  6. use Pathao\Kanban\Models\Board;
  7. use Predis\Connection\ConnectionException;
  8.  
  9. class BoardAccessValidationMiddleware
  10. {
  11.     public function handle($request, Closure $next)
  12.     {
  13.         $boardId = $request->route('boardId') ?: $request->route('board');
  14.         $key = 'board-parent:' . $boardId;
  15.  
  16.         $redis = Redis::connection();
  17.  
  18.         try {
  19.             // If redis server is not running or connection can't be established, following ping will throw exception
  20.             $redis->ping();
  21.  
  22.             $clientId = Redis::get($key);
  23.  
  24.             if(!$clientId) {
  25.                 $clientId = $this->getClientId($boardId);
  26.  
  27.                 Redis::set($key, $clientId);
  28.             }
  29.         } catch (ConnectionException $e) {
  30.  
  31.             $clientId = $this->getClientId($boardId);
  32.  
  33.         } catch (\Exception $e) {
  34.             throw $e;
  35.         }
  36.  
  37.         if($clientId != $request->get('client_id')) {
  38.             throw new \Exception('Unauthorized access', 403);
  39.         }
  40.  
  41.         return $next($request);
  42.     }
  43.  
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement