| 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 |
* @param \FluentSupport\Framework\Http\Request\Request $request |
| 16 |
* @return Boolean |
| 17 |
*/ |
| 18 |
public function verifyRequest(Request $request) |
| 19 |
{ |
| 20 |
return PermissionManager::currentUserCan('fst_sensitive_data'); |
| 21 |
} |
| 22 |
|
| 23 |
public function delete(Request $request) |
| 24 |
{ |
| 25 |
return $this->guardCustomerDeletion($request); |
| 26 |
} |
| 27 |
|
| 28 |
public function bulkDelete(Request $request) |
| 29 |
{ |
| 30 |
return $this->guardCustomerDeletion($request); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Deletion cascades into tickets, conversations and attachments. |
| 35 |
*/ |
| 36 |
protected function guardCustomerDeletion(Request $request) |
| 37 |
{ |
| 38 |
// The router runs either the per-route method or verifyRequest(), not both, |
| 39 |
// so re-apply the module boundary. The exception code becomes the HTTP status. |
| 40 |
if (!$this->verifyRequest($request)) { |
| 41 |
throw new \Exception( |
| 42 |
esc_html__('You do not have permission to manage customers.', 'fluent-support'), |
| 43 |
is_user_logged_in() ? 403 : 401 |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
// manage_options can't be granted by the plugin's own permissions. |
| 48 |
// Sites can drop this requirement. The boundary above still applies. |
| 49 |
$requiresAdmin = apply_filters('fluent_support/customer_delete_requires_admin', true); |
| 50 |
|
| 51 |
if ($requiresAdmin && !current_user_can('manage_options')) { |
| 52 |
throw new \Exception( |
| 53 |
esc_html__('Only administrators can delete customers.', 'fluent-support'), |
| 54 |
403 |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
} |
| 61 |
|