0
我试图使用Laravel 5.2身份验证包Authorization Policies。我遵循了文档。我似乎无法使其工作。Laravel 5.2授权政策不被阅读
我有用户和文档表,文档有user_id字段也有。
AuthServiceProvider.php
<?php
namespace App\Providers;
use App\Document;
use App\User;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
User::class => Document::class,
];
/**
* Register any application authentication/authorization services.
*
* @param \Illuminate\Contracts\Auth\Access\Gate $gate
* @return void
*/
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
}
}
DocumentPolicy.php
<?php
namespace App\Policies;
use App\Document;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class DocumentPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
public function before($user, $ability)
{
if ($user->isSuperAdmin()) {
return true;
}
}
/**
* Determine if the given post can be updated by the user.
*
* @param \App\User $user
* @param \App\Post $post
* @return bool
*/
public function update(User $user, Document $document)
{
return $user->id === $document->user_id;
}
}
在DocumentsController编辑功能
use Gate;
use App\Document;
.......some code here....
public function edit($id){
$document = Document::findOrFail($id);
if (Gate::allows('update', $document))
{
dd('a');
}
}
正如你可以看到我把DD(“A”)来测试这一点,我不能穿过它 总是落得条件内,即使记录我即将 编辑是不同的用户。