| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Policies; |
| 4 |
|
| 5 |
use FluentSupport\App\Modules\PermissionManager; |
| 6 |
use FluentSupport\Framework\Http\Request\Request; |
| 7 |
use FluentSupport\Framework\Foundation\Policy; |
| 8 |
|
| 9 |
/** |
| 10 |
* Policy for /customers: reads keep the module boundary, deletes are gated below. |
| 11 |
*/ |
| 12 |
class CustomerPolicy extends Policy |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Check user permission for any method |
| 16 |
* @param \FluentSupport\Framework\Http\Request\Request $request |
| 17 |
* @return Boolean |
| 18 |
*/ |
| 19 |
public function verifyRequest(Request $request) |
| 20 |
{ |
| 21 |
return PermissionManager::currentUserCan('fst_sensitive_data'); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Deleting a single customer. |
| 26 |
*/ |
| 27 |
public function delete(Request $request) |
| 28 |
{ |
| 29 |
return $this->guardCustomerDeletion($request); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Deleting multiple customers in one request. |
| 34 |
*/ |
| 35 |
public function bulkDelete(Request $request) |
| 36 |
{ |
| 37 |
return $this->guardCustomerDeletion($request); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Deletion cascades into tickets, conversations and attachments. |
| 42 |
*/ |
| 43 |
protected function guardCustomerDeletion(Request $request) |
| 44 |
{ |
| 45 |
// The router runs EITHER the per-route method OR verifyRequest(), never both |
| 46 |
// (FoundationTrait.php:120-122), so re-apply the module boundary here. |
| 47 |
// The exception code is carried through as the HTTP status by |
| 48 |
// Route::permissionCallback(), which falls back to 403 when the code is 0. |
| 49 |
// Pass it explicitly so an unauthenticated caller gets the canonical 401. |
| 50 |
if (!$this->verifyRequest($request)) { |
| 51 |
throw new \Exception( |
| 52 |
esc_html__('You do not have permission to manage customers.', 'fluent-support'), |
| 53 |
is_user_logged_in() ? 403 : 401 |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
// By default this also needs manage_options, a capability the plugin cannot mint. |
| 58 |
// Sites can drop that requirement with the filter below; the boundary above still |
| 59 |
// applies either way. Throwing gives a specific 403. |
| 60 |
$requiresAdmin = apply_filters('fluent_support/customer_delete_requires_admin', true); |
| 61 |
|
| 62 |
if ($requiresAdmin && !current_user_can('manage_options')) { |
| 63 |
// Only reachable by a logged-in user who already cleared the module |
| 64 |
// boundary above, so 403 is always the right status here. |
| 65 |
throw new \Exception( |
| 66 |
esc_html__('Only administrators can delete customers.', 'fluent-support'), |
| 67 |
403 |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
|