Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default the resolved customer to the current user #787

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions includes/type/object/class-root-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ public static function register_fields() {
],
],
'resolve' => static function ( $source, array $args, AppContext $context ) {
$customer_id = 0;
$current_user_id = get_current_user_id();

// Default the customer to the current user.
$customer_id = $current_user_id;

// If a customer ID has been provided, resolve to that ID instead.
if ( ! empty( $args['id'] ) ) {
$id_components = Relay::fromGlobalId( $args['id'] );
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
Expand All @@ -172,17 +177,20 @@ public static function register_fields() {
$customer_id = absint( $args['customerId'] );
}

$authorized = ! empty( $customer_id )
// If a user does not have the ability to list users, they can only view their own customer object.
$unauthorized = ! empty( $customer_id )
&& ! current_user_can( 'list_users' )
&& get_current_user_id() !== $customer_id;
if ( $authorized ) {
&& $current_user_id !== $customer_id;
if ( $unauthorized ) {
throw new UserError( __( 'Not authorized to access this customer', 'wp-graphql-woocommerce' ) );
}

// If we have a customer ID, resolve to that customer.
if ( $customer_id ) {
return Factory::resolve_customer( $customer_id, $context );
}

// Resolve to the session customer.
return Factory::resolve_session_customer();
},
],
Expand Down
Loading