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.

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

Redirect user to group page upon login


Go to End
Reply


15 Posts   2610 Views

Avatar
Willr

Forum Moderator, 5523 Posts

24 February 2014 at 7:43pm

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.

Avatar
the-tech-guy

Community Member, 13 Posts

3 April 2014 at 6:38pm

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

Avatar
the-tech-guy

Community Member, 13 Posts

3 April 2014 at 6:39pm

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

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

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

Avatar
garethjpeters

Community Member, 2 Posts

17 June 2017 at 1:30am

Just wondering if this was resolved? I'm at the same place as the-tech-guy, the redirect just doesn't seem to be working. It keeps redirecting to the BackURL parameter even if I delete this from the URL. Would love some help! Thanks.

Avatar
martimiz

Forum Moderator, 1391 Posts

22 June 2017 at 1:08am

Edited: 22/06/2017 1:10am

Like the previous posts, this is based on http://www.ssbits.com/tutorials/2009/custom-login-form-with-group-based-redirection/, with me experimenting a bit further, for you to adapt as you wish, see inline comments :)

Creating the fields for the group to select a linked page:

class GroupExtension extends DataExtension
{
    private static $db = array(
        'GoToAdmin' => 'Boolean'
    );

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

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

Note that I've replaced 'Member' by 'Description', because on creating a new group, the Member GridField is not yet present, so placing fields 'before' Member will make them not appear until the Group has been saved. But feel free to replace it.

To handle redirect:

class CustomLoginForm extends MemberLoginForm
{

    public function dologin($data)
    {

        if ($member = $this->performLogin($data)) {
            if (!$this->redirectByGroup($data, $member))
                $this->controller->redirect(Director::baseURL());
        } else {
            if ($badLoginURL = Session::get("BadLoginURL")) {
                $this->controller->redirect($badLoginURL);
            } else {
                $this->controller->redirectBack();
            }
        }
    }

    public function redirectByGroup($data, $member)
    {
        // get the first group this member belongs to. If the user belongs to multiple groups with multiple
        // redirection rules, we have a problem, so let's assume the 'redirectable' user belongs to
        // just the one group...
        $group = $member->Groups()->first();

        if (isset($group->ID)) {

            // go to admin overrules linkedPage setting (because the tree dropdown won't let you
            // uselect a page)
            if ($group->GoToAdmin) {
                return $this->controller->redirect(Director::absoluteURL('/admin'));
            } else {
                $page = $group->LinkedPage();

                // if no page is selected, ss returns ID=0 and Link() = '/', so check the ID
                if (!empty($page->ID)) {
                    return $this->controller->redirect(Director::absoluteURL($page->Link()));

                    // added this, so for instance the /admin link still works even if GoToAdmin = false,
                    // may be necessary for other BackURLs as well???
                } elseif (!empty($data['BackURL'])) {
                    return $this->controller->redirect(Director::absoluteURL($data['BackURL']));
                }
            }
        }
        return false;
    }
}

In /mysite/_config.yml:

# Add the GroupExtension to the Group class
Group:
  extensions:
    - GroupExtension
# use the CustomLoginForm instead of the default MemberLoginForm:
Injector:
  MemberLoginForm:
    class: CustomLoginForm

Next: do a ?flush=all to load the new settings

Now you can login using yoursite/Security/login or yoursite/admin

Avatar
garethjpeters

Community Member, 2 Posts

22 June 2017 at 1:41am

Thank you thank you martimiz! You have ended hours of head scratching and hair pulling! All works like a dream now. Cheers!

Avatar
TGx`

Community Member, 10 Posts

24 June 2017 at 3:13pm

Thank you infomation.

sbobet mobile

Go to Top