1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Permissions. |
4
|
|
|
* |
5
|
|
|
* Handles permission checks for endpoints. |
6
|
|
|
* |
7
|
|
|
* @package WooCommerce/RestApi |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4\Utilities; |
11
|
|
|
|
12
|
|
|
defined( 'ABSPATH' ) || exit; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Permissions class. |
16
|
|
|
*/ |
17
|
|
|
class Permissions { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resource permissions required. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected static $resource_permissions = [ |
25
|
|
|
'settings' => 'manage_woocommerce', |
26
|
|
|
'system_status' => 'manage_woocommerce', |
27
|
|
|
'attributes' => 'manage_product_terms', |
28
|
|
|
'shipping_methods' => 'manage_woocommerce', |
29
|
|
|
'payment_gateways' => 'manage_woocommerce', |
30
|
|
|
'webhooks' => 'manage_woocommerce', |
31
|
|
|
'product_reviews' => 'moderate_comments', |
32
|
|
|
'customers' => [ |
33
|
|
|
'read' => 'list_users', |
34
|
|
|
'create' => 'promote_users', // Check if current user can create users, shop managers are not allowed to create users. |
35
|
|
|
'edit' => 'edit_users', |
36
|
|
|
'delete' => 'delete_users', |
37
|
|
|
'batch' => 'promote_users', |
38
|
|
|
], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* See if the current user can do something to a resource. |
43
|
|
|
* |
44
|
|
|
* @param string $resource Type of permission required. |
45
|
|
|
* @param string $context Context. One of read, edit, create, update, delete, batch. |
46
|
|
|
* @param int $resource_id Optional resource ID. |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public static function check_resource( $resource, $context = 'read', $resource_id = 0 ) { |
50
|
|
|
if ( ! isset( self::$resource_permissions[ $resource ] ) ) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
$permissions = self::$resource_permissions[ $resource ]; |
54
|
|
|
$capability = is_array( $permissions ) ? $permissions[ $context ] : $permissions; |
55
|
|
|
$permission = current_user_can( $capability ); |
56
|
|
|
|
57
|
|
|
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $resource_id, $resource ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* See if the current user can do something to a resource. |
62
|
|
|
* |
63
|
|
|
* @param string $taxonomy Taxonomy name. |
64
|
|
|
* @param string $context Context. One of read, edit, create, update, delete, batch. |
65
|
|
|
* @param int $object_id Optional object ID. |
66
|
|
|
* @return boolean |
67
|
|
|
*/ |
68
|
|
|
public static function check_taxonomy( $taxonomy, $context = 'read', $object_id = 0 ) { |
69
|
|
|
$contexts = array( |
70
|
|
|
'read' => 'manage_terms', |
71
|
|
|
'create' => 'edit_terms', |
72
|
|
|
'edit' => 'edit_terms', |
73
|
|
|
'delete' => 'delete_terms', |
74
|
|
|
'batch' => 'edit_terms', |
75
|
|
|
); |
76
|
|
|
$cap = $contexts[ $context ]; |
77
|
|
|
$taxonomy_object = get_taxonomy( $taxonomy ); |
78
|
|
|
$permission = current_user_can( $taxonomy_object->cap->$cap, $object_id ); |
79
|
|
|
|
80
|
|
|
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, $taxonomy ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check permissions of posts on REST API. |
85
|
|
|
* |
86
|
|
|
* @param string $post_type Post type. |
87
|
|
|
* @param string $context Request context. |
88
|
|
|
* @param int $object_id Post ID. |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public static function check_post_object( $post_type, $context = 'read', $object_id = 0 ) { |
92
|
|
|
$contexts = array( |
93
|
|
|
'read' => 'read_private_posts', |
94
|
|
|
'create' => 'publish_posts', |
95
|
|
|
'edit' => 'edit_post', |
96
|
|
|
'delete' => 'delete_post', |
97
|
|
|
'batch' => 'edit_others_posts', |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
if ( 'revision' === $post_type ) { |
101
|
|
|
$permission = false; |
102
|
|
|
} else { |
103
|
|
|
$cap = $contexts[ $context ]; |
104
|
|
|
$post_type_object = get_post_type_object( $post_type ); |
105
|
|
|
$permission = current_user_can( $post_type_object->cap->$cap, $object_id ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return apply_filters( 'woocommerce_rest_check_permissions', $permission, $context, $object_id, $post_type ); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|