Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Allow unauthenticated user to view a customize-draft post via REST API #117

Closed
wants to merge 14 commits into from

Conversation

miina
Copy link
Contributor

@miina miina commented Feb 16, 2017

Fixes #32

Allows unauthenticated user read a customize-draft post in case it's with status publish within the Snapshot/Changeset.

@coveralls
Copy link

Coverage Status

Coverage decreased (-26.8%) to 50.152% when pulling e9576ba on feature/grant-unauth-users-api-read-permissions into cd5a18f on develop.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-26.8%) to 50.152% when pulling e9576ba on feature/grant-unauth-users-api-read-permissions into cd5a18f on develop.

@coveralls
Copy link

Coverage Status

Coverage decreased (-26.8%) to 50.152% when pulling e9576ba on feature/grant-unauth-users-api-read-permissions into cd5a18f on develop.

@coveralls
Copy link

coveralls commented Feb 16, 2017

Coverage Status

Coverage increased (+0.2%) to 77.103% when pulling 12051c7 on feature/grant-unauth-users-api-read-permissions into cd5a18f on develop.

return $allcaps;
}

$post = get_post( absint( $args[2] ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Why absint? Should be guaranteed to already be a post ID. If it isn't, then the previous if should check for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it seems to be guaranteed, good point. Added that automatically, will remove.

if ( ! $chageset_id ) {
return $allcaps;
}
$data = $this->post_type->get_post_content( get_post( absint( $chageset_id ) ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Mispelling in chageset_id.

if ( isset( $data[ $key ] ) ) {
$changeset_post_values = $data[ $key ]['value'];
if ( isset( $changeset_post_values['post_status'] ) ) {
$is_published = 'publish' === $changeset_post_values['post_status'];
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the post is private and the current user can read private posts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unauthenticated user shouldn't have the ability to read private posts by default. Are you thinking of a case when reading private posts permission is given somewhere else to the unauthenticated user via a filter? Maybe it should be handled in the place where that's added then? Or in which cases would unauthenticated user have the permission to read private posts?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking of when a less-privileged authenticated user is making an API request. Consider an editor user making an authenticated request to the API. They should be able to see a post which is in the DB as an draft but which is private in the customized state.

'0' => 'read_post',
'2' => $post_id,
);
$allcaps = $this->manager->filter_user_has_cap( array(), $caps, $args );
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using the filter_user_has_cap method directly, should this instead use current_user_can to ensure that the filter is applying?

@coveralls
Copy link

coveralls commented Feb 20, 2017

Coverage Status

Coverage increased (+0.2%) to 77.103% when pulling 2331edd on feature/grant-unauth-users-api-read-permissions into cd5a18f on develop.


if (
! $this->current_snapshot_uuid
|| 0 !== $current_user->ID
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the check for current user being 0 should be removed because it should also work for under-privileged users making authenticated requests to read posts that normally they wouldn't be able to read, except that the post is made readable due to the status in the customized state.

Consider an author user making an authenticated API request. They should be able to access a post that is draft in the DB but publish in the customized state.

! $this->current_snapshot_uuid
|| 0 !== $current_user->ID
|| ! isset( $args[2] )
|| 'read_post' !== $args['0']
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is 0 a string here?


if ( true === $is_published ) {
$allcaps[ $caps[0] ] = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to change the condition here to be:

$allcaps[ $caps[0] ] = $is_published;

This could potentially handle the reverse condition where access to a post could be revoked if if its status is changed from publish to private and the user is not authorized to view private posts. There's probably more to it than that, but this is what came to mind.

@coveralls
Copy link

coveralls commented Feb 21, 2017

Coverage Status

Coverage increased (+0.1%) to 77.058% when pulling 1e9b8e7 on feature/grant-unauth-users-api-read-permissions into cd5a18f on develop.

$args = array(
'0' => 'read_post',
'2' => $post_id,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

These variables, $args and $caps aren't being used?

$post_id = $this->factory()->post->create( array(
'post_type' => 'post',
'post_status' => 'auto-draft',
) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Can there be other posts here including ones that have the draft, private, and publish statuses? Then below in the changeset data these posts can have the post_status overridden to for example reverse the statuses. Then at the end it can set the current user to anonymous, to an author, and to an administrator and for each then the read_post cap check can be verified to return the proper result for the customized state?

Copy link
Contributor Author

@miina miina Mar 2, 2017

Choose a reason for hiding this comment

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

On it. For some reason checking for read_post cap causes failure (fatal error) in unit tests, haven't figured out why yet, that's why the delay in fixing.

@miina miina changed the title Allow unauthenticated user to view a customize-draft post via REST API [WIP] Allow unauthenticated user to view a customize-draft post via REST API Feb 24, 2017
@coveralls
Copy link

coveralls commented Mar 2, 2017

Coverage Status

Changes Unknown when pulling abefb00 on feature/grant-unauth-users-api-read-permissions into ** on develop**.

@westonruter westonruter modified the milestone: Next Major Release Mar 10, 2017
@coveralls
Copy link

coveralls commented Mar 16, 2017

Coverage Status

Coverage increased (+0.2%) to 76.756% when pulling 9ad389a on feature/grant-unauth-users-api-read-permissions into c7b9124 on develop.

@miina miina changed the title [WIP] Allow unauthenticated user to view a customize-draft post via REST API Allow unauthenticated user to view a customize-draft post via REST API Mar 16, 2017
@valendesigns
Copy link
Contributor

@miina Please fix merge conflicts so I can review. Thanks!

@coveralls
Copy link

coveralls commented Aug 24, 2017

Coverage Status

Coverage increased (+0.2%) to 75.075% when pulling 7d0da3e on feature/grant-unauth-users-api-read-permissions into 1784a74 on develop.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 75.075% when pulling 7d0da3e on feature/grant-unauth-users-api-read-permissions into 1784a74 on develop.

@mehigh mehigh closed this Dec 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unauthenticated user unable to view a customize-draft post via REST API
5 participants