@@ -160,7 +160,12 @@ public static function register_fields() {
160160 ],
161161 ],
162162 'resolve ' => static function ( $ source , array $ args , AppContext $ context ) {
163- $ customer_id = 0 ;
163+ $ current_user_id = get_current_user_id ();
164+
165+ // Default the customer to the current user.
166+ $ customer_id = $ current_user_id ;
167+
168+ // If a customer ID has been provided, resolve to that ID instead.
164169 if ( ! empty ( $ args ['id ' ] ) ) {
165170 $ id_components = Relay::fromGlobalId ( $ args ['id ' ] );
166171 if ( ! isset ( $ id_components ['id ' ] ) || ! absint ( $ id_components ['id ' ] ) ) {
@@ -172,17 +177,20 @@ public static function register_fields() {
172177 $ customer_id = absint ( $ args ['customerId ' ] );
173178 }
174179
175- $ authorized = ! empty ( $ customer_id )
180+ // If a user does not have the ability to list users, they can only view their own customer object.
181+ $ unauthorized = ! empty ( $ customer_id )
176182 && ! current_user_can ( 'list_users ' )
177- && get_current_user_id () !== $ customer_id ;
178- if ( $ authorized ) {
183+ && $ current_user_id !== $ customer_id ;
184+ if ( $ unauthorized ) {
179185 throw new UserError ( __ ( 'Not authorized to access this customer ' , 'wp-graphql-woocommerce ' ) );
180186 }
181187
188+ // If we have a customer ID, resolve to that customer.
182189 if ( $ customer_id ) {
183190 return Factory::resolve_customer ( $ customer_id , $ context );
184191 }
185192
193+ // Resolve to the session customer.
186194 return Factory::resolve_session_customer ();
187195 },
188196 ],
0 commit comments