Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Menu with alternating pages if logged in or not?


Go to End


11 Posts   7743 Views

Avatar
motoservo

Community Member, 27 Posts

10 August 2009 at 6:39pm

Edited: 10/08/2009 6:40pm

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.

Avatar
bummzack

Community Member, 904 Posts

10 August 2009 at 7:21pm

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?

Avatar
motoservo

Community Member, 27 Posts

10 August 2009 at 7:33pm

Edited: 10/08/2009 7:44pm

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.

Avatar
bummzack

Community Member, 904 Posts

10 August 2009 at 7:57pm

Edited: 10/08/2009 7:58pm

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.

Avatar
motoservo

Community Member, 27 Posts

10 August 2009 at 9:44pm

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.

Avatar
motoservo

Community Member, 27 Posts

10 August 2009 at 9:55pm

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.

Avatar
bummzack

Community Member, 904 Posts

10 August 2009 at 10:12pm

Edited: 10/08/2009 10:13pm

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...

Avatar
motoservo

Community Member, 27 Posts

10 August 2009 at 10:27pm

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.

Go to Top