private function update_user($localUserId, $accessToken) { $service = $this->get_oauth_service(); $service->setAccessToken($accessToken); try { $updatedUser = $service->getUser(); } catch (HttpException $e) { return false; } if ($updatedUser) { return User::update_local_user($localUserId, $updatedUser); } return false; }
public function get_user() { $waUser = $this->loginRoute->get_wa_user(); if ($this->settings->get_create_local_user($this->settings->get_current_locale())) { return User::get_local_user($waUser); } return $waUser; }
/** * The function that handles the user login request * * @param WP_REST_Request $request * @return WP_REST_Response */ public function login(WP_REST_Request $request) { $redirectUri = $request->get_param('redirectUri'); $postRequiredRole = null; // Check for overriding settings from post if ($postId = url_to_postid($redirectUri)) { if (PostMetaBox::post_is_unlocked($postId)) { $this->redirect($redirectUri); } if ($requiredRole = PostMetaBox::post_required_role($postId)) { $postRequiredRole = $requiredRole; } } // Persist auth destination $this->set_auth_destination($redirectUri); // Get user from admin service try { $waUser = $this->get_wa_user($request); } catch (HttpException $e) { return new WP_REST_Response(['error' => $e->getMessage()], $e->getCode()); } // If the user is not logged in, we redirect to the login screen. if (!$waUser) { $this->trigger_login_flow($postRequiredRole); } // Save the user locally if the create_local_user setting is on if ($this->settings->get_create_local_user($this->settings->get_current_locale())) { User::create_local_user($waUser, $this->get_access_token()); // Auto login local user if the auto_login_local_user setting is on if ($this->settings->get_auto_login_local_user($this->settings->get_current_locale())) { User::wp_login_user(User::get_local_user($waUser)); } } // Check if auth destination has been set $redirect = $this->get_auth_destination(); if (!$redirect) { // Redirect to user profile $redirect = $waUser->url; } $this->redirect($redirect); }