Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla: redirect to login page if user is not logged in and back in custom component

Here's what I want to do: I have a custom component which, in one of its views, requires the user to be logged in. If he isn't, for now, I only throw an Exception, and thats really ugly.

So I want to redirect the user to the login page. THAT is not the problem, I know how to do it.

BUT. There sure is a way of temporarily "overriding" the default redirect after login-page to the view the user wanted to see. In fact, the menu items do it, for instance. The problem is: I can't figure it out. Does anyone know how to override the default redirect of the login-page ONLY FOR ONE CALL?

like image 957
Khaarkh Avatar asked Sep 04 '14 07:09

Khaarkh


3 Answers

You don't need to create a plugin in order to do this.

The com_user component accepts a return parameter to know where to send the user after login. This is used exactly in the case you described: The user tried to access a restricted area, he is redirected to the login page, and returns to the page he tried accessing before.

For that, you do what @Brian suggested, but with a slight change:

$app = JFactory::getApplication();
$message = "You must be logged in to view this content";
$url = JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode('YOUR COMPONENT VIEW URL HERE')
$app->redirect($url, $message);
like image 152
pedromanoel Avatar answered Jan 04 '23 00:01

pedromanoel


There are 3 ways to achieve it.

  1. If you have menu item for your custom component view then there is an option in menu item page as Access. Just select the registered option and that it. Now when user tried to access that page it will prompt user to login to see that page. enter image description here

  2. You can create a plugin or call that plugin from where that page get load.

  3. Or you can directly add this code to your's views ,default file.

<?php
$user = JFactory::getUser();
if($user->id=='' || $user->id==NULL){
// Your message here.
}else{

// Your code here
}
?>

Hope this will give you an idea.

like image 41
Toretto Avatar answered Jan 04 '23 01:01

Toretto


You could create a plugin, but there's a more elegant way to accomplish using back-end or inline your existing code.

Instead of throwing the exception, use this line:

JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_users&view=login', JText::_("You must be logged in to view this content"));

Another option is to create a placement menu reference. This is a menu group which is not attached to a module and displayed on the website. It is used for a variety reasons, but in this instance you will create a menu reference and assign it the restricted access level.

Now, if you want to user to be able to access via a displayed menu option, you simply convert the public reference to type of Menu Alias and point to the placement menu reference with the access level restrictions.

If you just want to use embedded anchor links, you can use the placement menu alias as the href attribute to achieve the same result.

Having the user automatically routed to the log in page.

I would use one of these two options versus a user plugin.

like image 32
Brian Bolli Avatar answered Jan 04 '23 00:01

Brian Bolli