| 1 |
<?php |
| 2 |
|
| 3 |
namespace RestrictUserAccess\Module; |
| 4 |
|
| 5 |
use RestrictUserAccess\Hook\HookService; |
| 6 |
use RestrictUserAccess\Hook\HookSubscriberInterface; |
| 7 |
use RestrictUserAccess\Repository\SettingRepositoryInterface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class RestApiContentProtection |
| 11 |
* |
| 12 |
* @author Joachim Jensen <joachim@dev.institute> |
| 13 |
* @license https://www.gnu.org/licenses/gpl-3.0.html |
| 14 |
*/ |
| 15 |
class RestApiContentProtection implements HookSubscriberInterface |
| 16 |
{ |
| 17 |
/** @var SettingRepositoryInterface */ |
| 18 |
private $settingRepository; |
| 19 |
|
| 20 |
public function __construct( |
| 21 |
SettingRepositoryInterface $settingRepository |
| 22 |
) { |
| 23 |
$this->settingRepository = $settingRepository; |
| 24 |
} |
| 25 |
|
| 26 |
public function subscribe(HookService $service) |
| 27 |
{ |
| 28 |
$service->add_filter( |
| 29 |
'rest_authentication_errors', |
| 30 |
[$this, 'rest_api_access'] |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
public function rest_api_access($result) |
| 35 |
{ |
| 36 |
//bail if auth has been handled elsewhere |
| 37 |
if ($result === true || is_wp_error($result)) { |
| 38 |
return $result; |
| 39 |
} |
| 40 |
|
| 41 |
if (rua_get_user()->has_global_access()) { |
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
if (!$this->settingRepository->get_bool('rua_rest_api_access', true)) { |
| 46 |
return $result; |
| 47 |
} |
| 48 |
|
| 49 |
//Contributor is the lowest role that should have access, |
| 50 |
//since they can see content in admin area |
| 51 |
if (current_user_can('edit_posts')) { |
| 52 |
return $result; |
| 53 |
} |
| 54 |
|
| 55 |
$restricted = [ |
| 56 |
'/wp/v2/search' => true, |
| 57 |
'/wp/v2/users' => true |
| 58 |
]; |
| 59 |
|
| 60 |
$ignored_post_types = [ |
| 61 |
'nav_menu_item' => true, |
| 62 |
'wp_block' => true, |
| 63 |
'wp_template' => true, |
| 64 |
'wp_template_part' => true, |
| 65 |
'wp_navigation' => true |
| 66 |
]; |
| 67 |
foreach (get_post_types(['show_in_rest' => true], 'objects') as $post_type) { |
| 68 |
if (empty($post_type->rest_base)) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
if (isset($ignored_post_types[$post_type->name])) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
$restricted['/'.$post_type->rest_namespace.'/'.$post_type->rest_base] = true; |
| 75 |
} |
| 76 |
$ignored_taxonomies = [ |
| 77 |
'menu' => true, |
| 78 |
]; |
| 79 |
foreach (get_taxonomies(['show_in_rest' => true], 'objects') as $taxonomy) { |
| 80 |
if (empty($taxonomy->rest_base)) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
if (isset($ignored_taxonomies[$post_type->name])) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
$restricted['/'.$taxonomy->rest_namespace.'/'.$taxonomy->rest_base] = true; |
| 87 |
} |
| 88 |
|
| 89 |
global $wp; |
| 90 |
|
| 91 |
$route = $wp->query_vars['rest_route']; |
| 92 |
$route = preg_replace('/(\/\d+)$/', '', $route, 1); |
| 93 |
|
| 94 |
if (!isset($restricted[$route])) { |
| 95 |
return $result; |
| 96 |
} |
| 97 |
|
| 98 |
return new \WP_Error( |
| 99 |
'rest_forbidden', |
| 100 |
__('Sorry, you are not allowed to do that.'), |
| 101 |
['status' => rest_authorization_required_code()] |
| 102 |
); |
| 103 |
} |
| 104 |
} |
| 105 |
|