Jump to content

Show menu item only to users with certain roles


gebeer
 Share

Recommended Posts

Hello,

I have a similar case like described in bwakad's post. But I don't want to ask there since the post is already marked as solved.

My site has users with role "frontend" assigned that can edit their user profiles and add stuff on the frontend.

frontend role setup:

post-1920-0-96711100-1412335230_thumb.pn

The user profiles are all stored in pages with template "userprofile" That template has these access settings:

post-1920-0-87541200-1412334613_thumb.pn

All user profiles live under a page "Profiles" that also has the template "userprofile":

post-1920-0-56838600-1412334728_thumb.pn

Now I would expect that the "Profiles" page only appears in the menu when a user with role "frontend" is logged in. But the menu item does not get displayed.

I'm also using the function to display bootstrap menu item but bwakad's workaround seems a bit awkward (excuse the pun  ;) ).

Does PW just work that way or is my access and permission setup wrong somehow? And how do I get the "profiles" menu item displayed only to users with role frontend?

Link to comment
Share on other sites

    All user profiles live under a page "Profiles" that also has the template "userprofile":

profiles-tree.png

Do you have your own user access system you are working with?  I can be wrong, but the PW user profiles are normally under Admin/Access/Users.  

Link to comment
Share on other sites

@netcarver

I added the link to that post.

@onjegolders

The profiles page is neither unpuplished nor hidden. It's template "userprofile" has access rules defined as per screenshot I supplied in my original post.

I'm using this function to loop out my navigation:

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

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

        if ($child->numChildren(true) && count($child->parents) == 1) {
            $class .= 'dropdown';
            $atoggle .= ' class="dropdown-toggle" data-toggle="dropdown"';
        } else if ($child->numChildren(true) && count($child->parents) > 1 ) {
            $class .= 'dropdown-submenu';
            $atoggle .= ' class="dropdown-toggle"';
        } else if ($child->numChildren(true) && $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(true) && 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(true) && 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(true) && $child->id != 1) {
            $output .= renderChildrenOf($child->children, $output, $level);
        }
        $output .= "</li>";
    }
    $outerclass = ($level == 1) ? "nav navbar-nav navbar-right" : 'dropdown-menu';
    return "<ul class='$outerclass'>$output</ul>";
}

Link to comment
Share on other sites

The approach: I'm not sure what really those front end user are and what they're for and what all they can do. Usually members that only interact on front end are members of some sort and not editors, they are not backend users. What I always do is not give them any page edit permisson or anything alike, just view access like a guest role. Then in templates use their role and $user->isLoggedin() && $user->hasRole("member") to conditionally give them what they need. For example show a login link, or a Profile link in a metanav, depending on user status.

Backend user roles and permission are mainly for backend users, and I don't want to mix that up. I built specific forms and front-end pages solely based on what role a user has and if logged in. I have member pages with they're data stored in some branch and have a one "Profle" page that is used to build forms of they're profile with data they can edit. I don't need any page edit functionality of PW backend at all. This way I can specifically build functions and logic around what is needed without being caught in what PW is doing designed for backend usage. Also note that as soon as you give edit permission to a role they can access the admin, and you need to take care of that aswell. 

I tested a setup like you mention and it's working fine. If you have "profiles" template not giving "view" access you see a expanded settings at the bottom what to do when a user accesses this page. Default is showing a 404 page and that those page don't list in navigation. 

Now it depends how the navigation is built via API. It's still possible to load that page via API explict and show it to a not logged in member. But not showing in lists means for calls like children() and find() etc. 

I tested your menu code and all working as expected. 

$content .= renderChildrenOf($pages->get("/")->children);

The profiles page don't show (when not logged in with that user), and show up when logged in.

  • Like 3
Link to comment
Share on other sites

Thank you Soma, for taking your time and explaining things that precisely.

This really helped me to get a better understanding of the whole role and permissions concept.

I will alter my frontend role like you suggested and add checks for $user->isLoggedin() && $user->hasRole("frontend") in my templates.

If I get you correctly on the navigation render: It is expected behaviour that the Profile page does NOT show, right? So I need to render an extra users menu with links to their profile page and whatever else they need? Pls confirm. Thank you.

Link to comment
Share on other sites

Just to make clear my approach is just the way I prefer for members to a site only having their profile and maybe see things when logged in. The view permission is great for that and I keep things flexible on a per page or situation where not possbile with template permissions. Page edit permission isn't necessary really as they don't edit pages in "admin", if then I create forms tailored to the needs. Page editing isn't needed cause I edit them via API and not the page edit screen from the admin.

Note that the new Dynamic Roles module may open up a lot more possibilities to fine tune permissions, but then I still wouldn't use that for simple frontend only users. Not saying it's not possible.

About navigation, I'm not sure I can follow you. If a template (page) has guest view access removed, it doesn't show in navigations for guest user. One logged in with a role that has view access on that template it shows up. As explained in my post above. There's some settings on the behaviour one you untick the guest view access on a template below, like show 404 or redirect to a url etc and if pages with that template show in lists or not.

Link to comment
Share on other sites

Just to make clear my approach is just the way I prefer for members to a site only having their profile and maybe see things when logged in.

That approach totally makes sense to me and fits my project requirements very well, especially since I also want to keep frontend users out from the admin area.

If a template (page) has guest view access removed, it doesn't show in navigations for guest user. One logged in with a role that has view access on that template it shows up.

My Profile page has guest view permissions removed. Only frontend user role is allowed to view it. But still it does NOT show up when frontend user is logged in. And I don't know why. You can see my role and template setup in my first post. So I guess something must still be wrong with my role/template access setup. Still lost on this one.

Link to comment
Share on other sites

Your first post shows only profiles template. Not sure what profile. However I set up the same thing and used your function to create a list and it works fine as it should. Maybe we need to see all settings and whole code.

Or you just need to try harder cause it isn't magic and just works. :P

Link to comment
Share on other sites

I think your op is confusing cause you now say userprofile template has view access removed for guests. Yet your screen shot indicates clearly the profiles page is restricted by the lock icon. Not sure if that helps. So your profiles page wouldn't be visible when not logged in but visible when logged in. An all user profile pages are then also viewable. Just judging from the screen shot.

Link to comment
Share on other sites

These are the access settings now for my Profiles page with template userprofiles

post-1920-0-08051200-1412513867_thumb.pn

The Profiles page still doesn't show in the menu, although it says in the settings:

No - Pages may NOT not appear in searches/lists unless the user has access to view them

My logged in user has role guest and frontend. Role frontend has these permissions:

post-1920-0-28475500-1412514212_thumb.pn

Sorry, I still can't see why the menu item would not be visible.

Link to comment
Share on other sites

Took 10 min (ok 20min) to setup a site with this setup showing that it works just fine.

http://lithium-r3t.lightningpw.com/

If you go to site http://lithium-r3t.lightningpw.com/profiles/

You'll get to login (not 404 as default), 

member

member123

is a member user

Once logged in you'll see the profiles page and the profile page under it in navigations. I added the menu code you posted in the profiles page, but you can also confirm it by the top default navigation or the sitemap.

I sent you a admin login PW so you could see the setup (as you posted).

Link to comment
Share on other sites

Let's keep conv. in thread and not in PM.

Thanks working fine now.

Your setup is different in that you use two different templates for Profiles and profile1 page.

How would that matter? I just changed the /profiles/profile1/ page to use profiles template too and there's no change in behaviour.

Permission are inherited in PW so child pages inherit permission from parents. Not that it really matters here but it's good to keep in mind.

Maybe it's me or you're not communicating clearly and with all details, but there's nothing really obvious that I see but then I maybe don't see/know all.

Link to comment
Share on other sites

I just tried on your site and I'm not sure I see what is exactly not working, it's all working fine. I can login and see profile page (actually am on profile page). So what exactly is not showing that you think it should, and post your (all) code related to that, not just the menu function because that works fine.

Link to comment
Share on other sites

The profile page itself shows fine. But there should also be a menu item "Profiles" visible in the top menu bar next to "Home". when logged in as frontend user. That is my problem.

How would that matter? I just changed the /profiles/profile1/ page to use profiles template too and there's no change in behaviour.

I thought, too, that it wouldn't matter to have 2 different templates or only one. That was just the only difference that I noticed when comparing your settings to mine.

Link to comment
Share on other sites

Just looks like the menu code isn't generating the menu properly and that if you look at the code source you can tell that there are the profile pages, but they're not showing in the boostrap menu cause no title or just an <ul class="drop-down"> so not valid code?

So after all it looks like the menu code has problems (or not), but could easily spot with a glance at the console or html source... ;)

post-100-0-88320400-1412517922_thumb.png


Just looking at the "/profile/" page it has no title field?


So sorry but check your code and don't worry about PW setup or something. :D:P

  • Like 1
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...