Magento: Fire event before the login process - events

After reading some posts, I'm trying to implement an observer to fire an event before the user login. I'll explain: I have a forum and a blog, using another framework, and I want the visitor to log only once: blog, forum or eshop. After that, he will be logged for other areas. For example, I am browsing the forum and I login into the forum. Then, when I open the shop, I am already logged in. And same thing for the opposite way.
It works, but just one problem: when I logged into the forum/blog and if I open the shop, I need to refresh the page to see that I am logged.
Actually, it's the same behavior as the standard Magento logout process: if you click on the link "logout" of your Magento shop, you will still see that you are logged in. There's a redirection/refresh to the homepage and then you see the message that you are logged out.
Here is my code:
app/code/community/Fanxiang/UC/etc/config.xml:
[...]
<events>
<http_response_send_before>
<observers>
<UC>
<type>model</type>
<class>Fanxiang_UC_Helper_Data</class>
<method>ucSynlog</method>
</UC>
</observers>
</http_response_send_before>
</events>
[...]
and:
app/code/community/Fanxiang/UC/Helper/Data.php
class Fanxiang_UC_Helper_Data extends Mage_Core_Helper_Abstract
{
public function ucSynlog($observer){
if(!empty($_COOKIE['Example_auth'])){
list($Example_uid, $Example_username) = explode("\t", uc_authcode($_COOKIE['Example_auth'], 'DECODE'));
list($Example_uid, $Example_username,$email) =uc_get_user($Example_username);
$customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);
Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
} else {
Mage::getSingleton('customer/session')->logout();
}
Mage::log(time(), null, "logfile.log");
}
}
PB: I'd like to avoid to refresh the page to see that I am logged in.
Any idea or help is welcomed!
Yoong

As you've seen http_response_send_before occurs after the HTML is generated but before it is sent by the server, so the logged in status is not visible. You need to use an event that occurs before any HTML is put together, say, controller_action_predispatch.
Here is a reference of several other possibilities

The reason for the odd behavior seems to be related to the fact that the entire framework for Magento spins up before really getting around to controllers and such. This means that your previous login state (logged out, in this case) is recorded before your code is run. It's my guess (though I haven't run through all the code) that you could reset a few session/registry variables during login to solve this problem.
If you do this, please contribute it back to Magento so that we can all share :)

Related

Hello World Magento Plugin

I'm new to magento & am trying to get a simple plugin working.
When a user adds a product to the basket I'd like magento to display a dialog box with some text in it.
Can anyone provide a link to a guide to this or something similar?
I'm using magento2
Thanks
EDIT:
Thanks for that,
I found a tutorial and this is what I have atm, the plugin looks to be enabled as far as I can see.
What I'm trying to get to working is a message to display when a user adds a product to the cart.
Currently the text on the add to cart button changes to 'Adding..' and hangs. How can I debug this?
Thanks for that,
I found a tutorial and this is what I have atm, the plugin looks to be enabled as far as I can see.
What I'm trying to get to working is a message to display when a user adds a product to the cart.
Currently the text on the add to cart button changes to 'Adding..' and hangs. How can I debug this or fix this?
<?php
namespace Acme\AddToCartMessage\Plugin;
use Magento\Checkout\Model\Cart\CartInterface;
use Magento\Framework\Message\ManagerInterface as MessageManager;
class AddToCartMessage{
private $messageManager;
public fucntion __construct(MessageManager $messageManager){
$this->messageManager = $messageManager;
}
public afterAddProduct(\Magento\Checkout\Model\Cart\CartInterface $cart, $result){
$this->messageManager->addNoticeMessage('Testing');
return result;
}
}
There are actually a couple ways to achieve what you're trying to do. The cleanest way would be to utilize event observers. Here's a link to their documentation - Magento 2 - Events and Observers.
A quick overview:
Create an etc/frontend/events.xml file.
Create an observer for the checkout_cart_add_product_complete event.
Inject the \Magento\Framework\Message\ManagerInterface into your observer class.
public function __construct (
\Magento\Framework\Message\ManagerInterface $messageManager
) {
$this->_messageManager = $messageManager;
}
The message manager will display a notification after the item has been added. To show a popup, you should look into M2's JS components - JavaScript Developer Guide.

Rails: How to post on my main facebook page through API?

I have the following permissions:
create_note, email, offline_access, photo_upload, publish_stream, read_stream, share_item, status_update, manage_notifications, read_friendlists, video_upload, user_relationships, user_status, user_photos
I can do post on the user timeline (www.facebook.com/username) but I can't see how to post on the user's wall (www.facebook.com)
To post on the user timeline, I use a code something like:
if I need to attach an image to the post
client.put_connections("me", "feed", attachments, attachments)
and
if I don't need to attach an image to the post
client.put_connections("me", "links", attachments, attachments)
both are working nice, but none of them really publish the message on the wall (www.facebook.com as a logged in user)
So, I just wondering if it is a normal behaviour or am I just missing something?
Thank you.
The same behavior I can see for other users those are sharing update from API. I think it's normal and we need not to worry about it.
As per me : We can't not say this is required because getting our own feed in 'News Feed' section is not beneficial at all.

Front-end Ajax in ModX Revolution

What's the proper way for implementing front-end Ajax functionality in ModX Revolution? I like the idea of connectors and processors, but for some reason they are for back-end use only - modConnectorResponse checks if user is logged in and returns 'access denied', if he is not.
Inserting a snippet into resource and calling it by resource URL seems a one-time solution, but that doesn't look right to me.
So how do I get safe Connector-like functionality for front-end?
So, as boundaryfunctions said, it's not possible and ModX developers recommend using a resource with a single snippet included. But for those who despite the will of developers look for Connector-like functionality, there may be a solution made by guess who-- ModX core developer splittingred in Gallery extra. In connector.php, before handleRequest() call, there's a code that fakes authorisation:
if ($_REQUEST['action'] == 'web/phpthumb') {
$version = $modx->getVersionData();
if (version_compare($version['full_version'],'2.1.1-pl') >= 0) {
if ($modx->user->hasSessionContext($modx->context->get('key'))) {
$_SERVER['HTTP_MODAUTH'] = $_SESSION["modx.{$modx->context->get('key')}.user.token"];
} else {
$_SESSION["modx.{$modx->context->get('key')}.user.token"] = 0;
$_SERVER['HTTP_MODAUTH'] = 0;
}
} else {
$_SERVER['HTTP_MODAUTH'] = $modx->site_id;
}
$_REQUEST['HTTP_MODAUTH'] = $_SERVER['HTTP_MODAUTH'];
}
Works for me. Just need to replace first if condition with my own actions.
UPDATE: I forgot to mention that you need to pass &ctx=web parameter with your AJAX request, because default context for connectors is "mgr" and anonymous users will not pass policy check (unless you set access to the "mgr" context for anonymous users).
And also the code from Gallery extra I posted here seems to check some session stuff that for me doesn't work with anonymous front-end users (and works only when I'm logged in to back-end), so I replaced it with the next:
if (in_array($_REQUEST['action'], array('loadMap', 'loadMarkers'))){
$_SESSION["modx.{$modx->context->get('key')}.user.token"] = 1;
$_SERVER['HTTP_MODAUTH'] = $_REQUEST['HTTP_MODAUTH'] = 1;
}
I don't know if this code is 100% safe, but when anonymous user calls it, he doesn't appear to be logged in to Manager, and when admin is logged in and calls the action from back-end, he is not logged off by force. And that looks like enough security for me.
This solution is still portable (i.e. can be embedded into distributable Extra), but security should be researched more seriously for serious projects.
As far as I know, this is not possible in modX at the moment. It has already been discussed on the modx forums and filed as a bug here, but it doesn't look like anybody is working on it.
There are also two possible workarounds in the second link. Personally, I would favour putting the connector functionality into the assets folder to keep the resource tree clean.
There's a more complete explanation of the technique used in Gallery here:
http://www.virtudraft.com/blog/ajaxs-connector-file-using-modxs-main-index.php.html
It allows you to create a connector to run your own processors or a built-in MODX processors without creating a resource.

FB.ui permissions.request issue in new OAuth window

I having an issue with FB.ui permissions.request window.
FB.ui({
method: 'permissions.request',
perms: 'publish_actions',
display: 'popup'
},function(response) {
// This function is never called ? });
Context :
I use the new OAuth window (with timeline), i have configured my apps to work with it.
I'm french and use Facebook in French.
First issue :
- My callback function is never called ...
Second issue :
- The new OAuth window, seem to be not the good window.
It's called 'permission request' but inside it is the copy of login window. And no permission request is displayed.
So, my question is : how can i do the permission request in js ?
How displaying this window : https://developers.facebook.com/attachment/app_extended_perms.png/ ?
Thanks.
The reason you are not seeing it is because the application process has become a two step process.
Being that the person accepts to login into your application.
Being the person accept your extended permission which is where the callback url comes into play.
Documentation can be found here.
So the reason your callback isn't being called is because the two step process. I would suggest making the response attached to second page that is called.
I am not sure how the JS SDK works but it is how I managed to do it.
Goodluck.
Disable "Enhanced Auth Dialog" in your app's advance settings and see if it works. If you want to stick with Enhanced Auth Dialog then checkout Setup Auth Dialog Preview for Authenticating user section of this tutorial.

How to change the startup page of a WP7 Application

I am having an WP7 application in which I need to change the Startup page depending upon if the user is already registered or not. Suppose if user is registered then I need to redirect him to welcome page if not then to register page.
I have tried a couple of ways below is my code but it gives me an argument dispose exception.
To achieve above feature I wrote Navigation code in my MainPage.xaml constructor. Checked if the User Registration info is not in isolated storage then will redirect to other page.
private void IsAgreeed()
{
try
{
isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!isoStorage.FileExists("DataBase/MyPhoneNumber.txt"))
{
this.NavigationService.Navigate(new Uri("/EULA.xaml", UriKind.Relative));
}
else
{
return;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
Above code works properly when I use it first time but if I Close the Application and restart it again it throws exception argument dispose exception.
Thanks;
nil
Peter Torr covers page redirections quite well here. Two methods are offered with relative merits covered. Edit: Note you can redirect to a login page, rather then employing the popup suggestion for login handling.
Redirecting an initial navigation - Peter Torr's Blog
I'd also recommend familiarising with his accompanying post on places here.
Introducing the concept of “Places” - Peter Torr's Blog
This address back stack handling (certification consideration) and addresses scenarios such as login pages.
Peter Torr explained 2 techniques for your purpose.
Redirecting an initial navigation
To achieve this I put up MainPageorEula.xaml and got the functionality required.

Resources