| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Acl; |
| 4 |
|
| 5 |
class Acl |
| 6 |
{ |
| 7 |
|
| 8 |
public static function getPermissionSet() |
| 9 |
{ |
| 10 |
return apply_filters('fluentform_permission_set', [ |
| 11 |
'fluentform_dashboard_access', |
| 12 |
'fluentform_forms_manager', |
| 13 |
'fluentform_entries_viewer', |
| 14 |
'fluentform_settings_manager', |
| 15 |
'fluentform_full_access', |
| 16 |
]); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Fluentform access controll permissions assignment. |
| 21 |
*/ |
| 22 |
public static function setPermissions() |
| 23 |
{ |
| 24 |
// Fire an event letting others know that fluentform |
| 25 |
// is going to assign permission set to a role. |
| 26 |
do_action('before_fluentform_permission_set_assignment'); |
| 27 |
|
| 28 |
// The permissions that fluentform supports altogether. |
| 29 |
$permissions = self::getPermissionSet(); |
| 30 |
|
| 31 |
// The role that fluentform will use |
| 32 |
// to attach the permission set. |
| 33 |
$role = get_role('administrator'); |
| 34 |
|
| 35 |
// Looping through permission set to add to the role. |
| 36 |
foreach ($permissions as $permission) { |
| 37 |
$role->add_cap($permission); |
| 38 |
} |
| 39 |
|
| 40 |
// Fire an event letting others know that fluentform is |
| 41 |
// done with the permission assignment to the role. |
| 42 |
do_action('after_fluentform_permission_set_assignment'); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Verify if current user has a fluentform permission. |
| 47 |
* |
| 48 |
* @param $permission |
| 49 |
* @param null $formId |
| 50 |
* @param string $message |
| 51 |
* @param bool $json |
| 52 |
* |
| 53 |
* @throws \Exception |
| 54 |
*/ |
| 55 |
public static function verify( |
| 56 |
$permission, |
| 57 |
$formId = null, |
| 58 |
$message = 'You do not have permission to perform this action.', |
| 59 |
$json = true |
| 60 |
) { |
| 61 |
$allowed = self::hasPermission($permission, $formId); |
| 62 |
|
| 63 |
if (! $allowed) { |
| 64 |
if ($json) { |
| 65 |
wp_send_json_error([ |
| 66 |
'message' => $message |
| 67 |
], 423); |
| 68 |
} else { |
| 69 |
throw new \Exception($message); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public static function hasPermission($permission, $formId = false) |
| 75 |
{ |
| 76 |
$allowed = current_user_can('fluentform_full_access') ?: current_user_can($permission); |
| 77 |
return apply_filters('fluentform_verify_user_permission_'.$permission, $allowed, $formId); |
| 78 |
} |
| 79 |
|
| 80 |
public static function hasAnyFormPermission($form_id = false ) |
| 81 |
{ |
| 82 |
$allPermissions = self::getPermissionSet(); |
| 83 |
foreach ($allPermissions as $permission) { |
| 84 |
if(self::hasPermission($permission, $form_id)) { |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |