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 @@ +can('edit')) { + throw new Exception('You do not have permission to edit donation forms.'); +} +``` +}; +``` diff --git a/src/Framework/Permissions/UserPermission.php b/src/Framework/Permissions/UserPermission.php new file mode 100644 index 0000000000..fc367afdac --- /dev/null +++ b/src/Framework/Permissions/UserPermission.php @@ -0,0 +1,66 @@ +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 new file mode 100644 index 0000000000..0e4ab19938 --- /dev/null +++ b/tests/Unit/Framework/Permissions/Facades/TestUserPermissions.php @@ -0,0 +1,28 @@ +assertInstanceOf( + DonationFormPermissions::class, + UserPermissions::donationForms() + ); + } + +} diff --git a/tests/Unit/Framework/Permissions/TestDonationFormsPermissions.php b/tests/Unit/Framework/Permissions/TestDonationFormsPermissions.php new file mode 100644 index 0000000000..0b788d54f7 --- /dev/null +++ b/tests/Unit/Framework/Permissions/TestDonationFormsPermissions.php @@ -0,0 +1,93 @@ +user->create_and_get(); + $user->add_role($role); + + wp_set_current_user($user->ID); + + if ($shouldPass) { + $this->assertTrue( + UserPermissions::donationForms()->can($capability) + ); + } else { + $this->assertFalse( + UserPermissions::donationForms()->can($capability) + ); + } + } + + + /** + * @unreleased + * + * @return array> + */ + public function canProvider(): array + { + return [ + // true + ['give_worker', 'create', true], + ['give_worker', 'read', true], + ['give_worker', 'update', true], + ['give_worker', 'edit', true], + + ['give_manager', 'create', true], + ['give_manager', 'read', true], + ['give_manager', 'update', true], + ['give_manager', 'edit', true], + ['give_manager', 'delete', true], + + ['give_accountant', 'create', true], + ['give_accountant', 'read', true], + ['give_accountant', 'update', true], + ['give_accountant', 'edit', true], + ['give_accountant', 'read_private_give_forms', true], + + ['administrator', 'create', true], + ['administrator', 'read', true], + ['administrator', 'update', true], + ['administrator', 'edit', true], + ['administrator', 'delete', true], + + // false + ['give_accountant', 'delete', false], + + ['give_donor', 'create', false], + ['give_donor', 'read', false], + ['give_donor', 'update', false], + ['give_donor', 'edit', false], + ['give_donor', 'delete', false], + + ['give_subscriber', 'create', false], + ['give_subscriber', 'read', false], + ['give_subscriber', 'update', false], + ['give_subscriber', 'edit', false], + ['give_subscriber', 'delete', false], + + ['subscriber', 'create', false], + ['subscriber', 'read', false], + ['subscriber', 'update', false], + ['subscriber', 'edit', false], + ['subscriber', 'delete', false], + ]; + } +}