Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Pathao\Kanban\Middleware;
- use Closure;
- use Illuminate\Support\Facades\Redis;
- use Pathao\Kanban\Models\Board;
- use Predis\Connection\ConnectionException;
- class BoardAccessValidationMiddleware
- {
- public function handle($request, Closure $next)
- {
- $boardId = $request->route('boardId') ?: $request->route('board');
- $key = 'board-parent:' . $boardId;
- $redis = Redis::connection();
- try {
- // If redis server is not running or connection can't be established, following ping will throw exception
- $redis->ping();
- $clientId = Redis::get($key);
- if(!$clientId) {
- $clientId = $this->getClientId($boardId);
- Redis::set($key, $clientId);
- }
- } catch (ConnectionException $e) {
- $clientId = $this->getClientId($boardId);
- } catch (\Exception $e) {
- throw $e;
- }
- if($clientId != $request->get('client_id')) {
- throw new \Exception('Unauthorized access', 403);
- }
- return $next($request);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement