如何保护用户,访问magento的产品页面

时间:2014-04-25 11:03:02

标签: magento

我在Magento网站工作,要求是当用户访问网站时,用户应该重定向到登录页面,

不访问任何产品页面。

注册后,他将能够查看产品, 我已经尝试过,但还没有得到任何解决方案。 有人可以帮我这个吗?

3 个答案:

答案 0 :(得分:1)

您可以尝试以下方法described here。由于不推荐发布单链接答案,因此您需要执行此操作。

您需要为前端事件controller_action_predispatch创建一个观察者。您可以在自定义模块中执行此操作。我们称之为模块Easylife_Restrict 您将需要以下文件:

app/etc/modules/Easylife_Restrict.xml - 声明文件。

<?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 - 配置文件

<?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 - 模块观察者 - 这就是魔术发生的地方:

<?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'));
        }
    }
}

清除缓存并尝试一下。

答案 1 :(得分:0)

您可以在magento connect上使用免费的可用扩展程序。我将此扩展程序用于我的商店http://www.magentocommerce.com/magento-connect/login-check.html

这是免费的,做得很好。

答案 2 :(得分:0)

请使用此

 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>

我认为它会解决你的问题