Landing Pages for users on login

Permalink 4 users found helpful
Hi Guys,

Is it possible to assign a landing page to a group on login, so example groupA on login go to the home page, while GroupB on login go to the services page, while groupC on login go to the news page?

At the moment the landing page seems to affect everyone except the administrators who can be redirected to the dashboard.

Perhaps one for future releases?

I'm happy to put in a hack if anyone has any ideas?

Cheers
Al

engagingit
 
senshidigital replied on at Permalink Reply
senshidigital
Dont think it can be done at the moment but I would love this feature to be added to C5. All users have to be directed to the same page. (if there is a 'hack' tell me please)

I want users to login and be directed to a specific page that has been created and only they can edit.
jordanlev replied on at Permalink Reply
jordanlev
You may be able to do this using events. I haven't done this myself so I don't know if this will work, but try it out:

* Add this line to your config/site.php file:
define('ENABLE_APPLICATION_EVENTS', true);

* Create a new file in your config folder called "site_events.php" and put this code in it:
Events::extend('on_user_login', 'LoginUser', 'login_redirect', 'models/login_user.php');


* create a new file in your models folder called "login_user.php" and put this code in it:
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
class LoginUser extends Object {
public function login_redirect($user) {
   if ($user->inGroup(Group::getByName('GroupB'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/services');
      exit;
   } else if ($user->inGroup(Group::getByName('GroupC'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/news');
      exit;
   }
}
}


If that works please come back here and let me know -- I see that this question has been asked before so we should turn it into a how-to if it actually works.

Good luck!

-Jordan
senshidigital replied on at Permalink Reply
senshidigital
I see this has been answered so I take it it worked?

Say I had 30 users I would love to add a new page for each of them so that when they login, they go straight to it.

Will this code do that? Looking at the code I would have to add new line of code for each group/user.

I would love for my client to be able to add a page and set who can access it on login.

Would be good if it could done by adding a new user attribute that lets the user select a page from the sitemap that they would go to.

A how to vid or whatever would be cool!
engagingit replied on at Permalink Reply
engagingit
Cheers, I'll try it over the weekend and let you know how I go.

Thanks again
Al
engagingit replied on at Permalink Reply
engagingit
I get this error when I try it, shows at the top of every page I try and load??

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/config/site_events.php:2)

Thanks
Al
jordanlev replied on at Permalink Reply
jordanlev
Hmm... Can you post your login_user.php file here (you may need to change it's extension to .txt so the forum doesn't block it)? I think there might be a typo somewhere.
engagingit replied on at Permalink Reply
engagingit
as requested

Thanks for your help
Al
engagingit replied on at Permalink Reply
engagingit
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
class LoginUser extends Object {
public function login_redirect($user) {
   if ($user->inGroup(Group::getByName('FleetManager'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/how-it-works');
      exit;
   } else if ($user->inGroup(Group::getByName('GroupC'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/news');
      exit;
   }
}
}
?>
engagingit replied on at Permalink Reply
engagingit
Ok so found a work around that works for me.

I created an attribute for each user called default_page. I then put the url in there that I want them to find after login.

Basically I created a landing page that all users go to when they login.I added the user info block and show the default_page attribute.

I then create a little bit of script on the landing page using jquery which detects the default_page and redirects the page.

Not the most elegant solution but it works for what I need.
FatTony1952 replied on at Permalink Reply
FatTony1952
I am trying this method also. My client has only two groups, but those two groups may have 100 users each.

When I tried this method, I get this error when trying to login:


Fatal error: Call to undefined method LoginController::inGroup() in /home/user/public_html/root/models/login_user.php on line 5

This is my line 5:

if ($user->inGroup(Group::getByName('prospects'))) {

The two group names are 'Prospects' and 'Employees'.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Ahh yes, there is an error in the code above. Not sure why I didn't catch that before (the error message you posted was very helpful, thanks).

I think you want this instead:
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
class LoginUser extends Object {
public function login_redirect(&$loginController) {
   $user = new User();
   if ($user->inGroup(Group::getByName('Prospects'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/prospects-page');
      exit;
   } else if ($user->inGroup(Group::getByName('Employees'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/employees-page');
      exit;
   }
}
}
?>

(obviously you will want to change the URL that is being redirected to -- '/propsects-page' and '/employess-page').

Please post back here and let me know if that works or not. Perhaps I can turn this into an easy-to-use addon.

Thanks!

-Jordan
FatTony1952 replied on at Permalink Reply
FatTony1952
I'll be working on this project tomorrow and I'll let you know how this solution worked.

I did notice that you put in the loginController function. I guess that's why it couldn't find it on line 5 eh?
Hkofoed replied on at Permalink Reply
I'm listening in on this one:-) I also need to redirect users on login. I need to redirect to different cID's from different login blocks. For now I'm using the 'when login is complete, return users to this page' function, by creating a blocks/login/mytemplate.php and changing ... echo $c->getCollectionID(); to echo cID;(cID = number of the page i'm redirecting to). Not an elegant solution, but works for a quick solution:-)
FatTony1952 replied on at Permalink Reply
FatTony1952
Worked great jordan!

This thread needs to have your last code marked as the solution instead of the original code.

This saves me from having to redirect hundreds individually....can you say timesaver?

Thanks again!
engagingit replied on at Permalink Reply
engagingit
Marked it as best answer
acmarketing replied on at Permalink Reply
I realize it's been a year since this last post, but wanted to know if the error was resolved. I tried the code marked Best, but I also received the following error message:

Events::extend('on_user_login', 'LoginUser', 'login_redirect', 'models/login_user.php');
Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/config/site_events.php:1) in /home/public_html/concrete/libraries/view.php on line 843
jordanlev replied on at Permalink Reply
jordanlev
The original error was resolved with the code from the "best answer". It looks like you're having a different problem. If the "Events::extend..." thing is actually showing up above your error message, then that means the problem is you aren't running the php code inside a <?php tag.

Make sure you have an opening <?php at the very top of the file you put that code in (and make sure there is no white space before it -- no empty lines or spaces or tabs).
alanstr84d replied on at Permalink Reply
Hi

Thanks for posting this up. It seems to be the perfect solution to what I'm trying to achieve, however I am having a similar problem to acmarketing. I get the following error message:

Warning: Parameter 1 to LoginUser::login_redirect() expected to be a reference, value given in /Applications/MAMP/htdocs/concrete5.6.1.2/concrete/core/libraries/events.php on line 176
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/concrete5.6.1.2/concrete/core/libraries/events.php:176) in /Applications/MAMP/htdocs/concrete5.6.1.2/concrete/core/libraries/controller.php on line 368


I'm sure it's something very simple but I'm fairly new to php so I can't spot it!

Any help appreciated. Thanks!
alexbrandsen replied on at Permalink Reply
Just for people who have the same problem, I solved this issue by removing the '$' in front of '$loginController'. Hope it helps someone. :)
PatrickHenry replied on at Permalink Reply
PatrickHenry
Sorry to necro this post, but I can't seem to get it to work, nor do I have any errors?
On the 'site_events.php' page I created in /config folder I have the following:
defined('C5_EXECUTE') or die(_("Access Denied."));
Events::extend('on_user_login', 'LoginUser', 'login_redirect', 'models/login_user.php');

NOTE: I added the 'define('C5_EXECUTE') or die(_("Access Denied.")); line as it wasn't in your original directions but had a hunch it should be. I removed it and tested and couldn't tell any difference, so I've left it for now.

Then in the /models/login_user.php, I have the following:
defined('C5_EXECUTE') or die(_("Access Denied."));
class LoginUser extends Object {
public function login_redirect(&$loginController) {
   $user = new User();
   if ($user->inGroup(Group::getByName('Applicants'))) {
      header ("Location: ".BASE_URL.DIR_REL.'/admissions/apply/applicant-registration/application-admission/');
      exit;
   } 
}
}


At the moment, I only need to redirect 1 group, but I know I'll have many more in the near future. Am I correct in thinking that this should only apply to the applicants group. All other user will be redirected to the homepage as the system setting is, except for admins who'll go to the dashboard. If I'm using this, do I need to define those too, or allow the system to default it?
Thanks in advance!
jordanlev replied on at Permalink Reply
jordanlev
I believe your code does what you want it to. But you can test it out fairly easily yourself by creating a new user in that group and then logging in with their credentials and seeing if it takes you to the right place.
grafoman replied on at Permalink Reply
Any idea how to do this for concrete 5.7.x ?