我想在拉拉维尔创造一扇门。这样做的目的是检查一个角色是否存在于AzureAD访问令牌上。
到目前为止我有这个
Gate::define('admin_cml', function () {
$roles = AzureUser::roles();
return in_array(config('app-roles.cml_admin'),$roles);
});到目前为止,响应是“在对象上下文中不使用$this”。

这是完整的代码
AuthServiceProvider ->路径应用程序\提供者
<?php
namespace App\Providers;
use App\Models\AzureUser;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('admin_cml', function () {
$roles = AzureUser::roles();
return in_array(config('app-roles.cml_admin'),$roles);
});
}
}AzureUser Model ->应用程序模型
<?php
namespace App\Models;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Database\Eloquent\Model;
class AzureUser extends Model
{
protected $id_token;
protected $access_token;
protected $user;
public function __construct($access_token, $id_token)
{
$this->access_token = $access_token;
$this->id_token = $id_token;
$this->user = Socialite::driver('azure-oauth')->userFromToken($access_token);
}
public function get()
{
return $this->user;
}
public function roles()
{
$tokens = explode('.', $this->id_token);
return json_decode(static::urlsafeB64Decode($tokens[1]))->roles;
}
public static function urlsafeB64Decode($input)
{
$remainder = strlen($input) % 4;
if ($remainder) {
$padlen = 4 - $remainder;
$input .= str_repeat('=', $padlen);
}
return base64_decode(strtr($input, '-_', '+/'));
}
}路由
<?php
use App\Http\Controllers\loginController;
use Illuminate\Support\Facades\Route;
use Laravel\Socialite\Facades\Socialite;
Route::get('/', function () {
if(Auth::check()){
return redirect()->route('app');
}
return view('welcome');
})->name('home');
Route::get('/app',function(){
return view('layouts.app');
})->name('app')->middleware('auth');
// user routes
Route::prefix('user')->group(function () {
Route::get('/login', [loginController::class,'login'])->name('login');
Route::get('/logout', [loginController::class,'logout'])->name('logout');
});
Route::group(['middelware'=>['auth'],'prefix'=>'adminmm','as'=>'adminmm.'],function () {
Route::get('/carros', function(){
return "carros";
})->name('carros');
Route::get('/companias', function(){
return "companias";
})->name('companias');
});
// Here is the error. When going to admincml/mecanicos is when the error is showing up
Route::group(['middelware'=>['auth'],'prefix'=>'admincml','as'=>'admincml.'],function () {
Route::get('/mecanicos', function(){
if (! Gate::allows('admin_cml')) {
abort(403);
}
return "mecanicos";
})->name('mecanicos');
});有什么想法吗?为什么没有发现模型?谢谢!
发布于 2022-05-16 10:53:11
该错误来自于AuthServiceProvider,您在其中使用了对方法的静态调用,但它不是静态调用.
AzureUser::roles();您似乎希望在那里使用$this实例变量,因此您必须在AuthServiceProvider.php中为它创建一个实例:
use App\AzureUser;
$azure = new AzureUser();
$roles = $azure->role();https://stackoverflow.com/questions/70553886
复制相似问题