【问题标题】:Laravel Nova authorization using Policy is not working使用策略的 Laravel Nova 授权不起作用
【发布时间】:2019-04-04 23:45:57
【问题描述】:

我正在使用 Laravel 开发一个 Web 应用程序。我正在使用 Nova 作为管理面板。我现在正在做的是使用文档中提到的策略来授权我的资源。但似乎它不起作用。这是我到目前为止所做的。我已经创建了一个这样的新星资源。

class Item extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Item::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

然后我为该资源创建了一个名为 Item 的 Laravel 模型类。

然后我创建了策略。

class ItemPolicy
{
    use HandlesAuthorization;

    public function viewAny(User $user)
    {
        return true;
    }

    public function view(User $user, $item)
    {
        return true;
    }


    public function create(User $user)
    {
        return false;
    }

    public function update(User $user, $item)
    {

        return false;
    }

    public function delete(User $user, $item)
    {
        return false;
    }

    public function restore(User $user, $item)
    {
        return false;
    }

    public function forceDelete(User $user, $item)
    {
        return false;
    }
}

我在 AuthServiceProvider 中注册了策略。

protected $policies = [

    Item::class => ItemPolicy::class,
];

当我在 nova 管理面板中看到项目列表时,我仍然可以创建项目。怎么了?应该隐藏创建项目的选项。

【问题讨论】:

  • 仔细检查AuthServiceProvider类下的类命名空间是否正确。 AuthServiceProvider 不会抛出任何异常,即使模型/策略类不存在。
  • @Wai Yan Hein,你解决了这个问题吗?我也遇到了同样的问题。

标签: laravel laravel-nova


【解决方案1】:

您的保单注册错误

/**
 * The policy mappings for the application.
 *
 * @var array
 */
protected $policies = [
    // 'App\Model' => 'App\Policies\ModelPolicy',
    'App\Item' => 'App\Policies\ItemPolicy',
];

【讨论】:

    【解决方案2】:

    使用rolePolicy或permissionPolicy方法定义策略

    // in app/Providers/NovaServiceProvider.php
    
    // ...
    
    public function tools()
    {
        return [
            // ...
            \Vyuldashev\NovaPermission\NovaPermissionTool::make()
                ->rolePolicy(RolePolicy::class)
                ->permissionPolicy(PermissionPolicy::class),
        ];
    }
    

    【讨论】:

      【解决方案3】:

      再次检查 AuthServiceProvider。

      您定义策略映射数组的位置:

      protected $policies = [
          Item::class => ItemPolicy::class,
      ];
      

      Item - 应该是您的 Model,而不是 Nova Resource

      【讨论】:

        【解决方案4】:

        ItemPolicyPolicy 类中删除viewAny() 方法

        【讨论】:

          【解决方案5】:

          将以下内容添加到您的 Nova 资源类中:

          public static function authorizable()
          {
              return true;
          }
          

          【讨论】:

            【解决方案6】:

            可能是因为您在方法参数中缺少模型类型

            在所有传递 $item 的方法中添加Item $item,如下所示:

            public function update(User $user, Item $item)
            {
                return false;
            }
            

            您还可以排除所有您希望不可用的方法,默认情况下它们将被禁用

            【讨论】:

            • 没有。我把类型放到每个方法中。但它仍然显示创建选项。例如,我只想隐藏创建选项。但即使我从 create 方法返回 false,它仍然显示。
            猜你喜欢
            • 2017-02-05
            • 1970-01-01
            • 2023-03-23
            • 1970-01-01
            • 1970-01-01
            • 2021-06-29
            • 1970-01-01
            • 2019-05-04
            • 1970-01-01
            相关资源
            最近更新 更多