Jump to content

Page-specific permissions?


doolak

Recommended Posts

I go and try to reproduce this on another server first , copying the full page right now good N8

Edit:
The Server i wanted to move this has moved itself to a new location so for the moment i am stuck till the admin has send me new accessdata. So i guess its best to whish a happy new Year and continue this discussion next year ;-)

Link to comment
Share on other sites

  • 2 months later...

Hi Guys, 

So I have this navigation setup for a site, it can be found here:

https://processwire.com/talk/topic/5680-bootstrap-3-navigation-with-multiple-leveltier-fix/

However, I have also added this module to the site which works great:

http://modules.processwire.com/modules/custom-page-roles/

Any ideas for displaying the navigation links based on the logged in users ability to see the page?

Figured I would ask around before I dove right in. 

Here is the nav code:

function renderChildrenOf($pa, $output = '', $level = 0)
{
    $output = '';
    $level++;

    foreach ($pa as $child) {
        $atoggle = '';
        $class = '';

        if ($child->numChildren && count($child->parents) == 1) {
            $class .= 'dropdown';
            $atoggle .= ' class="dropdown-toggle" data-toggle="dropdown"';
        } else if ($child->numChildren && count($child->parents) > 1) {
            $class .= 'dropdown-submenu';
            $atoggle .= ' class="dropdown-toggle"';
        } else if ($child->numChildren && $child->id != 1 ) {
            $class .= 'dropdown-menu';
        }

        // Makes the current page and it's top level parent add an active class
        $class .= ($child === wire("page") || $child === wire("page")->rootParent) ? " active" : '';
        $class = strlen($class) ? " class='" . trim($class) . "'" : '';

        if ($child->numChildren && count($child->parents) == 1) {
            // Add Caret if have children
            $output .= "<li$class><a href='$child->url'$atoggle>$child->title <b class='caret'></b></a>";
        } else if ($child->numChildren && count($child->parents) > 1) {
            $output .= "<li$class><a tabindex='-1' href='$child->url'$atoggle>$child->title</a>";
        } else {
            $output .= "<li$class><a href='$child->url'$atoggle>$child->title</a>";
        }

        // If this child is itself a parent and not the root page, then render it's children in their own menu too...
        if ($child->numChildren && $child->id != 1) {
            $output .= renderChildrenOf($child->children, $output, $level);
        }
        $output .= '</li>';
    }
    $outerclass = ($level == 1) ? "nav navbar-nav" : 'dropdown-menu';
    return "<ul class='$outerclass'>$output</ul>";
}

// bundle up the first level pages and prepend the root home page
$homepage = $pages->get(1);
$pa = $homepage->children;
//$pa = $pa->prepend($homepage);

// Set the ball rolling...
echo renderChildrenOf($pa); ?>
Link to comment
Share on other sites

I'm using sort of the same function, and although my setup is different (i have a custom menu generated from a branch on the tree) this might help, i use this right after the opening foreach:

$menuVis = true;
if($child->menu_roles->count()) {	    
    foreach($child->menu_roles as $cmr) {
	    $menuVis = false;
	    $cmrRole = wire('roles')->get("$cmr->name");
		if (wire("user")->hasRole("$cmrRole") ) {
			$menuVis = true;
			break;
		}
	}
} 
if($menuVis == false) continue;
  • Like 2
Link to comment
Share on other sites

@quickjeff, i'm not using that module, but the code i posted could probably be adjusted to work for it, because as i see in the module description

Now when you edit a page using a template that has this field, you'll have checkboxes for roles that you can choose to define access from.

so wouldn't you be able to use that snippet in some form to see if the menu item's page should be output?

Link to comment
Share on other sites

With the assistance of Macrura and another thread in this awesome community, I successfully have also been able to manipulate what menu items display in the navigation based on the users page role. Example is using bootstrap navigation.
 

hey quickjeff - maybe you could post your final code, to see how it was adjust for the use case?
cheers!

Code below:

 

<div id="navbar" class="navbar-collapse collapse">
          <?php

			function renderChildrenOf($pa, $output = '', $level = 0)
			{
			    $output = '';
			    $level++;
			
			    foreach ($pa as $child) {
			        $atoggle = '';
			        $class = '';
			        

			         //Check if the user is allowed to view nav links
			        $menuVis = true;
					if($child->page_roles) {	    
					    foreach($child->page_roles as $cmr) {
						    $menuVis = false;
						    $cmrRole = wire('roles')->get("$cmr->name");
							if (wire("user")->hasRole("$cmrRole") ) {
								$menuVis = true;
								break;
							}
						}
					} 
					if($menuVis == false) continue;
			        

			
			        if ($child->numChildren && count($child->parents) == 1) {
			            $class .= 'dropdown';
			            $atoggle .= ' class="dropdown-toggle" data-toggle="dropdown"';
			        } else if ($child->numChildren && count($child->parents) > 1) {
			            $class .= 'dropdown-submenu';
			            $atoggle .= ' class="dropdown-toggle"';
			        } else if ($child->numChildren && $child->id != 1 ) {
			            $class .= 'dropdown-menu';
			        }
			
			// Makes the current page and it's top level parent add an active class
			$class .= ($child === wire("page") || $child === wire("page")->rootParent) ? " active" : '';
			$class = strlen($class) ? " class='" . trim($class) . "'" : '';
			
		if ($child->numChildren && count($child->parents) == 1) {
			            // Add Caret if have children
		$output .= "<li$class><a href='$child->url'$atoggle>$child->title <b class='caret'></b></a>";
			        } else if ($child->numChildren && count($child->parents) > 1) {
		$output .= "<li$class><a tabindex='-1' href='$child->url'$atoggle>$child->title</a>";
			        } else {
			            $output .= "<li$class><a href='$child->url'$atoggle>$child->title</a>";
			        }
			
		// If this child is a parent and not the root page, then render it's children in menu
			        if ($child->numChildren && $child->id != 1) {
			            $output .= renderChildrenOf($child->children, $output, $level);
			        }
			        $output .= '</li>';
			    }
			    $outerclass = ($level == 1) ? "nav navbar-nav" : 'dropdown-menu';
			    return "<ul class='$outerclass'>$output</ul>";
			}

			// bundle up the first level pages and prepend the root home page
			$homepage = $pages->get(1);
			$pa = $homepage->children;
			
			$pa = $pa->prepend($homepage);
			
			echo renderChildrenOf($pa); ?>
		  
		         
        </div>
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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...