Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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-扩展authorizeResource以处理自定义方法_Php_Laravel - Fatal编程技术网

PHP/Laravel-扩展authorizeResource以处理自定义方法

PHP/Laravel-扩展authorizeResource以处理自定义方法,php,laravel,Php,Laravel,我有一个名为StreamController.php的资源控制器,它使用名为StreamPolicy.php的策略 在我的控制器中,我有: //StreamController.php /** * Construct method. */ public function __construct() { $this->middleware('auth'); $this->authorizeResourc

我有一个名为
StreamController.php的资源控制器,它使用名为
StreamPolicy.php的策略

在我的控制器中,我有:

    //StreamController.php
    /**
     * Construct method.
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->authorizeResource(Stream::class, 'stream');
    }
有了以上内容,所有RESTful端点都可以使用策略成功地“保护”

但是,我在控制器中添加了一个新方法,称为
documents()
,如下所示:

//web.php
Route::get('streams/{stream}/documents', 'StreamController@documents');
现在的问题是,如果我访问URL:

example.com/streams/1如果我不是流的所有者,我会得到403页-但是如果我去: example.com/streams/1/documents如果我不是流的所有者,我仍然可以访问该页面

我做错了什么?我如何才能使我的策略也涵盖控制器中的
documents()
方法

编辑:

这是我的
StreamPolicy.php
文件:

//StreamPolicy.php
namespace App\Policies;

use App\User;
use App\Stream;
use Illuminate\Auth\Access\HandlesAuthorization;

class StreamPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view the stream.
     *
     * @param  \App\User  $user
     * @param  \App\Stream  $stream
     * @return mixed
     */
    public function view(User $user, Stream $stream)
    {
        return $user->id == $stream->user_id;
    }

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

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

        return $user->id == $stream->user_id;
    }

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

        return $user->id == $stream->user_id;
    }

    /**
     * Determine whether the user can restore the stream.
     *
     * @param  \App\User  $user
     * @param  \App\Stream  $stream
     * @return mixed
     */
    public function restore(User $user, Stream $stream)
    {
        //
    }

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

我不知道为什么不工作,但我担心
authorizeResource
方法只处理已知资源端点的路由:查看、创建、更新、删除和恢复

稍后编辑:查看文档,看看哪些是由资源控制器处理的操作

您应该做的是显式设置新路由的授权:

Route::get('streams/{stream}/documents', 'StreamController@documents')->middleware('can:documents,stream');
当然,
documents
方法应该存在于
StreamPolicy
类中

要在
StreamController.documents
方法中进行授权:

public function documents(Stream $stream)
{
    $this->authorize('documents', $stream);

    return view('streams.documents', compact('stream'));
}
Controller.php使用“AuthorizesRequest”特性,它定义了以下两种方法:

trait AuthorizesRequests
{
  /**
 * Get the map of resource methods to ability names.
 *
 * @return array
 */
protected function resourceAbilityMap()
{
    return [
        'show' => 'view',
        'create' => 'create',
        'store' => 'create',
        'edit' => 'update',
        'update' => 'update',
        'destroy' => 'delete',
    ];
}

/**
 * Get the list of resource methods which do not have model parameters.
 *
 * @return array
 */
protected function resourceMethodsWithoutModels()
{
    return ['index', 'create', 'store'];
}
每个控制器都可以重写这两个受保护的方法,因为每个控制器都扩展controller.php

class UserController extends Controller
{
    public function __construct ()
    {
        $this->authorizeResource ( User::class, 'user' );
    }

   /**
     * Get the map of resource methods to ability names.
     *
     * @return array
     */
    protected function resourceAbilityMap()
    {
        return [
            'show' => 'view',
            'create' => 'create',
            'store' => 'create',
            'edit' => 'update',
            'update' => 'update',
            'destroy' => 'delete',
            'customMethod'=>'customMethod',
            'customMethodWithoutModel'=>'customMethodWithoutModel'
        ];
    }

    /**
     * Get the list of resource methods which do not have model parameters.
     *
     * @return array
     */
    protected function resourceMethodsWithoutModels()
    {
        return ['index', 'create', 'store','customMethodWithoutModel'];
    }
其政策类别

class UserPolicy
{

   /**
     * Determine whether the user can custom method.
     *
     * @param  \App\User $user
     * @param  \App\User $model
     * @return mixed
     */
   public function customMethod(User $user, User $model){
        return true; 
   }

   /**
     * Determine whether the user can custom method without model.
     *
     * @param  \App\User $user
     * @return mixed
     */
   public function customMethodWithoutModel(User $user){
        return true;
   }

请您尝试一下
Route::get('streams/{stream}/documents','StreamPolicy','StreamController@documents');给我的
路由操作无效:[App\Http\Controllers\StreamPolicy]
确定。。然后。。另一种方法是检查StreamPolicy是否有
documents
方法,该方法实际上调用了policy中的
read
方法我在policy中没有看到任何
read
方法。然而,我试图在StreamPolicy中添加一个
documents
方法。但这并没有改变任何事情您有什么版本的laravel?如何将额外参数传递给自定义策略方法?您不能将额外参数传递给自定义策略方法,因为它们是由框架注入的。第一个是经过身份验证的用户,即
user$user
,第二个是针对资源路由的解析模型,即
user$user
。您需要创建一个服务,或者将它们添加到当前请求中,并通过请求助手功能访问它们。如果需要访问嵌套的资源路由,请通过
request()->route('model\u param\u name')
访问已解析的模型。e、 g.
request()->route('post')
这个答案应该被接受,因为它遵循了Laravel标准。
class UserPolicy
{

   /**
     * Determine whether the user can custom method.
     *
     * @param  \App\User $user
     * @param  \App\User $model
     * @return mixed
     */
   public function customMethod(User $user, User $model){
        return true; 
   }

   /**
     * Determine whether the user can custom method without model.
     *
     * @param  \App\User $user
     * @return mixed
     */
   public function customMethodWithoutModel(User $user){
        return true;
   }