1
0
Fork 0
mirror of https://github.com/archtechx/livewire-access.git synced 2025-12-12 20:34:03 +00:00

implicit access

This commit is contained in:
Samuel Štancl 2021-03-17 17:47:55 +01:00
parent a5e58a14d8
commit 8df06ae2c3
9 changed files with 184 additions and 22 deletions

View file

@ -1,8 +1,15 @@
# Explicit property/method access for Livewire
# Livewire Access
This package adds PHP 8.0 attribute support to Livewire. In specific, the attributes are used for flagging component properties and methods as *frontend-accessible*.
Components which implement the trait provided by this package will implicitly deny access to all properties and methods if they don't have the `#[FrontendAccess]` attribute, regardless of their visibility.
The package ships with two pairs of traits and attributes. One for *explicit* access, and one for *implicit* access.
- Components which implement the trait for **explicit** access will *deny* access to all properties and methods if they don't have the `#[FrontendAccess]` attribute.
- Components which implement the trait for **implicit** access will *allow* access unless the component has the `#[BlockFrontendAccess]` attribute.
This acts as a layer on top of Livewire's logic for distinguishing public properties, but it gives you the ability to manually make changes when you need more control than property/method visibility.
The trait for only allowing explicit access can also be useful to prevent accidentally making methods `public` when it's not needed, which has the potential to lead to security issues. This can be especially useful on teams with junior engineers who don't yet have a full understanding of Livewire's internals, but can be very productive with it.
## Installation
@ -12,6 +19,12 @@ composer require leanadmin/livewire-access
## Usage
This package doesn't make any changes to your existing code. Components which don't implement either one of its traits will not be affected.
### Explicit access
To enable the explicit access mode, i.e. only enable access to properties/methods that explicitly allow it, use the `WithExplicitAccess` trait.
```php
use Livewire\Component;
use Lean\LivewireAccess\WithExplicitAccess;
@ -34,16 +47,50 @@ class MyComponent extends Component
}
#[FrontendAccess]
public function secretMethod()
public function publicMethod()
{
// This method allows frontend access
}
}
```
The properties still need to be `public` to be accessible.
### Implicit access
The thrown exceptions are identical to those that Livewire would throw if the properties/methods were not public.
To enable the implicit access mode, i.e. keep using the same mode , use the `WithExplicitAccess` trait.
```php
use Livewire\Component;
use Lean\LivewireAccess\WithImplicitAccess;
use Lean\LivewireAccess\FrontendAccess;
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
}
}
```
### Details
- The properties still need to be `public` to be accessible.
- The thrown exceptions are identical to those that Livewire would throw if the properties/methods were not public.
## Development