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.

Customising the CMS /

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

Public user login to view page


Go to End


8 Posts   2913 Views

Avatar
Roweena

Community Member, 28 Posts

24 February 2009 at 5:39am

Edited: 24/02/2009 5:41am

I need to have a page that can only be viewed by the public if they login in with a given username and password. The link to the page will appear in the website navigation menu, when you click on the link you would be displayed with a login page where you would enter a username and password (that you would obtain by emailing and asking the website owner for the password).
Is it possible to do this and how?
I've tried creating a user group and making the page accessible by only this group but then the link to the page doesn't show up in the navigation menu.

Avatar
Carbon Crayon

Community Member, 598 Posts

25 February 2009 at 4:06am

Edited: 25/02/2009 6:17am

You could use a small function in your page controller to check whether the user is logged in as that member so that you can draw the template accordingly, something like this:

class YourPage_Controller extends Page_Controller 
{

public function IsCorrectMember(){

$member = Member::currentUser();

if($member->Email == 'pass@email.com'){
return true;
} else {
return false;
}
}
}

You can use any attribute of that member you like, for example if you wanted to use the ID you could do $member->ID etc. The only thing to remember is that by default the only fields that are always unique are ID and email.
Then in your template you can do something like this:

<% if IsCorrectMember %>
$Content
<% else %>
$LoginForm
<% end_if %>

Avatar
Roweena

Community Member, 28 Posts

25 February 2009 at 5:17am

Thanks for this, I just need a little more help.
I've set up a new page with controller as set out below, when I click on the link to access the page I am presented with the login form, I enter in the email I've set up and the page refreshes with the login form again but says "You're logged in as video" and doesn't display the content of the page.

I've set up a group called video and a member called video (with email address as video), but not assigned any permissions to the group (not sure which ones I would need to use, if at all).

...I've obviously gone a bit wrong somewhere with this, can you help?

Avatar
Carbon Crayon

Community Member, 598 Posts

25 February 2009 at 5:25am

hmm strange, it means our function is returning false each time.

Try first changing the member::currentUser() to Member::currentUser() I think that was a typo (capital M).

Also try returning 1 and 0 instead of true and false respectively. It shouldn't make a difference but I seem to recall having this issue before.

Avatar
Roweena

Community Member, 28 Posts

25 February 2009 at 5:38am

...Still not working, here is my page controller....

class VideoPage_Controller extends Page_Controller {

public function IsCorrectMember(){

$member = Member::currentUser();

if($member->email == 'video'){
return 1;
} else {
return 0;
}
}
}

Avatar
Carbon Crayon

Community Member, 598 Posts

25 February 2009 at 6:16am

Sorry another typo! try $member->Email (capital E)

I am at work so cant test the code.....well that's my excuse anyway ;)

Avatar
Roweena

Community Member, 28 Posts

25 February 2009 at 6:51am

THANK YOU!!!! That works! :-)

Avatar
Carbon Crayon

Community Member, 598 Posts

25 February 2009 at 7:21am

Great :)