-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add abstract class and make permissions more dynamic based o…
…n cap and role
- Loading branch information
Jon Waldstein
committed
Aug 23, 2024
1 parent
7d4f0cd
commit fbbe3d0
Showing
9 changed files
with
164 additions
and
33 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/Framework/Permissions/Contracts/UserPermissionsInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters