2017-04-02 98 views
2

我使用Laravel 5.4,並試圖爲我的索引視圖編寫策略。我試圖用一個Method Without a Model,我收到以下錯誤:Laravel索引策略

HttpException in Handler.php line 133:

This action is unauthorized.

這裏是我的控制器:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\County; 
use Session; 
use App\Http\Controllers\Controller; 

class CountyController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 

    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     $counties = County::orderBy('id', 'desc')->paginate(5); 
     $this->authorize('index'); 

     return view('county.index', array(
       'counties' => $counties 
      )); 
    } 

這裏是我的AuthServicePovider:

<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\Gate; 
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; 
use App\Role; 
use App\County; 

use App\Policies\CountyPolicy; 

class AuthServiceProvider extends ServiceProvider 
{ 
    /** 
    * The policy mappings for the application. 
    * 
    * @var array 
    */ 
    protected $policies = [ 
     County::class => CountyPolicy::class, 
    ]; 

    /** 
    * Register any authentication/authorization services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     $this->registerPolicies(); 

     Gate::define('is-Admin', function ($user) { 
      if($user->roles()->where('name','Admin')->first()){ 
       return true; 
      } 
      return false; 
     }); 
    } 
} 

這裏是我的策略:

<?php 

namespace App\Policies; 

use App\User; 
use App\Role; 
use App\County; 
use Illuminate\Auth\Access\HandlesAuthorization; 

class CountyPolicy 
{ 
    use HandlesAuthorization; 

    /** 
    * Determine whether the user can view the county. 
    * 
    * @param \App\User $user 
    * @param \App\County $county 
    * @return mixed 
    */ 
    public function index(User $user) 
    { 
     $userRoles = $user->getRoleNames(); 
     $acceptedRoles = ['Sudo','Admin']; 
     $testArr = array_intersect($acceptedRoles, $userRoles); 

     dd($testArr); 

     if(!empty($testArr)){ 
      return true; 
     } 
     return false; 
     // 
    } 

    /** 
    * Determine whether the user can view the county. 
    * 
    * @param \App\User $user 
    * @param \App\County $county 
    * @return mixed 
    */ 
    public function view(User $user, County $county) 
    { 
     $userRoles = $user->getRoleNames(); 
     $acceptedRoles = ['Sudo','Admin','Client']; 
     $testArr = array_intersect($acceptedRoles, $userRoles); 

     if(!empty($testArr)){ 
      return true; 
     } 
     return false; 
     // 
    } 

    /** 
    * Determine whether the user can create counties. 
    * 
    * @param \App\User $user 
    * @return mixed 
    */ 
    public function create(User $user) 
    { 
     // 
    } 

    /** 
    * Determine whether the user can update the county. 
    * 
    * @param \App\User $user 
    * @param \App\County $county 
    * @return mixed 
    */ 
    public function update(User $user, County $county) 
    { 
     // 
    } 

    /** 
    * Determine whether the user can delete the county. 
    * 
    * @param \App\User $user 
    * @param \App\County $county 
    * @return mixed 
    */ 
    public function delete(User $user, County $county) 
    { 
     // 
    } 
} 

我永遠不會在索引策略中使用dd($ testArr)。此外,查看政策正在完美運作。

如何爲我的索引視圖編寫策略?

回答

2

藏在心裏相同,但改變:

$this->authorize('index'); 

$this->authorize('index', County::class); 

解決了這一問題。顯然,模型類需要傳遞不需要模型的動作。這只是在Laravel的docs的中間件部分下描述的,而不是控制器助手......有點令人困惑。