Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.6使用策略授权行动_Php_Laravel - Fatal编程技术网

Php Laravel 5.6使用策略授权行动

Php Laravel 5.6使用策略授权行动,php,laravel,Php,Laravel,我正在尝试实施一个策略来阻止资源的编辑功能 我的路线: Route::resource('imagerequests', 'ImageRequestController'); 我的政策 class ImageRequestPolicy { use HandlesAuthorization; const STATUS_EXECUTING = "executing"; public function edit(ImageRequest $imageRequest)

我正在尝试实施一个策略来阻止资源的编辑功能

我的路线:

Route::resource('imagerequests', 'ImageRequestController');
我的政策

class ImageRequestPolicy
{
    use HandlesAuthorization;

    const STATUS_EXECUTING = "executing";

    public function edit(ImageRequest $imageRequest)
    {
        return $imageRequest->status !== self::STATUS_EXECUTING;
    }
}
但我仍然可以访问“imagerequests/{id}/edit”路由

编辑 ImageRequest模型

class ImageRequest extends Model
编辑ImageRequestController方法

public function edit($id, ImageRequest $imageRequest)
{
    $this->authorize('edit', $imageRequest);

    $imageRequest = ImageRequest::findOrFail($id);
    $requestTypes = RequestType::all();
    $attachments = $this->imageRequestRepository->getAttachmentsListOfImageRequestById($id);

    return view('imagerequest.edit', compact('imageRequest', 'requestTypes', 'attachments'));
}

使用中间件保护路由

use App\Post;
Route::put('/post/{post}', function (Post $post) {
    // The current user may update the post...
})->middleware('can:update,post');

希望这会有所帮助。

您的编辑方法错误,它的第一个参数必须是用户:

public function edit(User $user, ImageRequest $imageRequest)
{
    return $imageRequest->status !== self::STATUS_EXECUTING;
}
添加到ImageRequestController的编辑方法:

public function edit(ImageRequest $imageRequest) {

$this->authorize('edit',$imageRequest);

...

}
$user参数是由laravel自动添加的

您还需要在AuthServiceProvider中注册策略

protected $policies = [
    ImageRequest::class => ImageRequestPolicy::class,
];
ImageRequest必须扩展模型类。它是一个模型还是一个\http\request

你的控制器有问题。你说你的路线是:

/imagerequests/26/编辑

在您的控制器中,您正在注入一个新的空白ImageRequest,这可能就是它通过授权测试的原因。试试这个:

public function edit($id, ImageRequest $imageRequest)
{
    $imageRequest = ImageRequest::findOrFail($id);

    $this->authorize('edit', $imageRequest);

    $requestTypes = RequestType::all();
    $attachments = $this->imageRequestRepository->getAttachmentsListOfImageRequestById($id);

    return view('imagerequest.edit', compact('imageRequest', 'requestTypes', 'attachments'));
}

盖茨和政策之间存在差异,从文件中很难理解。闸门用于授权控制器方法,而资源负责授权与模型有关的操作,即实际的数据库记录

因此,在您的情况下,您应该使用门而不是策略。您仍然可以使用现有的策略类,但必须以不同的方式注册它。而不是使用

/**
 * The policy mappings for the application.
 *
 * @var array
 */
protected $policies = [
    ImageRequest::class => ImageRequestPolicy::class,
];

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();
}
你应该使用

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

    Gate::resource('imageRequests', App\Policies\ImageRequestPolicy::class);
}

供进一步参考,请查看。

请查看您可以授权的表单request@RomanBobrik我首先需要阻止他们访问表单。显示您的controller@edit代码。@ArthurSamarcos用controller@edit代码我不能使用这个,因为我有一个资源路径看看我的问题你可以在你的控制器构造函数中使用它。我不使用用户参数,为什么我需要把它放在那里?它是由laravel自动添加的。如果不使用,将出现错误。该imagerequest的状态如何?我在AuthServiceProvider(编辑的OP)中注册了策略,imagerequest已扩展模型状态为100%“正在执行”
/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Gate::resource('imageRequests', App\Policies\ImageRequestPolicy::class);
}