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.

Customising the CMS

Public user login to view page


Reply

8 Posts   1830 Views

Avatar
Roweena

24 February 2009 at 5:39am (Last edited: 24 February 2009 5:41am), Community Member, 28 Posts

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

25 February 2009 at 4:06am (Last edited: 25 February 2009 6:17am), Community Member, 598 Posts

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

25 February 2009 at 5:17am Community Member, 28 Posts

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

25 February 2009 at 5:25am Community Member, 598 Posts

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

25 February 2009 at 5:38am Community Member, 28 Posts

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

25 February 2009 at 6:16am Community Member, 598 Posts

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

25 February 2009 at 6:51am Community Member, 28 Posts

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

Avatar
Carbon Crayon

25 February 2009 at 7:21am Community Member, 598 Posts

Great :)