Laravel 5.6 策略在远程服务器上运行不正常
Laravel 5.6 Policy is not working properly on remote server
我创建了一个名为 LetterPolicy
的策略,这是代码
namespace App\Policies;
use App\Letter;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class LetterPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
public function update(User $user, Letter $letter)
{
return($user->id === $letter->user_id || $user->role_id===1 ) ;
}
}
这是authserviceprovider
namespace App\Providers;
use App\Letter;
use App\Policies\LetterPolicy;
use App\Policies\UserPolicy;
use App\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
User::class => UserPolicy::class,
Letter::class => LetterPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}
在下面的代码中我检查了用户
class LetterController extends Controller
{
protected $user;
public function __construct()
{
$this->middleware(function ($request, $next){
$this->user = Auth::user();
return $next($request);
});
}
public function edit(Letter $letter)
{
if($this->user->can('update', $letter)){
//edit
}
else
abort('403', 'Access Denied');
}
该代码在本地主机上运行良好,但在远程服务器上它报告 access denied
错误。我在服务器上部署站点后创建了此策略,因此我使用代码
创建了一个路由 /clear-cache
Route::get('/clear-cache', function() {
$exitCode = \Illuminate\Support\Facades\Artisan::call('cache:clear');
});
创建策略后清除缓存。但是还是报403
错误。有什么问题?
我在控制器中尝试 dd($this->user->id === $letter->user_id || $this->user->role_id===1 );
,它返回 false
。我试了 dd($this->user->id == $letter->user_id || $this->user->role_id==1 );
,结果是 true
。现在可以了,但我不知道为什么!!!
Fjarlaegur 的回答是关键。我遇到了同样的问题:在 localhost 中没有问题,但在生产服务器中,不知何故每次授权都失败了,这是因为比较运算符。从 === 更改为 ==,一切正常。
我创建了一个名为 LetterPolicy
的策略,这是代码
namespace App\Policies;
use App\Letter;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class LetterPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
public function update(User $user, Letter $letter)
{
return($user->id === $letter->user_id || $user->role_id===1 ) ;
}
}
这是authserviceprovider
namespace App\Providers;
use App\Letter;
use App\Policies\LetterPolicy;
use App\Policies\UserPolicy;
use App\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
User::class => UserPolicy::class,
Letter::class => LetterPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}
在下面的代码中我检查了用户
class LetterController extends Controller
{
protected $user;
public function __construct()
{
$this->middleware(function ($request, $next){
$this->user = Auth::user();
return $next($request);
});
}
public function edit(Letter $letter)
{
if($this->user->can('update', $letter)){
//edit
}
else
abort('403', 'Access Denied');
}
该代码在本地主机上运行良好,但在远程服务器上它报告 access denied
错误。我在服务器上部署站点后创建了此策略,因此我使用代码
/clear-cache
Route::get('/clear-cache', function() {
$exitCode = \Illuminate\Support\Facades\Artisan::call('cache:clear');
});
创建策略后清除缓存。但是还是报403
错误。有什么问题?
我在控制器中尝试 dd($this->user->id === $letter->user_id || $this->user->role_id===1 );
,它返回 false
。我试了 dd($this->user->id == $letter->user_id || $this->user->role_id==1 );
,结果是 true
。现在可以了,但我不知道为什么!!!
Fjarlaegur 的回答是关键。我遇到了同样的问题:在 localhost 中没有问题,但在生产服务器中,不知何故每次授权都失败了,这是因为比较运算符。从 === 更改为 ==,一切正常。