2016-03-15 2 views
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'); 
     } 
} 

Как вы можете видеть, что я поставил дд («а»), чтобы проверить это, и я не могу пройти через это и всегда в конечном итоге в состоянии, даже если запись я собираюсь правка другой пользователь.

ответ

0

в

protected $policies = [ User::class => Document::class, ]; должно быть

`protected $policies = [ 
    User::class => DocumentPolicy::class, 
];` 
Смежные вопросы