2017-04-02 2 views
2

Laravel 5.4를 사용 중이고 색인보기 정책을 작성하려고합니다. 여기 Laravel 인덱스 정책

HttpException in Handler.php line 133:

This action is unauthorized.

내 컨트롤러 : 나는 Method Without a Model를 사용하려고, 나는 다음과 같은 오류가 발생하고 여기에
<?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\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\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의 미들웨어 섹션에서만 설명되며 컨트롤러 도우미는 아닙니다 ... 조금 혼란 스럽습니다.

관련 문제