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, biapar, Willr, Ingo, swaiba, simon_w

Redirect user to group page upon login


Reply

11 Posts   757 Views

Avatar
Willr

24 February 2014 at 7:43pm Forum Moderator, 5511 Posts

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

3 April 2014 at 6:38pm Community Member, 13 Posts

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

3 April 2014 at 6:39pm Community Member, 13 Posts

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

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

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