user3544657
user3544657

Reputation:

How to Protect user,for visit product page in magento

I am working in a Magento site, Requirement is when user comes to the site user should be redirect to login page,

Without visit any product page.

After register he will be able to view the products, I have already tried but not getting any solution yet. Anyone can help me on this?

Upvotes: 2

Views: 1051

Answers (3)

Prince Kumar
Prince Kumar

Reputation: 498

Please use this

 Step 1: Go to Admin => System => Configuration => Customer Configuration => Login Options => "Redirect Customer to Account Dashboard after Logging in" set it "No".

    Step 2: If you're using a module page then you've the controller Action method like:

            public function indexAction()
            {
                // use here to restrict the page //
                if(!Mage::helper('customer')->isLoggedIn()){
                    Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
                       $this->_redirect('customer/account/login');
                }
                //        End your addition //

                $this->loadLayout();   
                $this->renderLayout();
            }
            you can use one of this code to redirect:

            Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
            $this->_redirect('customer/account/login');

                                OR

            $this->_redirect('customer/account/login/referer/'.Mage::helper('core')->urlEncode('Restricted Page Url'));

    Step 3: (optional) If you're using a cms page then follow this step:

            In your admin CMS section include a phtml page and write the below code at the top of the page:

            if(!Mage::helper('customer')->isLoggedIn()){
                Mage::getSingleton('customer/session')->setBeforeAuthUrl('Restricted Page Url');
                $this->_redirect('customer/account/login');
            }

>> Now if you want to use the login url inside a page with referer url follow this:

    <?php $referer = Mage::helper('core')->urlEncode(Mage::helper('core/url')->getCurrentUrl()); ?>
    <a href="<?php echo Mage::getUrl('customer/account/login', array('referer' => $referer)) ?>">Login</a>

I think it will solve your problem

Upvotes: 0

Marius
Marius

Reputation: 15216

You can try the following approach as described here. Since posting single link answers is not recommended, here is what you need to do.

You need to create an observer for the event controller_action_predispatch for frontend. You can do that in a custom module. Let's call that module Easylife_Restrict.
You will need the following files:

app/etc/modules/Easylife_Restrict.xml - the declaration file.

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Restrict>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Customer />
            </depends>
        </Easylife_Restrict>
    </modules>
</config>

app/code/local/Easylife/Restrict/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Restrict>
            <version>1.0.0</version>
        </Easylife_Restrict>
    </modules>
    <global>
        <models>
            <easylife_restrict>
                <class>Easylife_Restrict_Model</class>
            </easylife_restrict>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch>
                <observers>
                    <easylife_restrict>
                        <class>easylife_restrict/observer</class>
                        <method>redirectNotLogged</method>
                    </easylife_restrict>
                </observers>
            </controller_action_predispatch>
        </events>
    </frontend>
</config>

app/code/local/Easylife/Restrict/Model/Observer.php - the module observer - this is where the magic happens:

<?php
class Easylife_Restrict_Model_Observer{
    public function redirectNotLogged(Varien_Event_Observer $observer)
    {
        //get the current request action
        $action = strtolower(Mage::app()->getRequest()->getActionName());
        //get the current request controller
        $controller = strtolower(Mage::app()->getRequest()->getControllerName());
        //a list of allowed actions for the not logged in user
        $openActions = array(
            'create',
            'createpost',
            'login',
            'loginpost',
            'logoutsuccess',
            'forgotpassword',
            'forgotpasswordpost',
            'resetpassword',
            'resetpasswordpost',
            'confirm',
            'confirmation'
        );
        //if the controller is the customer account controller and the action is allowed for everyone just do nothing.
        if ($controller == 'account' && in_array($action, $openActions)) {
            return $this; //if in allowed actions do nothing.
        }
        //if the user is not logged in redirect to the login page
        if(! Mage::helper('customer')->isLoggedIn()){
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('customer/account/login'));
        }
    }
}

Clear the cache and give it a try.

Upvotes: 1

Mufaddal
Mufaddal

Reputation: 5381

You can simply use free available extension on magento connect. I used this extension for my store http://www.magentocommerce.com/magento-connect/login-check.html

It is free and doing job nice.

Upvotes: 1

Related Questions