Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Menu with alternating pages if logged in or not?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 4147 Views
  • motoservo
    Avatar
    Community Member
    24 Posts

    Menu with alternating pages if logged in or not? Link to this post

    Anyone out there who can recommend a way to make Menu(1) show a different set of pages when a user is logged in? I have three pages that change, depending on if the user is anonymous or logged in. For instance, an Anonymous user sees Contact Us while a logged in user sees Help. I've tried overloading the get getShowInMenus:

    http://pastie.textmate.org/private/bydgzvlivmqi0ll7e8wrtw

    Then I added a conditional to the template: <% if ShowInMenus %> within the Menu(1) control.

    But the only thing that has changed is now, after I save a page with Show In Menus checked, then refresh the page, the page no longer has that option checked.

    The Menu(1) on my template still shows the page though, even when I'm logged in.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Hi motoservo

    I might be wrong, but couldn't you solve that issue by simply selecting Who can view this page?: Logged-in users in the Access-Tab?

  • motoservo
    Avatar
    Community Member
    24 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    The problem with that is that there is no inverse of that, "Logged Out Users". Anonymous users should see some pages that logged-in uses can't.

    I also tried another solution that *almost* worked out. It involved overriding canView. It worked on the front end but it also hid the pages in the cms and I couldn't figure out how to prevent that.

    Thanks for your reply.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Hi motoservo.
    Ah, I see the problem. I think the canView is the right direction though, since it not only prevents the page from showing in the menus, but also disallows access when the url is entered directly in the browser.

    You could simply grant view access to all "administrators" (to view and edit the page in the CMS) and use the anon/logged in rule for all other users?
    Something like this:

    class AnonPage extends Page {
       public function canView($member = null) {
          if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
          if($member && Permission::checkMember($member, "ADMIN")) return true;
          
          return $member ? false : true;
       }
    }

    class AnonPage_Controller extends Page_Controller {
       
    }

    Instead of a AnonPage class, you could also introduce some boolean value to your Page class that indicates if it's meant for anonymous or logged-in access.

  • motoservo
    Avatar
    Community Member
    24 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    I plugged that code in but the menu didn't seem to change. I wonder if it had anything to do with a second method that Simon had me override, I think it has to clear before canView is called. Here is the original two methods as were suggested to me:

    function canView() {
       // Simon's suggestions
       return Member::currentUserID() ? false : true;
    }

    function can($type) {
       return $type == 'view' ? $this->canView() : parent::can($type);
    }

    These are the two the got my close. Oddly, and I don't know if it's been due to my tinkering, but the results seem inconsistent. I'm pretty sure I tried this recently and this was the solution that worked on the front end but prevented me from seeing the page in the CMS. I just tried it again, however, and now it doesn't seem to be changing the menu, either.

  • motoservo
    Avatar
    Community Member
    24 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Just messed with it a bit, I can't log in with those two methods there, but if I remove them I can log in, then when I add them back the menu works as expected. But I can't access the cms still.

    Thanks for taking a look at this for me, banal.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Hi motoservo

    Did you try my suggestion? I tested it and it should work like this:
    Admin can see AnonPage (required, so that he can edit in CMS)
    Anonymous user can see AnonPage
    Any logged in user (not admin) cannot see AnonPage

    As you can see in my code-example, I first check if the user is Admin and grant permission if he's admin. If you don't do that, you can't login to the CMS because you cannot see any of the pages...

  • motoservo
    Avatar
    Community Member
    24 Posts

    Re: Menu with alternating pages if logged in or not? Link to this post

    Yeah, I tried your code but it didn't seem to change my menu. I'll try again to see if I can figure out why it didn't work.

    Also, not to complicate things, but the admin shouldn't be able to see the anon pages on the front end. The menu is just wide enough to hold one set of list items, or the other. If both are showing up the page will break. This will only look bad for the client but I'm hoping to figure out a way so that the admin is just treated as a logged in user on the front end, but on the back end he can see the pages.

    I know, this is a strange design! I told the designer I'm having trouble implementing it and it may be possible to change the design, but I'm not giving up just yet.

    I like your idea of the boolean and just using one class. I was considering doing this in the beginning but for whatever reason I went the other way with it. I'm going to switch this to a boolean as soon as I can get everything working.

    I'll go test your code now to see if I can figure out why it didn't change anything.

    4147 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.