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.

Customising the CMS /

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

User log-in and redirect to different page


Go to End


26 Posts   13490 Views

Avatar
suntrop

Community Member, 141 Posts

29 March 2010 at 3:07am

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

Avatar
patjnr

Community Member, 102 Posts

29 March 2010 at 5:01am

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

Avatar
suntrop

Community Member, 141 Posts

29 March 2010 at 10:08am

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 :D I'll have a closer look at it tomorrow.

Thanks
suntrop

Avatar
suntrop

Community Member, 141 Posts

30 March 2010 at 7:34am

… 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 :-)

Avatar
suntrop

Community Member, 141 Posts

30 March 2010 at 8:38am

Edited: 30/03/2010 8:39am

… 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

Avatar
patjnr

Community Member, 102 Posts

30 March 2010 at 8:53am

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
Avatar
suntrop

Community Member, 141 Posts

30 March 2010 at 9:37am

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
Avatar
suntrop

Community Member, 141 Posts

31 March 2010 at 9:25am

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!

Go to Top