07
Apr
24
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 startup aab web. He recently went full time into web development after leaving his previous career in 3D animation behind.
Comments (10)
-
This give error:
if($this->performLogin($data)) {
04. if(!$this->redirectByGroup($data))
05. Director::redirect(Director::baseURL());
I've resolved with:
if($this->performLogin($data)) {
// if(!$this->redirectByGroup($data))
//Director::redirect(Director::baseURL());
$this->redirectByGroup($data);
} else {Posted by Biagio Paruolo, 17/02/2010 4:18am (21 days ago)
-
I've got SS 2.3.3 and I installed the completed files exactly as the tutorial says to. Ran the dev/build and a flush just for good measure. I don't see the checkbox or drop down fields that are supposed to show up under each group in the Security tab. Please advise..
Posted by R. Marshall, 12/01/2010 1:41pm (2 months ago)
-
Cheer man, works flawlessly in 2.3.4.
Posted by Nisse, 11/01/2010 9:33pm (2 months ago)
-
Hi Anaya
I havn't actually tried that, not sure how easy it is but it should be possible. I will have a play when I get a min, in the mean time might be worth posting on the forum.Posted by Aram, 25/11/2009 3:20am (4 months ago)
-
Hi Aram,
Thanks for this great script!
It works fine...But as it uses admin login template(i have security_login.ss template), the login message is confusing to general users...
Just would like to know whether its possible to create seprate template for CustomLoginForm..If so in which folder can I upload that???
Regards
Anaya.Posted by Anaya, 24/11/2009 8:40am (4 months ago)
-
I started learning SliverStripe today and attempted to get Customlogin working, but nothing happened. I followed the instructions and I see no checkbox or login form.
Please advise. thanksPosted by josephine, 12/10/2009 12:55pm (5 months ago)
-
Thta was an inspiring post,
thanks for sharing this, it has helped so much.. ive left with no more questions...
Thanks for writing about itPosted by software development uk, 12/10/2009 7:07am (5 months ago)
-
This is the best documentation I've found about using Decorators. The SS documentation doesn't mention that you have to include the 'augmentSQL' function.
Posted by Brad Touesnard, 05/08/2009 10:34pm (7 months ago)
-
@Terry - I'm not sure about this off the top of my head, not been in that situation before. I'm sure there is a solution though, have you tried the forum?
@nick - yea I have noticed that on a couple of sites, although strangely id doesnt seem to happen all the time. I will take a look into it when I get some time :)Posted by Aram , 28/07/2009 4:32am (8 months ago)
-
This is great, just what I needed...one small niggle I have is that after I have logged in I get a white screen with "Done. Redirecting to ......" flash up before I get to the redirected page. Any ideas...??
Posted by nick jacobs, 27/07/2009 8:35pm (8 months ago)
RSS feed for comments on this page RSS feed for all comments