From fbbe3d00ffb312938b51650e34eb757a413b1e21 Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Fri, 23 Aug 2024 11:26:06 -0400 Subject: [PATCH] feature: add abstract class and make permissions more dynamic based on cap and role --- .../Contracts/UserPermissionsInterface.php | 19 ++++++ .../Permissions/DonationFormPermissions.php | 16 +++++ .../Permissions/DonationFormsPermissions.php | 26 -------- .../Permissions/DonationPermissions.php | 17 +++++ .../Permissions/DonorPermissions.php | 17 +++++ .../Permissions/Facades/UserPermissions.php | 8 ++- .../Facades/UserPermissionsFacade.php | 24 ++++++- src/Framework/Permissions/UserPermission.php | 66 +++++++++++++++++++ .../Facades/TestUserPermissions.php | 4 +- 9 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 src/Framework/Permissions/Contracts/UserPermissionsInterface.php create mode 100644 src/Framework/Permissions/DonationFormPermissions.php delete mode 100644 src/Framework/Permissions/DonationFormsPermissions.php create mode 100644 src/Framework/Permissions/DonationPermissions.php create mode 100644 src/Framework/Permissions/DonorPermissions.php create mode 100644 src/Framework/Permissions/UserPermission.php diff --git a/src/Framework/Permissions/Contracts/UserPermissionsInterface.php b/src/Framework/Permissions/Contracts/UserPermissionsInterface.php new file mode 100644 index 0000000000..d4449f6db6 --- /dev/null +++ b/src/Framework/Permissions/Contracts/UserPermissionsInterface.php @@ -0,0 +1,19 @@ +getCapability('delete'); + break; + case 'read': + case 'view': + case 'create': + case 'update': + case 'edit': + $capability = $this->getCapability('edit'); + break; + } + + return current_user_can($capability); + } + + /** + * @unreleased + */ + protected function getCapability( string $cap): string + { + $caps = $this->getCapabilities($this::getType()); + + return $caps[$cap]; + } + + /** + * @unreleased + */ + protected function getCapabilities(string $type): array + { + return [ + // Post type. + "edit" => "edit_{$type}s", + "edit_others" => "edit_others_{$type}s", + "publish" => "publish_{$type}s", + "read_private" => "read_private_{$type}s", + "delete" => "delete_{$type}s", + "delete_private" => "delete_private_{$type}s", + "delete_published" => "delete_published_{$type}s", + "delete_others" => "delete_others_{$type}s", + "edit_private" => "edit_private_{$type}s", + "edit_published" => "edit_published_{$type}s", + + // Terms / taxonomies. + "manage_terms" => "manage_{$type}_terms", + "edit_terms" => "edit_{$type}_terms", + "delete_terms" => "delete_{$type}_terms", + "assign_terms" => "assign_{$type}_terms", + + // Custom capabilities. + "view_stats" => "view_{$type}_stats", + "import" => "import_{$type}s" + ]; + } +} diff --git a/tests/Unit/Framework/Permissions/Facades/TestUserPermissions.php b/tests/Unit/Framework/Permissions/Facades/TestUserPermissions.php index 7c35def0f0..0e4ab19938 100644 --- a/tests/Unit/Framework/Permissions/Facades/TestUserPermissions.php +++ b/tests/Unit/Framework/Permissions/Facades/TestUserPermissions.php @@ -2,7 +2,7 @@ namespace Give\Tests\Unit\Framework\Permissions\Facades; -use Give\Framework\Permissions\DonationFormsPermissions; +use Give\Framework\Permissions\DonationFormPermissions; use Give\Framework\Permissions\Facades\UserPermissions; use Give\Tests\TestCase; use Give\Tests\TestTraits\RefreshDatabase; @@ -20,7 +20,7 @@ final class TestUserPermissions extends TestCase public function testDonationForms(): void { $this->assertInstanceOf( - DonationFormsPermissions::class, + DonationFormPermissions::class, UserPermissions::donationForms() ); }