leanadmin/livewire-access

使用PHP 8属性控制Livewire中的属性/方法的前端访问。

v0.1.3 2023-02-16 16:05 UTC

This package is auto-updated.

Last update: 2024-09-16 19:29:10 UTC


README

此包为Livewire添加了PHP 8.0属性支持。具体来说,这些属性用于标记组件属性和方法为前端可访问

该包包含两对特性和属性。一对用于显式访问,另一对用于隐式访问。

它的工作原理

  • 实现显式访问特质的组件如果没有#[FrontendAccess]属性,将拒绝对所有属性和方法的访问。
  • 实现隐式访问特质的组件如果没有#[BlockFrontendAccess]属性,将允许对所有属性和方法的访问。

这作为Livewire的public检查逻辑的额外层,但提供了更细粒度的控制。

为什么使用这个?

有时,您可能希望允许在PHP中(在组件外部)访问组件的属性,但不允许从前端访问。为此,您可以使用WithImplicitAccess特质。默认情况下,将启用所有属性的前端访问,但您可以禁用特定属性(或方法)的访问。

其他时候,您可能只需要比Livewire提供的默认功能更多的保证。为此,WithExplicitAccess特质正是为此而设计的。它禁用了所有前端访问,并要求您手动在特定属性/方法上启用它。

第二个选项是推荐的,因为它提供了最大的安全效益。意外地将方法设置为public是常见的,这可能会引起安全问题。禁用隐式访问对于团队中尚未完全理解Livewire内部结构的初级工程师特别有用,但可以使用它来完成许多有生产力的工作。

实际用例

假设您有一个包含以下方法的组件

public function getItemsProperty()
{
    return [
      ['secret' => false, 'name' => 'Item 1'],
      ['secret' => true, 'name' => 'Item 2'],
      ['secret' => true, 'name' => 'Item 3'],
      ['secret' => false, 'name' => 'Item 4'],
    ];
}

在Blade模板中,您想遍历项目并仅显示非秘密项目。

@foreach($this->items->filter(...) as $item)

即使您没有渲染任何秘密项目,整个数据集也将从前端可访问。

用户可以轻松地在开发者工具中获取Livewire组件并发出如下调用

component.call('getItemsProperty');

它将返回由getItemsProperty()方法返回的所有数据。

Screen Shot 2021-03-17 at 21 53 00

您可能认为在这种情况下,只需将方法设置为protected/private即可。然而,这将使其无法从Blade模板中访问。尽管Livewire在模板中使用$this,但它是从外部访问对象的。

这意味着尽管Blade模板是完全服务器端渲染的,并且以安全的方式允许您以安全的方式访问任何PHP代码,但您无法访问许多Livewire组件的属性或方法,除非使它们公开,这可能导致意外的数据泄露。

使用此包,您可以将属性设置为公开并在PHP中的任何地方访问它,同时完全阻止从前端访问它的任何尝试。

安装

需要PHP 8。

composer require leanadmin/livewire-access

用法

此包不会更改您现有的代码。没有实现其任何特质的组件将不会受到影响。

显式访问

要启用显式访问模式,即仅启用显式允许的属性/方法的访问,请使用 WithExplicitAccess 特性。

use Livewire\Component;
use Lean\LivewireAccess\WithExplicitAccess;
use Lean\LivewireAccess\FrontendAccess;

class MyComponent extends Component
{
    // Use the trait on your component to enable this functionality
    use WithExplicitAccess;

    // Accessing this from the frontend will throw an exception
    public string $inaccessible;

    #[FrontendAccess]
    public string $accessible; // This property allows frontend access

    public function secretMethod()
    {
        // Calling this from the frontend will throw an exception
    }

    #[FrontendAccess]
    public function publicMethod()
    {
        // This method allows frontend access
    }
}

隐式访问

要启用隐式访问模式,即继续使用相同模式,请使用 WithExplicitAccess 特性。

use Livewire\Component;
use Lean\LivewireAccess\WithImplicitAccess;
use Lean\LivewireAccess\BlockFrontendAccess;

class MyComponent extends Component
{
    // Use the trait on your component to enable this functionality
    use WithImplicitAccess;

    // This property allows frontend access
    public string $accessible;

    #[BlockFrontendAccess]
    public string $inaccessible; // This property blocks frontend access

    public function publicMethod()
    {
        // This method allows frontend access
    }

    #[BlockFrontendAccess]
    public function secretMethod()
    {
        // This method blocks frontend access
    }
}

详情

  • 属性仍然需要是 public 才能被访问。
  • 抛出的异常与 Livewire 在属性/方法不是公开的情况下抛出的异常相同。

开发

在本地运行所有检查

./check

运行测试

phpunit

代码风格将由 php-cs-fixer 自动修复。