Jump to content

Selector works for admin but not for guest


GradDev
 Share

Recommended Posts

Hello,

I have the following selector and output which works when I am logged in as an admin and as a guest as well.

$pages->get("name=reports")->children()->each("report_meta_details.report_type.title")

//Output
//Array ( [0] => Global [1] => Global [2] => Global [3] => Global [4] => Global )

Now,

The following selector returns different results for Admin and Guest:

$pages->get("name=reports")->children("report_meta_details.report_type.title=Global")->each("report_meta_details.report_type.title");

//Output for Admin
//Array ( [0] => Global [1] => Global [2] => Global [3] => Global [4] => Global )

//Output for Guest
//Array ( )

 

Here, the field "report_meta_details" is a FieldsetPage, the field "report_type" is a page reference (to children of a page called report-type). 

Now, the first once runs for both the users; and both the users have the access to the templates, as the titles are returned correctly in the first selector.

But the second one, the selector is using something that the users have access to but still does not show the results to the Guest.

Any reasons why this is the case?

All help is appreciated. Thanks!

 

EDIT:

I have tried the check_access=0 selector as well. But no effect.

Link to comment
Share on other sites

Looks like access control issue ?

Can you try:

$pages->get("name=reports")->children("check_access=0,report_meta_details.report_type.title=Global")->each("report_meta_details.report_type.title");

I wonder, if you try this one, does it behave the same way?

$pages->find("parent.name=reports, report_meta_details.report_type.title=Global")->each("report_meta_details.report_type.title");

 

  • Like 3
Link to comment
Share on other sites

Thank you for the response @elabx

But, check_access=0 does not work. I have tried it already. It still returns an empty array. That's what was more confusing for me! ?
(I should have added it in the question. My mistake.)

And yes, it is the same with find() and the parent selector.

-----

@Jan Romero yes, you are right!  I do use the selectors in a key-value pair for more clarity in code ?

 

 

  • Like 1
Link to comment
Share on other sites

56 minutes ago, GradDev said:

But, check_access=0 does not work.

This surprises me. To debug this, it may be useful to look at the generated SQL. You can do this easily with Tracy Debugger (I guess you would have to enable it for guests temporarily?), but you can also do it in code: 

 

  • Like 1
Link to comment
Share on other sites

Thank you @Jan Romero for your reply!

I installed Tracy Debugger, checked how to use the console to get the SQL like in the link you have shared and following are the results. I am not able to understand the details in the query yet, but it surely is different for both the users.

 

When run in with Admin user:

I ran this in the console of Tracy:

d($pages->getPageFinder()->find(new Selectors("report_meta_details.report_type.title=Global"), array('returnQuery' => true))->getQuery());

And this was the result: (please note that there are many many similar lines where I have entered dots)

SELECT pages.id,pages.parent_id,pages.templates_id  
FROM `pages`  
JOIN field_report_meta_details AS field_report_meta_details ON field_report_meta_details.pages_id=pages.id AND ((((field_report_meta_details.data=25488) ) 
        OR ((field_report_meta_details.data=25507) )  
        OR ((field_report_meta_details.data=25526) )  
        OR ((field_report_meta_details.data=25545) )  
        OR ((field_report_meta_details.data=25564) )  
        OR ((field_report_meta_details.data=25583) )  
        .
		.
		.
        OR ((field_report_meta_details.data=38127) )  )) 
WHERE (pages.status<:i0X)
GROUP BY pages.id

 

And for the Guest User:

I entered this:

d($pages->getPageFinder()->find(new Selectors("check_access=0,report_meta_details.report_type.title=Global"), array('returnQuery' => true))->getQuery());

And this is the result:

SELECT pages.id,pages.parent_id,pages.templates_id  
FROM `pages`  
JOIN field_report_meta_details AS field_report_meta_details ON field_report_meta_details.pages_id=pages.id AND ((((field_report_meta_details.data=0) ) )) 
WHERE (pages.status<:i0X)
GROUP BY pages.id 

 

Following is run in Tracy console without "check_access=0" with Guest user:

d($pages->getPageFinder()->find(new Selectors("report_meta_details.report_type.title=Global"), array('returnQuery' => true))->getQuery());

And this is the result:

SELECT pages.id,pages.parent_id,pages.templates_id  
FROM `pages`  
JOIN field_report_meta_details AS field_report_meta_details ON field_report_meta_details.pages_id=pages.id AND ((((field_report_meta_details.data=0) ) )) 
LEFT JOIN pages_access ON (pages_access.pages_id=pages.id AND pages_access.templates_id IN(2,133,3)) 
WHERE (pages.status<:i0X) 
AND pages.templates_id NOT IN(2,133,3) 
AND pages_access.pages_id IS NULL
GROUP BY pages.id
Link to comment
Share on other sites

Mh, this might be a ProcessWire bug? This configuration causes multiple database queries to be executed. The query you end up with contains a list of all pages that reference the "Global" report_type. To get this list, a different query is run beforehand and that one doesn’t seem to get the right results for guest users. I assume this is because guests don’t have access to the repeater pages (a hidden page called "Repeaters" under the "Admin" branch), but I haven’t been able to figure it our completely.

As a workaround, you can use "include=all" instead of "check_access=0", but be aware that this may have unintended effects because it will find pages that are hidden or unpublished. You can also get the IDs yourself and use them like so:

$ids = pages()->findIDs('template=report_type, title=Global'); //adjust selector according to your setup

$reports = pages()->find('parent.name=reports, report_meta_details.report_type=' . implode('|', $ids)); //note: don’t use “report_type.id=”

Can you confirm that your "report_type" pages have no access restrictions and are not hidden or unpublished?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...