0

I have a rest API where a lot of routes will be prefixed by /org/{id}. This prefix will scope the nested data and also the user will need to be checked if they are a member of the org. Would middleware be the best place to run these checks and also initialise an orgcontext class that can be accessed throughout the rest of the request?

2
  • Are users typically members of more than one org? Commented Jan 1, 2017 at 1:35
  • @ceejayoz Yes. There is functionality in the front end to change which org you are working in. It is an angular spa and I am using session storage to store the current org id so the user can work in different orgs in different tabs. Commented Jan 1, 2017 at 11:02

1 Answer 1

1

A middleware is probably the perfect place to do those checks:

<?php

namespace App\Http\Middleware;

use Closure;
use App\Repositories\Eloquent\UserRepository;

class CanAccessOrg
{
    public function handle($request, Closure $next, UserRepository $userRepository)
    {
        if (! $userRepository->currentUserCanAccessOrg(Session::get('org'))) {
            Auth::logout();

            return redirect()->route('login')->withErrors(['msg', 'You dont have access to this org']);;
        }

        return $next($request);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.