Contracts
5 months ago
Facades
5 months ago
Migrations
5 months ago
Traits
5 months ago
CampaignPermissions.php
5 months ago
DonationFormPermissions.php
5 months ago
DonationPermissions.php
5 months ago
DonorPermissions.php
5 months ago
EventPermissions.php
5 months ago
ReportsPermissions.php
5 months ago
SensitiveDataPermissions.php
5 months ago
SettingsPermissions.php
5 months ago
SubscriptionPermissions.php
5 months ago
UserPermission.php
5 months ago
UserPermission.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Permissions; |
| 4 | |
| 5 | use Give\Framework\Permissions\Traits\WithAdminAccess; |
| 6 | |
| 7 | abstract class UserPermission implements Contracts\UserPermissionsInterface |
| 8 | { |
| 9 | use WithAdminAccess; |
| 10 | |
| 11 | /** |
| 12 | * Check if user can create (maps to edit capability). |
| 13 | * |
| 14 | * @since 4.14.0 |
| 15 | */ |
| 16 | public function canCreate(): bool |
| 17 | { |
| 18 | return $this->currentUserCan('edit'); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Check if user can view/read. |
| 23 | * |
| 24 | * @since 4.14.0 |
| 25 | */ |
| 26 | public function canView(): bool |
| 27 | { |
| 28 | return $this->currentUserCan('view'); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Check if user can edit. |
| 33 | * |
| 34 | * @since 4.14.0 |
| 35 | */ |
| 36 | public function canEdit(): bool |
| 37 | { |
| 38 | return $this->currentUserCan('edit'); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check if user can delete. |
| 43 | * |
| 44 | * @since 4.14.0 |
| 45 | */ |
| 46 | public function canDelete(): bool |
| 47 | { |
| 48 | return $this->currentUserCan('delete'); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Check if the current user has the specified capability. |
| 53 | * |
| 54 | * @since 4.14.0 |
| 55 | */ |
| 56 | protected function currentUserCan(string $capability): bool |
| 57 | { |
| 58 | if ($this->isAdmin()) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return current_user_can($this->getCapability($capability)); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the user capability string for the given capability type. |
| 67 | * |
| 68 | * @since 4.14.0 |
| 69 | */ |
| 70 | public function getCapability(string $cap): string |
| 71 | { |
| 72 | $caps = $this->getCapabilities($this::getType()); |
| 73 | |
| 74 | return $caps[$cap] ?? ''; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @since 4.14.0 |
| 79 | */ |
| 80 | public function viewCap(): string |
| 81 | { |
| 82 | return $this->getCapability('view'); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @since 4.14.0 |
| 87 | */ |
| 88 | public function editCap(): string |
| 89 | { |
| 90 | return $this->getCapability('edit'); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 4.14.0 |
| 95 | */ |
| 96 | public function deleteCap(): string |
| 97 | { |
| 98 | return $this->getCapability('delete'); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @since 4.14.0 |
| 103 | */ |
| 104 | protected function getCapabilities(string $type): array |
| 105 | { |
| 106 | return [ |
| 107 | // Post type. |
| 108 | "edit" => "edit_{$type}s", |
| 109 | "edit_others" => "edit_others_{$type}s", |
| 110 | "publish" => "publish_{$type}s", |
| 111 | "read_private" => "read_private_{$type}s", |
| 112 | "delete" => "delete_{$type}s", |
| 113 | "delete_private" => "delete_private_{$type}s", |
| 114 | "delete_published" => "delete_published_{$type}s", |
| 115 | "delete_others" => "delete_others_{$type}s", |
| 116 | "edit_private" => "edit_private_{$type}s", |
| 117 | "edit_published" => "edit_published_{$type}s", |
| 118 | "view" => "view_{$type}s", |
| 119 | |
| 120 | // Terms / taxonomies. |
| 121 | "manage_terms" => "manage_{$type}_terms", |
| 122 | "edit_terms" => "edit_{$type}_terms", |
| 123 | "delete_terms" => "delete_{$type}_terms", |
| 124 | "assign_terms" => "assign_{$type}_terms", |
| 125 | |
| 126 | // Custom capabilities. |
| 127 | "view_stats" => "view_{$type}_stats", |
| 128 | "import" => "import_{$type}s" |
| 129 | ]; |
| 130 | } |
| 131 | } |
| 132 |