99namespace WPGraphQL \WooCommerce \Data \Mutation ;
1010
1111use GraphQL \Error \UserError ;
12+ use WPGraphQL \Utils \Utils ;
1213
1314
1415/**
@@ -22,8 +23,9 @@ class Order_Mutation {
2223 * @param \WPGraphQL\AppContext $context AppContext instance.
2324 * @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo instance.
2425 * @param string $mutation Mutation being executed.
25- * @param integer|null $order_id Order ID.
26- *
26+ * @param integer|null|false $order_id Order ID.
27+ * @throws \GraphQL\Error\UserError Error locating order.
28+ *
2729 * @return boolean
2830 */
2931 public static function authorized ( $ input , $ context , $ info , $ mutation = 'create ' , $ order_id = null ) {
@@ -34,18 +36,38 @@ public static function authorized( $input, $context, $info, $mutation = 'create'
3436 */
3537 $ post_type_object = get_post_type_object ( 'shop_order ' );
3638
37- return apply_filters (
38- "graphql_woocommerce_authorized_to_ {$ mutation }_orders " ,
39- current_user_can (
40- 'delete ' === $ mutation
41- ? $ post_type_object ->cap ->delete_posts
42- : $ post_type_object ->cap ->edit_posts
43- ),
44- $ order_id ,
45- $ input ,
46- $ context ,
47- $ info
48- );
39+ if ( ! $ order_id ) {
40+ return apply_filters (
41+ "graphql_woocommerce_authorized_to_ {$ mutation }_orders " ,
42+ current_user_can ( $ post_type_object ->cap ->edit_posts ),
43+ $ order_id ,
44+ $ input ,
45+ $ context ,
46+ $ info
47+ );
48+ }
49+
50+ /** @var false|\WC_Order $order */
51+ $ order = \wc_get_order ( $ order_id );
52+ if ( false === $ order ) {
53+ throw new UserError (
54+ sprintf (
55+ /* translators: %d: Order ID */
56+ __ ( 'Failed to find order with ID of %d. ' , 'wp-graphql-woocommerce ' ),
57+ $ order_id
58+ )
59+ );
60+ }
61+
62+ $ post_type = get_post_type ( $ order_id );
63+ if ( false === $ post_type ) {
64+ throw new UserError ( __ ( 'Failed to identify the post type of the order. ' , 'wp-graphql-woocommerce ' ) );
65+ }
66+
67+ // Return true if user is owner or admin.
68+ $ is_owner = 0 !== get_current_user_id () && $ order ->get_customer_id () === get_current_user_id ();
69+ $ is_admin = \wc_rest_check_post_permissions ( $ post_type , 'edit ' , $ order_id );
70+ return $ is_owner || $ is_admin ;
4971 }
5072
5173 /**
@@ -565,25 +587,26 @@ public static function apply_coupons( $order_id, $coupons ) {
565587 /**
566588 * Validates order customer
567589 *
568- * @param array $input Input data describing order.
590+ * @param string $customer_id ID of customer for order.
569591 *
570592 * @return bool
571593 */
572- public static function validate_customer ( $ input ) {
573- if ( ! empty ( $ input ['customerId ' ] ) ) {
574- // Make sure customer exists.
575- if ( false === get_user_by ( 'id ' , $ input ['customerId ' ] ) ) {
576- return false ;
577- }
578- // Make sure customer is part of blog.
579- if ( is_multisite () && ! is_user_member_of_blog ( $ input ['customerId ' ] ) ) {
580- add_user_to_blog ( get_current_blog_id (), $ input ['customerId ' ], 'customer ' );
581- }
594+ public static function validate_customer ( $ customer_id ) {
595+ $ id = Utils::get_database_id_from_id ( $ customer_id );
596+ if ( ! $ id ) {
597+ return false ;
598+ }
582599
583- return true ;
600+ if ( false === get_user_by ( 'id ' , $ id ) ) {
601+ return false ;
584602 }
585603
586- return false ;
604+ // Make sure customer is part of blog.
605+ if ( is_multisite () && ! is_user_member_of_blog ( $ id ) ) {
606+ add_user_to_blog ( get_current_blog_id (), $ id , 'customer ' );
607+ }
608+
609+ return true ;
587610 }
588611
589612 /**
0 commit comments