gpt4 book ai didi

php - Laravel 索引策略

转载 作者:行者123 更新时间:2023-12-02 05:19:05 27 4
gpt4 key购买 nike

我使用 Laravel 5.4,我正在尝试为我的索引 View 编写一个策略。我正在尝试使用 Method Without a Model ,我收到以下错误:

HttpException in Handler.php line 133:

This action is unauthorized.

这是我的 Controller :

<?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) 。此外, View 策略运行良好。

如何为我的索引 View 编写策略?

最佳答案

保持一切相同但发生变化:

$this->authorize('index');

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

解决了问题。显然,模型类需要传递给不需要模型的操作。这仅在 Laravel 的 docs 的中间件部分下进行了描述。 ,而不是 Controller 助手...有点令人困惑。

关于php - Laravel 索引策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43173663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com