Hardcoded autonav display by permissions

Permalink
I have hardcoded an autonav block into my template, like so

$bt = BlockType::getByHandle('autonav');
$bt->controller->displayPages = 'custom';
$bt->controller->displayPagesCID = '67';      
$bt->controller->orderBy = 'display_asc';                    
$bt->render('view');


Certain pages have permissions set and are only viewable to certain groups.

I want the autonav to display all pages, but only those with permissions will be clickable as links, the others will just be greyed out text.

I've experimented with the users, groups and permissions objects, but with no success so far.

mrnoisy
 
jordanlev replied on at Permalink Reply
jordanlev
Are you saying that the non-permission pages are not showing up at all in the autonav, or they are showing up but you want them to not be clickable?
mrnoisy replied on at Permalink Reply
mrnoisy
They are not displaying with the current code, however if I change
$bt->controller->displayPages = 'custom';

to
$bt->controller->displayPages = 'all';

then they all show up.
jordanlev replied on at Permalink Reply
jordanlev
Well, you need to use the setting that gets them to display (displayPages='all'), then you will want to create a custom template (copy concrete/blocks/autonav/view.php to blocks/autonav/templates/mytemplatename.php, and edit that new file you just copied).
In the custom template, you're going to have to write some code that checks the permissions of each page and changes the html somehow depending on whether the user has access to it or not.

This is probably an "intermediate level" task -- if you're not a coder, it's going to be very difficult, but if you have some coding experience but are new to C5, it should be doable.
mrnoisy replied on at Permalink Reply
mrnoisy
I'm actually putting the code directly into the template file.

I am a coder, but I can't seem to find the function that determines whether a page has permissions for a user and/or group.
jordanlev replied on at Permalink Reply
jordanlev
Okay, well if you are modifying the default view.php file, then you want to put this code inside the "foreach($aBlocks as $ni)" loop. Note the first line inside that loop:
$_c = $ni->getCollectionObject();

So the $_c variable is the page (aka collection) object. To get permissions for that page, do this:
$p = new Permissions($_c);
if ($p->canRead()) {
  //User has permission to read the page
} else {
  //User does not have permission
}


You're probably going to want to assign the result of $p->canRead() to a variable, then down in the "if ($c->getCollectionID() == $_c->getCollectionID()) {" portion of code, use it in each of the if branches to either output the <a> or not.

Hope that makes sense.

-Jordan

PS - You should at least copy the concrete/blocks/autonav/view.php to blocks/autonav/view.php and modify that -- it will override the default view without you having to choose a custom template, but has the advantage of not being overwriiten when you update your system.
mrnoisy replied on at Permalink Reply
mrnoisy
I used the wrong terminology - the code is not in the block view.php, it is in the theme page eg. left_column.php.
jordanlev replied on at Permalink Reply
jordanlev
You can't do what you're trying to do from the theme page. You need to do it by either overriding the autonav block's view.php file or by creating a custom template.
mrnoisy replied on at Permalink Reply
mrnoisy
Ok, in blocks/autonav/view.php I've added the following:
$_c = $ni->getCollectionObject();
$p = new Permissions($_c);
if ($p->canRead()) {
 echo "y";
} else {
 echo "n";
}


I get the y's showing up, but no n's.
jordanlev replied on at Permalink Reply
jordanlev
Sorry if this is a stupid question, but are you testing this out while logged in as the administrator, or logging out and testing it?
mrnoisy replied on at Permalink Reply
mrnoisy
Not logged in as admin.

Here is the scenario I need. For example 3 pages and 3 groups:

page 1
page 2
page 3

group 1
group 2
group 3

group 1 can access only page 1, but can see page 2 and 3 listed

group 2 can access pages 1 and 2, but can see page 3 listed

group 3 can access all pages
mrnoisy replied on at Permalink Reply
mrnoisy
I got it. I had to tick the box

"Display pages to users even when those users cannot access those pages."
ion replied on at Permalink Reply
ion
Thanks ! i had a really hard time too, finding this hidden checkbox... >.<