login state in top.links when called - magento

I'm trying to pull the header of our Magento store in a standalone php page. Everything works as expected except the 'Log In' link does not appear. The customer.xml file uses the standard 'customer_logged_in' node to 'addLink' but it seems like the login status isn't getting assessed with the method I'm using. How do I get this Log In | Log Out link to display?
Here is the code I'm using:
require_once $mage_path;
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
$layout = Mage::app()->getLayout();
$layout->getUpdate()->addHandle('default')->load();
$layout->generateXml()->generateBlocks();
echo $layout->getBlock('header')->toHtml();
I'm able to get the correct login state independently using the following:
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
if ($session->isLoggedIn()) {
/* logged in */
} else {
/* not logged in */
}
However, I don't want to manage two different styles (one through the default magento XML and another for this custom page). I would rather have the getBlock call return the whole block with the correct login status. Any insight is appreciated.

You need to add the customer_logged_in to your handles, as well as default. For example:
...
$handles = array('default');
if (Mage::helper('customer')->isLoggedIn()) {
$handles[] 'customer_logged_in';
}
$layout->getUpdate()->addHandle($handles);
...

Related

You are not authorised to view this resource - Joomla

I am using joomla 2.5.9 version, and I would like Joomla to redirect me to the login page if I am not logged in when i click an article which the Permission Access is for Registered only, but instead Joomla returns me this message: You are not authorised to view this resource.
And I dont see any reason why joomla by default havent made it redirect to login page.
Thanks
This doesn't answer your exact question, but I think it's a good workaround. I'm working on the same issue. My approach at the moment is to check the messages for the "not authorised" string, and then set a flag based on that. You can then check that flag anywhere in template and either redirect, or just choose to optionally show the login form.`
/* get message from app */
$app = JFactory::getApplication();
$messages = $app->getMessageQueue();
/* set login flag to 0 */
$showlogin = 0;
/* if there is a message set... */
if (isset($messages[0])) {
/* loop through messages and check for the "not authorised" string */
foreach ($messages as $msg) {
if ($msg["type"] == "error" && strpos($msg["message"], "not authorised") ) {
/* if found, update login flag */
$showlogin = 1;
}
}
}
/* include in template body - you could redirect here instead of including login form */
if ($showlogin) { ?>
<jdoc:include type="modules" name="login-form" style="none" />
<?php } ?>
`
this happens when you try to access an article which is not visible, but the category is publically visible.
Seems like it is not considered a bug, but I think its a pretty unexpected "feature".
To fix this you can edit:
joomla/components/com_content/views/article/view.html.php
// Check the view access to the article (the model has already computed the values).
if ($item->params->get('access-view') == false && ($item->params->get('show_noauth', '0') == '0'))
{
$app->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error');
$uri = urlencode(base64_encode(JURI::getInstance()->toString()));
JFactory::getApplication()->redirect(
JRoute::_('index.php?option=com_users&view=login&return='. $uri, false)
);
return;
}
This will show the login screen and return to the article after a succesfull login.
If you dont want to edit the core file (because you want to update your system), you have to create a system plugin to override this.

How to determine the authorization in Joomla

After login i use need to set up redirect to custom page. How to catch this authorization in onAfterRoute event?
You should go to this path:
JOOMLAROOT/components/com_user/controller.php
in function register_save(), find this code:
if ( $useractivation == 1 ) {
$message = JText::_( 'REG_COMPLETE_ACTIVATE' );
} else {
$message = JText::_( 'REG_COMPLETE' );
}
after line put this code:
$this->setRedirect('/Your Custom Page Address', $message);
Why not just use the built in redirect in either the Joomla user login menu item or the standard Joomla login module. Both offer the option to redirect a user after a successful login. In the case of the module, you would need to create a menu item pointing to the custom page, but that's easy enough to do.
Is there something you need to do other than just a simple redirect? If not, then just use the system as it is designed.
I would create a small plugin that handles the redirect after login.
After a user has been logged in, the event onUserLogin is triggered, and you could simply do a redirect when the event is called.
Avoid any core hacks, since you'll allways end up having a hazzle during updates.
The code for a plugin like this could look like this:
class plgAuthenticationMyredirect extends JPlugin{
function onUserLogin ($user, $options){
$link = 'index.php?option=.....';
$msg = 'Message to show after login';
$app = JFactory::getApplication();
$app->redirect($link, $msg);
}
}

MAGENTO - Registration form (+validation and callback) outside magento files?

I'm trying to integrate a php file inside an aplication that gives users the posibility to register an account. This account has to be registered inside a magento store.
This is what i have to this momment:
<?php
require_once '../../../app/Mage.php';
umask(0);
Mage::app('default');
Mage::app()->getTranslator()->init('frontend');
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
// get layout object
$layout = Mage::getSingleton('core/layout');
//get block object
$block = $layout->createBlock('core/template');
//print_r(get_class_methods(get_class($block))); <- use for seeing classes
$block = $block->setTemplate('customer/form/register.phtml')->renderView();
echo $block;
?>
This code renders the registration form but stops when he is showing the input fields. I tried with "mini.login.phtml" and it renderes correctly. I'm not very good at Magento, or english. I can provide any other information if necessary.
Any help will be appreciated!
Regards
You are using the wrong block type when dynamically creating the block. Try using this:
createBlock('customer/form_register')

Magento Session not working in external page (same domain)

Magento Session in external page (same domain) is not working well, I've checked all the other topics here but any solution it is working.
require_once ( "../app/Mage.php" );
umask(0);
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
if($session->isLoggedIn()){
//do it
} else {
// Transfer them to a login page
header("Location: http://www.mydomain.com/customer/account/login/");
}
I can not get this to work, I have checked all recommendations everywhere and nothing work.
Just for your information, after the header() function, there should be another line:
exit;
for the redirection to work.
(Apparently this belongs more to a comment rather than an answer, but I don't have the privilege to comment everwhere yet.)
You can try these lines instead, they looks similar, but work for me on Magento 1.5.1..
require_once ( "../app/Mage.php" );
umask(0);
Mage::app("default");
if(Mage::getSingleton('customer/session')->isLoggedIn()){
//do it
}
else {
// Transfer them to a login page
header("Location: http://www.mydomain.com/customer/account/login/");
}
I think your issue is that you already have a session started, so your attempts to start a magento session and get data from it are failing.
If you look inside Mage_Core_Model_Session_Abstract_Varien, the first thing it does is look to see if the $_SESSION variable is already set and return if it is. You could set your debugger breakpoint there and verify if you aren't sure.
You could close the other session and start the magento session to get your data, or you could get both to share the same session

Magento - Cannot get cart info into an external page

I have magento installed in a folder called store. The min site is installed above that at the root of the hosting.
I have followed many ideas on how to get the cart to display on external pages, but I cannot seem to get any info passed from magento.
<?
$mageFilename = 'store/app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
/* Init User Session */
$session = Mage::getSingleton('customer/session', array('name'=>'frontend'));
if ($session->isLoggedIn()) {
echo'logged in <br />';
/* do something if logged in */
} else {
echo'not logged in<br />';
/* do something else if not logged in */
}
/* Magento uses different sessions for 'frontend' and 'adminhtml' */
Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo 'cart items count: ' . $cart;
?>
I have this code at present to call the number of items in the cart, and also state whether the user is logged in or not. But nothing seems to get passed to it.
I believe you must run this from a Magento Controller.
Magento is an incredibly powerful, advanced, professional PHP platform. It is one of the most advanced PHP MVC frameworks out there. If you're trying to build a PHP site that has a Magento store in it, why wouldn't you use Magento to build the whole site? Are you using a better platform?

Resources