【问题标题】:How to Protect user,for visit product page in magento如何保护用户,在magento中访问产品页面
【发布时间】:2014-04-25 11:03:01
【问题描述】:

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

无需访问任何产品页面。

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

【问题讨论】:

    标签: magento


    【解决方案1】:

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

    它是免费的,而且做得很好。

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法作为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'));
              }
          }
      }
      

      清空缓存试试看。

      【讨论】:

        【解决方案3】:

        请使用这个

         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>
        

        我想它会解决你的问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多