mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
Authorization hook implemented.
This commit is contained in:
@@ -14,6 +14,25 @@ use GraphQL\Error\UserError;
|
||||
* Class - Order_Mutation
|
||||
*/
|
||||
class Order_Mutation {
|
||||
/**
|
||||
* Filterable authentication function.
|
||||
*
|
||||
* @param string $mutation Mutation being executed.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function authorized( $mutation = 'create', $input, $context, $info ) {
|
||||
$post_type_object = get_post_type_object( 'shop_order' );
|
||||
|
||||
return apply_filters(
|
||||
"authorized_to_{$mutation}_orders",
|
||||
! current_user_can( $post_type_object->cap->create_posts ),
|
||||
$input,
|
||||
$context,
|
||||
$info
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an order.
|
||||
*
|
||||
|
||||
@@ -133,9 +133,7 @@ class Order_Create {
|
||||
*/
|
||||
public static function mutate_and_get_payload() {
|
||||
return function( $input, AppContext $context, ResolveInfo $info ) {
|
||||
$post_type_object = get_post_type_object( 'shop_order' );
|
||||
|
||||
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
|
||||
if ( Order_Mutation::authorized( 'create', $input, $context, $info ) ) {
|
||||
throw new UserError( __( 'User does not have the capabilities necessary to create an order.', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
|
||||
@@ -84,9 +84,7 @@ class Order_Delete {
|
||||
*/
|
||||
public static function mutate_and_get_payload() {
|
||||
return function( $input, AppContext $context, ResolveInfo $info ) {
|
||||
$post_type_object = get_post_type_object( 'shop_order' );
|
||||
|
||||
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
|
||||
if ( Order_Mutation::authorized( 'delete', $input, $context, $info ) ) {
|
||||
throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
|
||||
@@ -85,9 +85,7 @@ class Order_Update {
|
||||
*/
|
||||
public static function mutate_and_get_payload() {
|
||||
return function( $input, AppContext $context, ResolveInfo $info ) {
|
||||
$post_type_object = get_post_type_object( 'shop_order' );
|
||||
|
||||
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
|
||||
if ( Order_Mutation::authorized( 'update', $input, $context, $info ) ) {
|
||||
throw new UserError( __( 'User does not have the capabilities necessary to update an order.', 'wp-graphql-woocommerce' ) );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user