Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Conversation

@hichamboushaba
Copy link
Member

@hichamboushaba hichamboushaba commented Jan 10, 2023

When fetching a site using REST API or Jetpack CP, we use the endpoint /wc/v3/settings to infer if Woo is installed or not, and we were inferring a valid installation when we get a success, while 404 results in Woo missing, and other errors are reported as fetch errors.

When working on the User role check for the REST API project, I faced an issue as the API returns 403. This PR adjusts the logic to handle the 403 status code too, and if we get it we can infer that Woo is installed, since this means that API call reached the Woo API controller and resulted in the permission error.

For the second commit of the PR, check the comments below.

Testing

Please check the WCAndroid PR

Comment on lines +1344 to +1351
return coroutineEngine.withDefaultContext(T.API, this, "Fetch site") {
val updatedSite = if (site.isUsingWpComRestApi) {
siteRestClient.fetchSite(site)
} else {
siteXMLRPCClient.fetchSite(site)
}

updateSite(updatedSite)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions in the SiteStore are suspendable, and yet, the DB logic wasn't offloaded correctly to the background thread, with this change, we follow the convention that the suspendable functions in the Stores use the default context.

This exact issue was fixed for fetchSites in #2254, and this PR updates the two other fetch functions.

@hafizrahman hafizrahman self-assigned this Jan 12, 2023
NOT_FOUND -> WooPayload(false)
FORBIDDEN -> {
// If we get a 403 status code, we can infer that Woo is installed,
// the user just doesn't have permission to access the Woo API
Copy link
Contributor

@hafizrahman hafizrahman Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to discuss:

when a user doesn't get a permission to access API, is this exclusively because of their user role? Or can it be because of security plugins blocking certain users from using API, for example?

If there can be multiple reasons, do they need to be handled in this PR (maybe be clarified in comments)? I'm mostly asking because this PR seems to be tied to a task specifically about checking user role.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when a user doesn't get a permission to access API, is this exclusively because of their user role? Or can it be because of security plugins blocking certain users from using API, for example?

We don't care about the cause here, our goal of this code is just to know whether Woo is installed or not, what happens later depends on other parts of the app: for example, we handle the role check, but if the cause is different, then the behavior won't be defined, same as what happens when accessing the site using WordPress.com or Jeptack CP.

Another point to keep in mind: this is needed just because we fetch the site using XMLRPC for now, if we get a chance to work on improving this part, we will handle the Woo detection differently, as we will be able to infer whether its present or not from fetching the root /wp-json/ endpoint, which will be used also to fetch site details (check my discussion with Huong here).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation!

Copy link
Contributor

@hafizrahman hafizrahman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, just added a comment for discussion.

@hafizrahman hafizrahman merged commit 463c753 into trunk Jan 12, 2023
@hafizrahman hafizrahman deleted the rest-api-fixes branch January 12, 2023 10:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants