首页 > 解决方案 > 在 users_groups 表中使用 jwt-auth 进行附加检查

问题描述

在使用 tymon/jwt-auth 1.0.0 的 Laravel 5.8 应用程序中,我有 users_groups 表,我需要一些控制器的登录用户来检查是否在某个组内。为此,在 routes/api.php 我有:

Route::group(['middleware' => 'jwt.auth',  'prefix' => 'manager', 'as' => 'manager.'], function ($router) {
    Route::get('users_of_event_selection/{event_id}', 'API\ManagerController@users_of_event_selection');
    Route::post('add_user_to_event', 'API\ManagerController@add_user_to_event');
...

I app/Http/Controllers/API/ManagerController.php I added checks:

    public function __construct()
    {
        $this->middleware('jwt.auth', ['except' => []]);
        $request           = request();
        $this->requestData = $request->all();

        $loggedUser= Auth::guard('api')->user();
        $userGroupsCount = UsersGroups
            ::getByUserId($loggedUser->id)
            ->getByGroupId([ACCESS_ROLE_ADMIN,ACCESS_ROLE_MANAGER])
            ->count();
        if($userGroupsCount == 0) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

    }

$userGroupsCount == 0 

上面的代码不像我预期的那样工作,我的控件的方法返回有效数据。我想我可以制作小函数并在任何控件的方法上调用它,但是如果那是好方法?如果 jwt-auth 有任何方法来扩展附加检查?

谢谢!

标签: phplaravellaravel-5jwt-auth

解决方案


推荐阅读