Jump to:

5552 Posts in 1742 Topics by 1226 members

Customising the CMS

SilverStripe Forums » Customising the CMS » User log-in and redirect to different page

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

Page: 1 2 3 4
Go to End
Author Topic: 6952 Views
  • suntrop
    Avatar
    Community Member
    131 Posts

    User log-in and redirect to different page Link to this post

    Hi there. I am new to SilverStripe and just working a few days with it. It looks pretty good and I love the flexibility of SS.

    I want to give all my customers a page where I can post some stuff related to them (news, offers etc.).

    I am wondering if there is a simple solution to give different users (after a log-in) a different version of a page or redirect the user to a page associated with him.
    The "external auth" module seems to be only able to log-in and all see the same content.
    Build one page for each customer and restrict permission to only one customer doesn't work either. I need a form that identifies the user and redirects him automatically.

    Anybody some help for me?

    Thanks!
    suntrop

  • patjnr
    Avatar
    Community Member
    101 Posts

    Re: User log-in and redirect to different page Link to this post

    Welcome to SS

    here we go.

    in your _config.php add the following lines

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

    create a page called CustomLogin.php and paste the code below which will do all the work for you.

    <?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))
                   Director::redirect(Director::baseURL());
                //echo 'done';
          } else {
             if($badLoginURL = Session::get("BadLoginURL")) {
                Director::redirect($badLoginURL);
             } else {
                Director::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
                 Director::redirect(Director::baseURL() . '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->LinkPageID != 0)
             {   
                //Get the page that is referenced in the group      
                $Link = DataObject::get_by_id("SiteTree", "{$Group->LinkPageID}")->URLSegment;
                //direct to that page
                Director::redirect(Director::baseURL() . $Link);
                return true;
             }
             
          }
          //otherwise if none of the above worked return fase
          return false;
                
       }
                   
       
    }   
    ?>

    after you have rebuild the db you will notice that for each group you will create, you have to apecify whether it access the admin area by checking admin area or specify a page to be redirected to. failure to specify where to take that group will result in nothing happening after log in.

    create CustomerPage.php and change the access level to Login users

    Under security create a secondary Users called Customers and specify the redirect page to be the newly created customers page.
    So now you will be having Administrators and Customers.

    yell if problems

    ta

    PAt

  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: User log-in and redirect to different page Link to this post

    Thanks for your answer and help, PatJnr.

    It looks like the solution The Sapphire framwork seems to be cool. I didn't expect you need "just a few lines" for that.
    Anyway, it is Sunday and pretty late I'll have a closer look at it tomorrow.

    Thanks
    suntrop

  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: User log-in and redirect to different page Link to this post

    … it seems the GroupDecorator Plugin is missing. I found http://www.ssbits.com/custom-login-form-with-group-based-redirection/ and come back later if it doesn't help

  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: User log-in and redirect to different page Link to this post

    … uh uh uh. Now there is a tab 'Members' but I can't specify where to redirect.

    More disturbing is that I can't see any of the existing members and I can't add new ones.

    What to do now?

    I allready ran /dev/build?flush=1 and /db/build?flush=1 (don't know where the differnce is) and cleard the browser cache.
    The 'Group' table in my DB contains the newly created fields 'GoToAdmin' and 'LinkPageID'. Both filled 0.

    Hope you or someone else can help. A CMS without members isn't fun at all.

    BTW: I am using SS 2.3.7

  • patjnr
    Avatar
    Community Member
    101 Posts

    Re: User log-in and redirect to different page Link to this post

    Hi

    (zero) 0 means no redirect page. even if anyone logs in thy wont go to either admin or Customer page.

    i have attached what you should do on the Administrators group.
    you should tick the check box "Go to Admin area"

    on other groups select a page from the created drop down list.

    (Pay mind not to create duplicate users with the same login email. the redirect is powerful, they wont access the admin unless you delete them from other group which will be redirected after login)

    yell if problems

    ta

    PAt

    Attached Files
  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: User log-in and redirect to different page Link to this post

    Hi PatJnr.

    Unfortunately there is nothing I can tick. The tab is purely empty. The last tab (in my screenshot Members) is empty as well.

    Any ideas what went wrong?

    Attached Files
  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: User log-in and redirect to different page Link to this post

    Hi again. The empty tab was because I am using a translated interface so in my case it isn't Memebers but Mitglieder. So SS has created a new tab Memebers and made both empty.

    I renamed the tabs now …

    public function updateCMSFields(FieldSet &$fields) {
        $fields->addFieldToTab("Root.Mitglieder", new CheckboxField("GoToAdmin", " Go to Admin area"), 'Mitglieder');
        $fields->addFieldToTab("Root.Mitglieder", new TreeDropdownField("LinkPageID", "Or select a Page to redirect to", "SiteTree"), 'Mitglieder');
       }

    … but the new Checkbox and TreeDropdown arn't there! Does anybody have some help for that?

    Thanks!

    6952 Views
Page: 1 2 3 4
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.