php - 在 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 有任何方法来扩展附加检查?
谢谢!
解决方案
推荐阅读
- css - 修改内容中的文本
- javascript - 如何在 Google 表格/应用程序脚本中复制值并保留格式?
- c# - 如何为 MediatR 处理程序 C# 编写单元测试
- python - 如何像在 R 中一样轻松地使用 python 读取和写入 googlesheets?
- python-3.x - 为什么应用程序不可用 Openshift 4?
- airflow - 运行 Apache-airflow SnowflakeOperator 教程示例的问题
- sql - 通过订阅获取文章列表
- powershell - CmdletBinding() 不会在 Azure Pipeline Powershell 任务中运行
- arrays - ReDim 后初始化数组
- sql-server - 在 SQL Server 中使用带有更新查询的用户定义函数