add check to private visibility for taxonomy #220

This commit is contained in:
vnmedeiros 2019-04-03 16:35:32 -03:00
parent 01c76e5c0c
commit e0360dd1e9
4 changed files with 41 additions and 9 deletions

View File

@ -385,21 +385,38 @@ class REST_Items_Controller extends REST_Controller {
public function get_items_permissions_check( $request ) {
$collection = $this->collections_repository->fetch($request['collection_id']);
if(($collection instanceof Entities\Collection)) {
if('edit' === $request['context'] && !$collection->can_read()) {
if('edit' === $request['context'] && !is_user_logged_in()) {
return false;
}
if ( isset($request['taxquery']) && !$this->get_items_permissions_check_for_taxonomy($request['taxquery']) ) {
return false;
}
if(($collection instanceof Entities\Collection)) {
if(!$collection->can_read()) {
return false;
}
return true;
} else {
if('edit' === $request['context'] && !$this->collections_repository->can_read(new Entities\Collection())) {
return false;
}
return true;
}
}
private function get_items_permissions_check_for_taxonomy($taxonomies) {
$taxonomy_repository = Repositories\Taxonomies::get_instance();
foreach ($taxonomies as $tax) {
$tax_id = $taxonomy_repository->get_id_by_db_identifier($tax['taxonomy']);
$taxonomy = $taxonomy_repository->fetch($tax_id);
if(($taxonomy instanceof Entities\Taxonomy)) {
if(!$taxonomy->can_read()) {
return false;
}
}
}
return true;
}
/**
* @param \WP_REST_Request $request
*

View File

@ -172,7 +172,10 @@ class REST_Taxonomies_Controller extends REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
if(($taxonomy instanceof Entities\Taxonomy)) {
if('edit' === $request['context'] && !$taxonomy->can_read()) {
if('edit' === $request['context'] && !is_user_logged_in()) {
return false;
}
if(!$taxonomy->can_read()) {
return false;
}

View File

@ -350,7 +350,10 @@ class REST_Terms_Controller extends REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
if(($taxonomy instanceof Entities\Taxonomy)) {
if('edit' === $request['context'] && !$taxonomy->can_read()) {
if('edit' === $request['context'] && !is_user_logged_in()) {
return false;
}
if(!$taxonomy->can_read()) {
return false;
}
@ -385,7 +388,10 @@ class REST_Terms_Controller extends REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
if(($taxonomy instanceof Entities\Taxonomy)) {
if('edit' === $request['context'] && !$taxonomy->can_read()) {
if('edit' === $request['context'] && !is_user_logged_in()) {
return false;
}
if(!$taxonomy->can_read()) {
return false;
}

View File

@ -670,6 +670,12 @@ abstract class Repository {
* @throws \Exception
*/
public function can_read( $entity, $user = null ) {
if(!is_user_logged_in()) {
$status = get_post_status($entity->get_id());
$post_status_obj = get_post_status_object($status);
return $post_status_obj->public;
}
if ( is_null( $user ) ) {
$user = get_current_user_id();
} elseif ( is_object( $user ) ) {