Skip to content

Commit

Permalink
Merge pull request #205 from boris-glumpler/component-authorization
Browse files Browse the repository at this point in the history
Implemented basic component authorization & ability to sort components
  • Loading branch information
jeffgreco13 authored Aug 24, 2023
2 parents 07f8ca0 + a614f34 commit 4e4841c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ BreezyCore::make()
])
```

#### Sorting My Profile components

Custom MyProfile components can be sorted by setting their static `$sort` property. This property can be set for existing MyProfile components in any service provider:

```php
TwoFactorAuthentication::setSort(4);
```

A lot of the time this won't be necessary, though, as the default sort order is spaced out in steps of 10, so there should be enough numbers to place any custom components in between.

### Two Factor Authentication

Expand Down
11 changes: 8 additions & 3 deletions src/BreezyCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,16 @@ public function myProfileComponents(array $components)

public function getRegisteredMyProfileComponents(): array
{
$components = $this->registeredMyProfileComponents;
$components = collect($this->registeredMyProfileComponents)->filter(
fn (string $component) => $component::canView()
)->sortBy(
fn (string $component) => $component::getSort()
);

if ($this->shouldForceTwoFactor()){
$components = Arr::only($components,['two_factor_authentication']);
$components = $components->only(['two_factor_authentication']);
}
return collect($components)->all();
return $components->all();
}

public function passwordUpdateRules(array | Password $rules, bool $requiresCurrentPassword = true)
Expand Down
16 changes: 15 additions & 1 deletion src/Livewire/MyProfileComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyProfileComponent extends Component implements HasForms, HasActions
{
use InteractsWithForms, InteractsWithActions;

public $sort = 0;
public static $sort = 0;

function getName()
{
Expand All @@ -24,4 +24,18 @@ public function render()
return view($this->view);
}

public static function canView(): bool
{
return true;
}

public static function getSort(): int
{
return static::$sort;
}

public static function setSort(int $sort): void
{
static::$sort = $sort;
}
}
2 changes: 2 additions & 0 deletions src/Livewire/PersonalInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class PersonalInfo extends MyProfileComponent
public bool $hasAvatars;
public array $only = ['name','email'];

public static $sort = 10;

public function mount()
{
$this->user = Filament::getCurrentPanel()->auth()->user();
Expand Down
1 change: 1 addition & 0 deletions src/Livewire/SanctumTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SanctumTokens extends MyProfileComponent implements Tables\Contracts\HasTa
public $user;
public ?string $plainTextToken;

public static $sort = 40;

public function mount()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Livewire/TwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TwoFactorAuthentication extends MyProfileComponent
public $code;
public bool $showRecoveryCodes = false;

public static $sort = 30;

public function mount()
{
$this->user = Filament::getCurrentPanel()->auth()->user();
Expand Down
2 changes: 2 additions & 0 deletions src/Livewire/UpdatePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class UpdatePassword extends MyProfileComponent
public ?array $data = [];
public $user;

public static $sort = 20;

public function mount()
{
$this->user = Filament::getCurrentPanel()->auth()->user();
Expand Down

0 comments on commit 4e4841c

Please sign in to comment.