Home

Custom Login Form with Group Based Redirection

What it Does

Redirects the user after login. The redirected page is decided by the group the user belongs to and is editable from within the CMS security section via a sitetree dropdown field. There is also a checkbox to redirect to the admin area.

Requirements

Silverstripe v2.3+

Attached Files


Complete.zip(1.4 KB)

Preparation

We will be using 2 files in this tutorial: CustomLoginForm.php and GroupDecorator.php. Create both of these files in your mysite/code directory and fill them will the following:

mysite/code/GroupDecorator.php

Mysite/code/CustomLoginForm.php

Part I: Decorating

We need to add a couple of database fields to the group class as well as their corresponding CMS fields. First lets add our database fields:

mysite/code/GroupDecorator.php

The first function "augmentSQL()" tells silversripe that it's going to need to add some new fields the existing table in the database.

The next function 'extraStatics' is where we define these new fields. Here we define a Boolean to use when deciding if we want our group to go strait to the admin area. We also define a new has_one relationship attaching a siteTree object to our group, which will be the page that we want to redirect to if our GoToAdmin value is false.

Next we want to add our CMS fields to the Group object so that we can select our page from a nice dropdown or check a checkbox to go strait to the admin area. We use a TreeDropdownField and a CheckboxField respectively.

mysite/code/GroupDecorator.php

Note. Even though our has_one relationship was called “LinkPage” we call the TreeDropdown “LinkPageID” as this is what the column in the Group table will be called, as it is referencing a page in another table.

We now need to tell silverstripe to extend the group object with our decorator. We do this by adding the following line to our _config.php file.

mysite/_config.php

Now if you visit www.yoursite.com/dev/build the fields will be added to the database. You can now see in the CMS under security that each group has a checkbox and a tree dropdown. However they don’t do a whole lot just yet.

 

Part II: Creating the Custom Login action.

Our new Loginform is going to be an extension of the MemberLoginForm which holds all of the login actions. We only need to overload (read create a new version of) the dologin() function, as this is the function that normally does the redirecting.

This is what our new function will look like:

Mysite/code/CustomLoginForm.php

First we try to perform the login by calling ($this->performLogin($data)), if we succeed then we move on, otherwise we check for a bad Login and if that returns false we just return the user back to where they were.

If our login was a success then we try to call our redirectByGroup() function (which we will write in a minute) from an if statement. If this returns false then we run the contents of the if and just direct to the base URL. Otherwise if it has returned true then it was run and we don't need to worry. In most cases (hopefully!) this is what will happen, but it's still essential to make sure there is a fallback just in case.

Now let's add our magic function that will redirect the user based on their group.

Mysite/code/CustomLoginForm.php

So first thing we do is get the currently logged in user (i.e. the one that just logged in) followed by all the groups that exist.

We then cycle through each group checking whether the user is in any of them. If they are and that group has GoToAdmin checked then we go straight to the admin page. Otherwise if they are in the group and that group has a page relationship defined we get that page and link to it. If the user is not in any other the groups (very unlikely if not impossible), or the group they are in does not have a pageLink defined then we return false and the doLogin() function takes back control.

Finally we just need to tell SilverStripe to use our custom login class instead of the usual one. Again this is done in the _config.php file.

mysite/_config.php

That's it! You can now create user groups and choose which page they link to on login.

Limitations

If a user is in multiple groups the login will just redirect them to the first group it comes across with that member in. This will usually be the one with the lowest ID. You can manipulate this by reordering the $Groups array before cycling through it in the redirectByGroup() function.

About the Author

Name: Aram

Website: http://www.aabweb.co.uk

Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

Comments (10)



  • I was just doing some web browsing on my Garmin Phone during my break at my work place, and I came across something I thought was interesting .
    It linked over to your website so I clicked over. I can’t really find the relevance between your site and the one I came from, but your site good none the less.thanks for sharing thoes informations , It is interesting, i like it!

    Posted by fm stereo transmitter, 18/08/2010 10:22pm (19 days ago)

  • DsX, is anything being output on that line - such as an \'echo\'? If so remove it.

    Posted by Barry, 14/06/2010 7:41am (3 months ago)

  • I have this up and running in 2.4 however I get a redirect page with some text (done
    Redirecting to /secured... (output started on C:\vhost\projectsite\httpdocs\mysite\code\CustomLogin.php, line 10)
    )

    Is there anyway to avoid having the output and script/line # shown?

    Posted by DsX, 31/05/2010 8:14pm (3 months ago)

  • I am hoping someone may be able to tell me if this will work for SS 2.4
    I will test shortly, but have little experience with 2.4 so I am unlikely able to correct any issues.

    Posted by DsX, 27/05/2010 3:20pm (3 months ago)

  • Hi Aram, I have fixed the problem. It is important that the backend-language is english - not german. Now your script is working fine. But it is possible to change the language to german without the problem? Thank, Lars.

    Posted by Lars, 16/05/2010 8:16am (4 months ago)

  • Hi Aram, I think your script is the perfect solution for me, but I have still the same problem like R. Mashall: I installed the completed files exactly as the tutorial says to. Ran the dev/build and a flush just for good measure. But I don't see the checkbox or drop down fields that are supposed to show up under each group in the Security tab. Please help me. I've got SS 2.3.7 - Many thanks.

    Posted by Lars, 16/05/2010 7:31am (4 months ago)

  • Ok found my error!

    I added the lines into the default SilverStripe _config.php

    and what I should have done was download the _config.php from my server, add the extra lines/code, and reupload it.

    now it works spledit! :D

    Posted by Borriej, 29/04/2010 3:05am (4 months ago)

  • I use SS 2.3.7 btw. Also noticed GroupDecorator.php was missing \'?>\' at the end of the php file. Is this correct? Or was this done for a reason?
    Please help me! I really need this and this is the only documentation for SS @ the moment.

    Posted by Borriej, 28/04/2010 2:20pm (4 months ago)

  • Hi!

    Problem here: downloaded files.

    Edited _config.php

    and uploaded your files to:
    mysite
    and mysite/code

    ran dev/build...

    But nothing happens! Whats wrong? Logged into the CMS, but still the same.

    Please e-mail me, or reply :)

    Posted by Borriej, 28/04/2010 3:47am (4 months ago)

  • I've tried the tutorial, but when I go to security the members list are gone, and when I try to add a new member I get this error: memberTableFields[0] is undefined
    [Break on this error] memberTableFields[0].openPopup(null,addLinks[0].href,tables[0]);

    I use SS version 2.3.6

    Posted by frabraha, 19/03/2010 8:45am (6 months ago)

1 2 3 4 next »

RSS feed for comments on this page RSS feed for all comments

Post your comment