我正在尝试使用Laravel 5.2身份验证包授权策略。我已经遵循了文档。而且我似乎无法让它工作。
我有用户和文档表,文档也有user_id字段。
身份验证服务提供商.php
<?php
namespace AppProviders;
use AppDocument;
use AppUser;
use IlluminateContractsAuthAccessGate as GateContract;
use IlluminateFoundationSupportProvidersAuthServiceProvider 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 IlluminateContractsAuthAccessGate $gate
* @return void
*/
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
}
}
文档策略.php
<?php
namespace AppPolicies;
use AppDocument;
use AppUser;
use IlluminateAuthAccessHandlesAuthorization;
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 AppUser $user
* @param AppPost $post
* @return bool
*/
public function update(User $user, Document $document)
{
return $user->id === $document->user_id;
}
}
在文档中控制器编辑功能
use Gate;
use AppDocument;
.......some code here....
public function edit($id){
$document = Document::findOrFail($id);
if (Gate::allows('update', $document))
{
dd('a');
}
}
如您所见,我用dd('a')来测试它,但我无法通过它 并且总是在条件中结束,即使我即将记录 编辑是不同的用户。
protected $policies = [
User::class => Document::class,
];
它应该是
`protected $policies = [
User::class => DocumentPolicy::class,
];`