From 9da31990fdc04d56acbb7bfc6fde7fa1d9c9c8a2 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Tue, 28 Aug 2018 14:34:55 +0200 Subject: [PATCH] add user info to JWT token response --- .../wiaas/includes/class-wiaas-user.php | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index 710ecd7..ff9fb7d 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -10,6 +10,8 @@ class Wiaas_User { public static function init() { add_action('init', array(__CLASS__, 'load_user_organization')); add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30); + add_filter('woocommerce_rest_prepare_customer', array(__CLASS__, 'transform_rest_customer'), 10, 3); + add_filter('jwt_auth_token_before_dispatch', array(__CLASS__, 'transform_jwt_token_response'), 10, 2); } public static function load_user_organization() { @@ -24,6 +26,58 @@ class Wiaas_User { remove_action( 'init', 'wp_register_default_user_group_taxonomy' ); remove_action( 'init', 'wp_register_default_user_type_taxonomy' ); } + + /** + * Apply wiaas custom transformation on retrieved JSON customer object + * + * @param $response + * @param $order + * @param $request + * + * @return mixed + */ + public static function transform_rest_customer($response, $order, $request) { + $data = $response->get_data(); + $result = array( + 'id' => $data['id'], + 'idCompany' => 0, + 'isCompanyAdmin' => 1, + 'mail' => $data['email'], + 'phone' => $phone, + 'name' => $data['first_name'] . ' ' . $data['last_name'], + 'companyName' => '', + 'billingAddresses' => [], + 'profileAddresses' => [], + 'userType' => $data['role'], + 'vatCode' => 0, + ); + + return new WP_REST_Response($result); + } + + /** + * Apply wiaas custom transformation on JWT token response + * + * @param $data + * @param $user + * + * @return mixed + */ + public static function transform_jwt_token_response($data, $user) { + return new WP_REST_Response(array( + 'token' => $data['token'], + 'userInfo' => array( + 'wiaas_id_user' => $user->ID, + 'wiaas_is_company_admin' => 1, //TODO: don't hardcode this + 'wiaas_user_full_name' => $user->first_name . ' ' . $user->last_name, + 'wiaas_user_type' => $user->roles, + 'wiaas_username' => $user->data->user_login + ) + )); + + } + + } Wiaas_User::init(); \ No newline at end of file