generated from pascalmartineau/wp-skeleton
refactor: rename userSwitchTo
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m6s
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m6s
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
<?php
|
||||
|
||||
// Register userSwitchTo mutation
|
||||
add_action( 'graphql_register_types', 'ccat_graphql_register_user_switch_to' );
|
||||
function ccat_graphql_register_user_switch_to() {
|
||||
// Register switchTo mutation
|
||||
add_action( 'graphql_register_types', 'ccat_graphql_register_switch_to' );
|
||||
function ccat_graphql_register_switch_to() {
|
||||
register_graphql_mutation(
|
||||
'userSwitchTo',
|
||||
'switchTo',
|
||||
array(
|
||||
'inputFields' => array(
|
||||
'userId' => array(
|
||||
'type' => 'ID',
|
||||
'description' => esc_html__( 'The ID of the user to switch to', 'ccat' ),
|
||||
'description' => 'The ID of the user to switch to',
|
||||
),
|
||||
),
|
||||
'outputFields' => array(
|
||||
'authToken' => array(
|
||||
'type' => 'String',
|
||||
'description' => esc_html__( 'JWT Token for the target user', 'ccat' ),
|
||||
'description' => 'JWT Token for the target user',
|
||||
),
|
||||
'refreshToken' => array(
|
||||
'type' => 'String',
|
||||
'description' => esc_html__( 'JWT Refresh Token for the target user', 'ccat' ),
|
||||
'description' => 'JWT Refresh Token for the target user',
|
||||
),
|
||||
'user' => array(
|
||||
'type' => 'User',
|
||||
'description' => esc_html__( 'The target user object', 'ccat' ),
|
||||
'description' => 'The target user object',
|
||||
),
|
||||
),
|
||||
'mutateAndGetPayload' => 'ccat_graphql_switch_to_mutation',
|
||||
@@ -31,19 +31,19 @@ function ccat_graphql_register_user_switch_to() {
|
||||
);
|
||||
}
|
||||
|
||||
// Callback for userSwitchTo mutation
|
||||
// Handle switchTo mutation
|
||||
function ccat_graphql_switch_to_mutation( $input ) {
|
||||
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {
|
||||
throw new \GraphQL\Error\UserError( esc_html__( 'Insufficient permissions', 'ccat' ) );
|
||||
throw new \GraphQL\Error\UserError( "Permissions insuffisantes pour se connecter en tant qu'un autre utilisateur." );
|
||||
}
|
||||
$user_id = absint( $input['userId'] );
|
||||
$current_user_id = get_current_user_id();
|
||||
if ( $user_id === $current_user_id ) {
|
||||
throw new \GraphQL\Error\UserError( esc_html__( 'Cannot switch to yourself', 'ccat' ) );
|
||||
throw new \GraphQL\Error\UserError( 'Impossible de se connecter en tant que son propre utilisateur.' );
|
||||
}
|
||||
$target_user = get_user_by( 'ID', $user_id );
|
||||
if ( ! $target_user ) {
|
||||
throw new \GraphQL\Error\UserError( esc_html__( 'User not found', 'ccat' ) );
|
||||
throw new \GraphQL\Error\UserError( 'Utilisateur introuvable.' );
|
||||
}
|
||||
$secret_key = defined( 'GRAPHQL_JWT_AUTH_SECRET_KEY' ) ? GRAPHQL_JWT_AUTH_SECRET_KEY : wp_salt();
|
||||
$issued_at = time();
|
||||
Reference in New Issue
Block a user