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.
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.
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 %>
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?
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.
...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;
}
}
}
Sorry another typo! try $member->Email (capital E)
I am at work so cant test the code.....well that's my excuse anyway ;)
THANK YOU!!!! That works! :-)
Great :)