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.

General Questions /

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

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

Custom Login in SS3.1


Go to End


6 Posts   1772 Views

Avatar
lozhowlett

Community Member, 151 Posts

15 January 2014 at 2:05am

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

Avatar
swaiba

Forum Moderator, 1899 Posts

15 January 2014 at 2:36am

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

Avatar
lozhowlett

Community Member, 151 Posts

15 January 2014 at 2:40am

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?

Avatar
Willr

Forum Moderator, 5523 Posts

16 January 2014 at 8:33pm

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);

Avatar
lozhowlett

Community Member, 151 Posts

17 January 2014 at 2:23am

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

Avatar
Willr

Forum Moderator, 5523 Posts

17 January 2014 at 5:25pm