| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Acl; |
| 4 |
|
| 5 |
class RoleManager |
| 6 |
{ |
| 7 |
public function getRoles() |
| 8 |
{ |
| 9 |
if (!current_user_can('manage_options')) { |
| 10 |
wp_send_json_success([ |
| 11 |
'capability' => [], |
| 12 |
'roles' => [], |
| 13 |
], 200); |
| 14 |
} |
| 15 |
|
| 16 |
$roles = \get_editable_roles(); |
| 17 |
$formatted = []; |
| 18 |
foreach ($roles as $key => $role) { |
| 19 |
if ('administrator' == $key) { |
| 20 |
continue; |
| 21 |
} |
| 22 |
if ('subscriber' != $key) { |
| 23 |
$formatted[] = [ |
| 24 |
'name' => $role['name'], |
| 25 |
'key' => $key, |
| 26 |
]; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
$capability = get_option('_fluentform_form_permission'); |
| 31 |
|
| 32 |
if (is_string($capability)) { |
| 33 |
$capability = []; |
| 34 |
} |
| 35 |
|
| 36 |
wp_send_json_success([ |
| 37 |
'capability' => $capability, |
| 38 |
'roles' => $formatted, |
| 39 |
], 200); |
| 40 |
} |
| 41 |
|
| 42 |
public function setRoles() |
| 43 |
{ |
| 44 |
if (current_user_can('manage_options')) { |
| 45 |
$capability = wp_unslash(wpFluentForm('request')->get('capability', [])); |
| 46 |
|
| 47 |
foreach ($capability as $item) { |
| 48 |
if ('subscriber' == strtolower($item)) { |
| 49 |
wp_send_json_error([ |
| 50 |
'message' => __('Sorry, you can not give access to the Subscriber role.', 'fluentform'), |
| 51 |
], 423); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
update_option('_fluentform_form_permission', $capability, 'no'); |
| 56 |
wp_send_json_success([ |
| 57 |
'message' => __('Successfully saved the role(s).', 'fluentform'), |
| 58 |
], 200); |
| 59 |
} else { |
| 60 |
wp_send_json_error([ |
| 61 |
'message' => __('Sorry, You can not update permissions. Only administrators can update permissions', 'fluentform'), |
| 62 |
], 423); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public function verifyPermissionSet() |
| 67 |
{ |
| 68 |
$availablePermissions = Acl::getPermissionSet(); |
| 69 |
$currentCapability = $this->currentUserFormFormCapability(); |
| 70 |
if (!$currentCapability) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Give permission to internal issues |
| 75 |
foreach ($availablePermissions as $permission) { |
| 76 |
add_filter('fluentform_verify_user_permission_' . $permission, function ($allowed) { |
| 77 |
return true; |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
// Give permission to menu items |
| 82 |
add_filter('fluentform_dashboard_capability', function ($capability) use ($currentCapability) { |
| 83 |
return $currentCapability; |
| 84 |
}); |
| 85 |
|
| 86 |
add_filter('fluentform_settings_capability', function ($capability) use ($currentCapability) { |
| 87 |
return $currentCapability; |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
public function currentUserFormFormCapability() |
| 92 |
{ |
| 93 |
if (current_user_can('manage_options')) { |
| 94 |
return 'manage_options'; |
| 95 |
} |
| 96 |
if (!is_user_logged_in()) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$capabilities = get_option('_fluentform_form_permission'); |
| 101 |
if (is_string($capabilities)) { |
| 102 |
$capabilities = (array) $capabilities; |
| 103 |
} |
| 104 |
if (!$capabilities) { |
| 105 |
return; |
| 106 |
} |
| 107 |
foreach ($capabilities as $capability) { |
| 108 |
if (current_user_can($capability)) { |
| 109 |
return $capability; |
| 110 |
} |
| 111 |
} |
| 112 |
return false; |
| 113 |
} |
| 114 |
} |
| 115 |
|