0
我有一个休息API,其中很多路由将以/ org/{id}为前缀。此前缀将限制嵌套数据的范围,并且如果用户是组织成员,则需要检查该用户。中间件是否是运行这些检查的最佳位置,并初始化可在整个请求的其余部分访问的orgcontext类?Laravel rest API中间件
我有一个休息API,其中很多路由将以/ org/{id}为前缀。此前缀将限制嵌套数据的范围,并且如果用户是组织成员,则需要检查该用户。中间件是否是运行这些检查的最佳位置,并初始化可在整个请求的其余部分访问的orgcontext类?Laravel rest API中间件
中间件可能是做这些检查的理想场所:
<?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);
}
}
是用户通常不止一个组织的成员? – ceejayoz
@ceejayoz是的。在前端有功能来改变你正在工作的组织。它是一个角度spa,我使用会话存储来存储当前的组织标识,以便用户可以在不同的标签中使用不同的组织。 –