Skip to content

Commit

Permalink
feature: add abstract class and make permissions more dynamic based o…
Browse files Browse the repository at this point in the history
…n cap and role
  • Loading branch information
Jon Waldstein committed Aug 23, 2024
1 parent 7d4f0cd commit fbbe3d0
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 33 deletions.
19 changes: 19 additions & 0 deletions src/Framework/Permissions/Contracts/UserPermissionsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Give\Framework\Permissions\Contracts;

/**
* @unreleased
*/
interface UserPermissionsInterface
{
/**
* @return string
*/
public static function getType(): string;

/**
* @unreleased
*/
public function can(string $capability): bool;
}
16 changes: 16 additions & 0 deletions src/Framework/Permissions/DonationFormPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Give\Framework\Permissions;
/**
* @unreleased
*/
class DonationFormPermissions extends UserPermission
{
/**
* @unreleased
*/
public static function getType(): string
{
return 'give_form';
}
}
26 changes: 0 additions & 26 deletions src/Framework/Permissions/DonationFormsPermissions.php

This file was deleted.

17 changes: 17 additions & 0 deletions src/Framework/Permissions/DonationPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Give\Framework\Permissions;

/**
* @unreleased
*/
class DonationPermissions extends UserPermission
{
/**
* @unreleased
*/
public static function getType(): string
{
return 'give_payment';
}
}
17 changes: 17 additions & 0 deletions src/Framework/Permissions/DonorPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Give\Framework\Permissions;

/**
* @unreleased
*/
class DonorPermissions extends UserPermission
{
/**
* @unreleased
*/
public static function getType(): string
{
return 'give_payment';
}
}
8 changes: 6 additions & 2 deletions src/Framework/Permissions/Facades/UserPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Give\Framework\Permissions\Facades;

use Give\Framework\Permissions\DonationFormsPermissions;
use Give\Framework\Permissions\DonationFormPermissions;
use Give\Framework\Permissions\DonationPermissions;
use Give\Framework\Permissions\DonorPermissions;
use Give\Framework\Support\Facades\Facade;

/**
* @unreleased
*
* @method static DonationFormsPermissions donationForms()
* @method static DonationFormPermissions donationForms()
* @method static DonationPermissions donations()
* @method static DonorPermissions donors()
*/
class UserPermissions extends Facade
{
Expand Down
24 changes: 21 additions & 3 deletions src/Framework/Permissions/Facades/UserPermissionsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Give\Framework\Permissions\Facades;

use Give\Framework\Permissions\DonationFormsPermissions;
use Give\Framework\Permissions\DonationFormPermissions;
use Give\Framework\Permissions\DonationPermissions;
use Give\Framework\Permissions\DonorPermissions;

/**
* This is a facade for interacting with WP and GiveWP permissions.
Expand All @@ -16,8 +18,24 @@ class UserPermissionsFacade
/**
* @unreleased
*/
public function donationForms(): DonationFormsPermissions
public function donationForms(): DonationFormPermissions
{
return new DonationFormsPermissions();
return new DonationFormPermissions();
}

/**
* @unreleased
*/
public function donations(): DonationPermissions
{
return new DonationPermissions();
}

/**
* @unreleased
*/
public function donors(): DonorPermissions
{
return new DonorPermissions();
}
}
66 changes: 66 additions & 0 deletions src/Framework/Permissions/UserPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace Give\Framework\Permissions;

abstract class UserPermission implements Contracts\UserPermissionsInterface
{
/**
* @unreleased
*/
public function can(string $capability): bool
{
switch ($capability) {
case 'delete':
$capability = $this->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"
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +20,7 @@ final class TestUserPermissions extends TestCase
public function testDonationForms(): void
{
$this->assertInstanceOf(
DonationFormsPermissions::class,
DonationFormPermissions::class,
UserPermissions::donationForms()
);
}
Expand Down

0 comments on commit fbbe3d0

Please sign in to comment.