Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Redirect user to group page upon login

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 2
Go to End
Author Topic: 720 Views
  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Redirect user to group page upon login Link to this post

    Your code should look like

    <?php
    class GroupDecorator extends DataExtension {

    private static $db = array(
    'GoToAdmin' => 'Boolean'
    );

    private static $has_one = array(
    'LinkedPage' => 'SiteTree'
    );

    }

    And then carry on with that tutorial.

  • the-tech-guy
    Avatar
    Community Member
    13 Posts

    Re: Redirect user to group page upon login Link to this post

    Okay, so I have made some progress,

    But when I try to login I get redirected from mydomain.com/index.php/Security/login? to mydomain.com/index.php/Security/LoginForm which is a blank page.

    Here is the GroupDecorator.php, it appears to work fine in SS3, the cms fields & database rows are created and populated without a problem:
    <?php
    class GroupDecorator extends DataExtension {

    private static $db = array(
    'GoToAdmin' => 'Boolean'
    );

    private static $has_one = array(
    'LinkedPage' => 'SiteTree'
    );

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

    Here is CustomLoginForm.php, now I suspect my problem lies within here:
    <?php
    class CustomLoginForm extends MemberLoginForm {

    public function dologin($data) {
    if($this->performLogin($data)) {
    if(!$this->redirectByGroup($data))
    Director::redirect(Director::baseURL());
    } 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 && $member->inGroup($Group->ID) && $Group->GoToAdmin)
    {
    //redirect to the admin page
    return Director::redirect(Director::baseURL() . 'admin' );
    }
    //otherwise if the member is in the group and that group has a page linked
    elseif($member && $member->inGroup($Group->ID) && $Page = $Group->LinkedPage())
    {
    //direct to that page
    return Director::redirect(Director::baseURL() . $Page->Link());
    }
    }

    return false;
    }
    }

  • the-tech-guy
    Avatar
    Community Member
    13 Posts

    Re: Redirect user to group page upon login Link to this post

    Also I have added both these lines to /mysite/_config.php

    Object::add_extension('Group', 'GroupDecorator');

    Object::useCustomClass('MemberLoginForm', 'CustomLoginForm');

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