Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Public user login to view page

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

Page: 1
Go to End
Author Topic: 1770 Views
  • Roweena
    Avatar
    Community Member
    28 Posts

    Public user login to view page Link to this post

    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.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Public user login to view page Link to this post

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

  • Roweena
    Avatar
    Community Member
    28 Posts

    Re: Public user login to view page Link to this post

    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?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Public user login to view page Link to this post

    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.

  • Roweena
    Avatar
    Community Member
    28 Posts

    Re: Public user login to view page Link to this post

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

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Public user login to view page Link to this post

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

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

  • Roweena
    Avatar
    Community Member
    28 Posts

    Re: Public user login to view page Link to this post

    THANK YOU!!!! That works!

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Public user login to view page Link to this post

    Great

    1770 Views
Page: 1
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.