Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Custom Login in SS3.1

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 239 Views
  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Custom Login in SS3.1 Link to this post

    Hi Everyone

    Well I am having a complete nightmare with the Member system and creating a custom login page, who knew it would be nearly impossible to work with and without proper documentation anywhere!

    I am simply trying to build a login page for members that have a certain Boolean checked, and then redirect them to a different page. However I am suffering barriers in all directions.

    The current ones are:

    1. [Strict Notice] Non-static method Controller::redirect() should not be called statically, assuming $this from incompatible context
    2. When clicking the logout button it takes me no a white screen

    CustomLogin .php

    <?php
    class CustomLogin extends MemberLoginForm {

    // this function is overloaded on our sublcass (this) to do something different
    public function dologin($data) {
    if($this->performLogin($data)) {
    if(!$this->redirectByGroup($data)){
    return Controller::redirect("/home");
    //echo 'done';
    }
    } else {
    if($badLoginURL = Session::get("BadLoginURL")) {
    return Controller::redirect($badLoginURL);

    } else {
    return Controller::redirectBack();
    }
    }
    }

    public function redirectByGroup($data) {
    // gets the current member that is logging in.
    $member = Member::currentUser();
    // gets all the groups.
    $Groups = DataObject::get("Group");

    //cycle through each group
    foreach($Groups as $Group){
    //if the member is in the group and that group has GoToAdmin checked
    if($member->inGroup($Group->ID) && $Group->GoToAdmin == 1)
    {
    //redirect to the admin page
    return Controller::redirect('/admin');
    return true;
    }
    //otherwise if the member is in the group and that group has a page link defined
    elseif($member->inGroup($Group->ID) && $Group->GoToMemberProfile == 1)
    {
    Controller::redirect('/members/profile');
    return true;
    }

    }
    //otherwise if none of the above worked return fase
    return false;

    }

    }
    ?>

    GroupDecorator.php

    <?php
    class GroupDecorator extends DataExtension {

    static $db = array (
    "GoToAdmin" => "Boolean",
    "GoToMemberProfile" => "Boolean"
    );

    private static $has_one = array(
    );

    function updateCMSFields(FieldList $fields) {
    $fields->push(new CheckboxField("GoToAdmin", " Go to Admin area"), "Members");
    $fields->push(new CheckboxField("GoToMemberProfile", " Go to Member area"), "Members");
    }

    }

    _config.php

    Object::useCustomClass('MemberLoginForm', 'CustomLogin');
    //Tells silverstripe to add our extention to the group class
    Object::add_extension('Group', 'GroupDecorator');

    MemberLoginPage.php

    ...
    class MemberLoginPage_Controller extends Page_Controller {

       private static $allowed_actions = array (
          "LoginForm"
       );

       function LoginForm() {
    $form = new CustomLogin($this,"LoginForm");
    // print_r($_REQUEST);
    return $form;
    }

    }

    Any help will be great. I have looked at the SSBits tutorial, I have tried the member profiles extension on GitHub, read hours of documentation and nothing is giving in!

    Thanks

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Custom Login in SS3.1 Link to this post

    Hi Lawrence,

    I've not customized it yet - am hoping to reuse the current code as much as possible. However here is a complete working example in a module - it's what I intend to reference when I add it

    https://github.com/axyr/silverstripe-adminlogin

  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Re: Custom Login in SS3.1 Link to this post

    I Got this working in the end by changing the dologin into a single function, exact same code, just one function, it then didnt throw a wobbly at me about strict notices!

    <?php
    class CustomLogin extends MemberLoginForm {

    // this function is overloaded on our sublcass (this) to do something different
    public function dologin($data) {
    if($this->performLogin($data)) {

    $member = Member::currentUser();
    // gets all the groups.
    $Groups = DataObject::get("Group");

    //cycle through each group
    foreach($Groups as $Group){
    //if the member is in the group and that group has GoToAdmin checked
    if($member->inGroup($Group->ID) && $Group->GoToAdmin == 1)
    {
    //redirect to the admin page
    return Controller::redirect('/admin');
    }
    //otherwise if the member is in the group and that group has a page link defined
    elseif($member->inGroup($Group->ID) && $Group->GoToMemberProfile == 1)
    {
    return Controller::redirect('/members/profile');
    }

    }

    return Controller::redirect("/home");

    } else {
    if($badLoginURL = Session::get("BadLoginURL")) {
    return Controller::redirect($badLoginURL);

    } else {
    return Controller::redirectBack();
    }
    }
    }

    }
    ?>

    Not this why SS threw a strict notice before tho?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Custom Login in SS3.1 Link to this post

    As the strict notice says, Controller::redirect() is not static so you cannot call it like that, You'll need to do Controller::curr()->redirect($badLoginURL);

  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Re: Custom Login in SS3.1 Link to this post

    Sorry Wiilr, I dont follow - is there anywhere I can read up on "static" to get an understanding of it?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts
    239 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.