From 20edbb0e0c7502678b2ef5d3a40964f66fd99840 Mon Sep 17 00:00:00 2001 From: Boris Glumpler Date: Fri, 11 Aug 2023 12:25:04 +0200 Subject: [PATCH 1/4] Implemented basic profile component authorization --- src/BreezyCore.php | 9 ++++++--- src/Livewire/MyProfileComponent.php | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/BreezyCore.php b/src/BreezyCore.php index 8b22121..f2ac788 100644 --- a/src/BreezyCore.php +++ b/src/BreezyCore.php @@ -169,11 +169,14 @@ public function myProfileComponents(array $components) public function getRegisteredMyProfileComponents(): array { - $components = $this->registeredMyProfileComponents; + $components = collect($this->registeredMyProfileComponents)->filter( + fn (string $component) => $component::canView() + ); + 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) diff --git a/src/Livewire/MyProfileComponent.php b/src/Livewire/MyProfileComponent.php index 9c9bcdd..dc9fdc1 100644 --- a/src/Livewire/MyProfileComponent.php +++ b/src/Livewire/MyProfileComponent.php @@ -24,4 +24,8 @@ public function render() return view($this->view); } + public static function canView(): bool + { + return true; + } } From b7a9f2178540f779ec0fdb05588a842b13063d33 Mon Sep 17 00:00:00 2001 From: Boris Glumpler Date: Fri, 11 Aug 2023 15:42:56 +0200 Subject: [PATCH 2/4] Added ability to sort profile sections --- src/BreezyCore.php | 2 ++ src/Livewire/MyProfileComponent.php | 12 +++++++++++- src/Livewire/PersonalInfo.php | 2 ++ src/Livewire/SanctumTokens.php | 1 + src/Livewire/TwoFactorAuthentication.php | 2 ++ src/Livewire/UpdatePassword.php | 2 ++ 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/BreezyCore.php b/src/BreezyCore.php index f2ac788..84c39ae 100644 --- a/src/BreezyCore.php +++ b/src/BreezyCore.php @@ -171,6 +171,8 @@ public function getRegisteredMyProfileComponents(): array { $components = collect($this->registeredMyProfileComponents)->filter( fn (string $component) => $component::canView() + )->sortBy( + fn (string $component) => $component::sort() ); if ($this->shouldForceTwoFactor()){ diff --git a/src/Livewire/MyProfileComponent.php b/src/Livewire/MyProfileComponent.php index dc9fdc1..ac0c230 100644 --- a/src/Livewire/MyProfileComponent.php +++ b/src/Livewire/MyProfileComponent.php @@ -12,7 +12,7 @@ class MyProfileComponent extends Component implements HasForms, HasActions { use InteractsWithForms, InteractsWithActions; - public $sort = 0; + public static $sort = 0; function getName() { @@ -28,4 +28,14 @@ public static function canView(): bool { return true; } + + public static function sort(): int + { + return static::$sort; + } + + public static function setSort(int $sort): void + { + static::$sort = $sort; + } } diff --git a/src/Livewire/PersonalInfo.php b/src/Livewire/PersonalInfo.php index 0076fee..7b258f4 100644 --- a/src/Livewire/PersonalInfo.php +++ b/src/Livewire/PersonalInfo.php @@ -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(); diff --git a/src/Livewire/SanctumTokens.php b/src/Livewire/SanctumTokens.php index 4e823c7..071f31c 100644 --- a/src/Livewire/SanctumTokens.php +++ b/src/Livewire/SanctumTokens.php @@ -20,6 +20,7 @@ class SanctumTokens extends MyProfileComponent implements Tables\Contracts\HasTa public $user; public ?string $plainTextToken; + public static $sort = 40; public function mount() { diff --git a/src/Livewire/TwoFactorAuthentication.php b/src/Livewire/TwoFactorAuthentication.php index fadd8ed..a182d2b 100644 --- a/src/Livewire/TwoFactorAuthentication.php +++ b/src/Livewire/TwoFactorAuthentication.php @@ -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(); diff --git a/src/Livewire/UpdatePassword.php b/src/Livewire/UpdatePassword.php index ee89c04..1967dd7 100644 --- a/src/Livewire/UpdatePassword.php +++ b/src/Livewire/UpdatePassword.php @@ -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(); From 791f850243965085f6c18f7cb2f2d0bc7e472dba Mon Sep 17 00:00:00 2001 From: Boris Glumpler Date: Fri, 11 Aug 2023 16:50:41 +0200 Subject: [PATCH 3/4] Added a note about sorting in the readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 5e5e56c..e5d26e7 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,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 From a614f34c66d03c55d2af2d03fbfd9d5975b5fa8f Mon Sep 17 00:00:00 2001 From: Boris Glumpler Date: Thu, 24 Aug 2023 09:15:03 +0200 Subject: [PATCH 4/4] Changed MyProfileComponent::sort() to MyProfileComponent::getSort() --- src/BreezyCore.php | 2 +- src/Livewire/MyProfileComponent.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BreezyCore.php b/src/BreezyCore.php index bf2f5c9..24d7813 100644 --- a/src/BreezyCore.php +++ b/src/BreezyCore.php @@ -174,7 +174,7 @@ public function getRegisteredMyProfileComponents(): array $components = collect($this->registeredMyProfileComponents)->filter( fn (string $component) => $component::canView() )->sortBy( - fn (string $component) => $component::sort() + fn (string $component) => $component::getSort() ); if ($this->shouldForceTwoFactor()){ diff --git a/src/Livewire/MyProfileComponent.php b/src/Livewire/MyProfileComponent.php index ac0c230..d14f9be 100644 --- a/src/Livewire/MyProfileComponent.php +++ b/src/Livewire/MyProfileComponent.php @@ -29,7 +29,7 @@ public static function canView(): bool return true; } - public static function sort(): int + public static function getSort(): int { return static::$sort; }