用于模仿的Magento用户子帐户

时间:2013-04-08 15:21:43

标签: magento magento-1.7 impersonation user-accounts

我要求Magento项目的帐户是分层的。这是一个帐户可以“拥有”多个其他帐户。如果一个帐户拥有另一个帐户,则可以模拟该帐户:创建订单,查看帐户信息以及查看以前的订单。

我不知道从哪里开始。如果您有任何想法,请指点我正确的方向?

2 个答案:

答案 0 :(得分:0)

一种解决方案是设置“多选”属性,并使用允许模拟的用户的用户ID填充该属性。然后,您可以创建一个运行magento的单独php文件,并根据用户选择的用户登录用户,或将其集成到cms中。

这是我的自定义“登录”代码,可以让我的Microsoft数据库用户登录到magento。

您调用此函数并将其传递给您要登录的“用户”。似乎工作得很好,但是你需要根据自己的需要进行修改。不要指望它开箱即用!

仅供参考:如果您没有传递magento所需的有关dispatchevents()的所有垃圾,那么用户将无法正常登录。我不得不对这整个东西进行逆向工程,所以不要指望在这里除了它以外的任何地方和magento核心的点点滴滴:)

$userId = 5;
$user = Mage::getModel('customer/customer')->load($userId)->setWebsiteId(1);
$this->LoginToMagento($user, null);

function LoginToMagento($user, $noAddress) {

        // Must include this file in order to use the object
        include ('/var/www/app/code/core/Mage/Customer/controllers/AccountController.php');

        // Configure Magento to think its using the frontend
        Mage::getSingleton("core/session", array("name" => "frontend"));
        Mage::getConfig()->init();
        Mage::getConfig()->loadEventObservers('frontend');
        Mage::app()->addEventArea('frontend');
        Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);

        // Grab the request and modify it with my emulated controller's information
        $request = Mage::app()->getRequest();
        $request->setRouteName('customer');
        $request->setControllerModule('Mage_Customer');
        $request->setRoutingInfo('');
        $request->setModuleName('customer');
        $request->setControllerName('account');
        $request->setModuleKey('module');
        $request->setControllerKey('account');
        $request->setActionName('loginPost');
        $request->setActionKey('action');

        // Grab the response
        $response = Mage::app()->getResponse();

        // Feed the request and response into a new accountcontroller object
        $accountControl = new Mage_Customer_AccountController($request, $response);

        // Dispatch events related to the controller actions for predispatch
        Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $accountControl));
        Mage::dispatchEvent('controller_action_predispatch_customer', array('controller_action' => $accountControl));
        Mage::dispatchEvent('controller_action_predispatch_customer_account_loginPost', array('controller_action' => $accountControl));

        // Grab an instance of the customer session model
        $session = Mage::getSingleton('customer/session');

        try{
            // Attempt to login the user
            $session->setCustomerAsLoggedIn($user);
            $session->renewSession();

        } catch (Mage_Core_Exception $e) {
            // Lets hope to never get here
            $message = $e->getMessage();
            error_log($message);
            Mage::getSingleton('core/session')->addError($message);
        }

// Perform the postdispatch events for 'after emulation of the controller'
        Mage::dispatchEvent('controller_action_postdispatch_customer_account_loginPost', array('controller_action'=>$accountControl));
        Mage::dispatchEvent('controller_action_postdispatch_customer', array('controller_action'=>$accountControl));
        Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$accountControl));

        $customer = Mage::getModel('customer/customer')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('entity_id', array('eq' => $user->getId()))
            ->getFirstItem();

        try
        {
            // Prepare a collection of required values that the customer *should* have been set from netforum
            $collection = Mage::getModel('eav/entity_attribute')->getCollection();
            $collection->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('customer')->getTypeId());

            // The var representing if validation has failed
            $failedReq = false;

            // Loop through each user defined required attribute and if we find one
            // on the customer that is not set, forward the user to their account config page
            foreach ($collection as $attribute)
            {
                if ($attribute['is_required'] && $attribute['is_user_defined'])
                {
                    $attrCode = $attribute['attribute_code'];
                    if (!isset($customer[$attrCode]))
                    {
                        $failedReq = true;
                    }
                }
            }

            // Try to determine where we logged in from (URL)
            Mage::getSingleton("core/session", array("name" => "frontend"));
            $session = Mage::getSingleton("customer/session");
            $outputMessage = $session->getData('before_auth_url');


            // Proceeed differently based on the existence of addresses
            if ($noAddress == true)
            {
                if ($failedReq)
                {
                    // Customer failed login. To be expected if they are signing in with SSO and never have before
                    $redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/edit/';
                    Mage::getSingleton('core/session')->addError('<b>Please fill in the required fields marked with * and click "Save"</b>');
                    header("Location: $redirect_to");
                }
                else
                {
                    // Customer checks out ok, but has no addresses. Send them to the address setup screen
                    Mage::getSingleton('core/session')->addError('<b>Please fill in your address and phone number, then click "Save"</b>');
                    $redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/address/edit/';
                    header("Location: $redirect_to");
                }

            }
            else
{
                // Customer has addresses being passed from SSO
                $defaultBillingId = $customer->getDefaultBillingAddress()->getId();

                $hasPhoneNumber = false;

                foreach ($customer->getAddresses() as $address)
                {
                    $addrs = Mage::getModel('customer/address')->load($address->getId());
                    $magePhone = $addrs->getTelephone();
                    if ($magePhone)
                    {
                        $hasPhoneNumber = true;
                    }
                }

                if ($failedReq)
                {
                    // Customer failed login. To be expected if they are signing in with SSO and never have before
                    $redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/edit/';
                    Mage::getSingleton('core/session')->addError('<b>Please fill in the required fields marked with * and click "Save"</b>');
                    header("Location: $redirect_to");

                }
                else
                {
                    // Customer is has default values filled out
                    if (!$hasPhoneNumber)
                    {
                        // Phone number is missing for an address so redirect there and force em to enter it.
                        Mage::getSingleton('core/session')->addError('<b>Please fill in the required fields marked with * and click "Save Address"</b>');
                        $redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/address/edit/id/' . $defaultBillingId;
                        header("Location: $redirect_to");

                    }
                    else
                    {
                        // Everything is ok, so just try to send them back to where they came from, or the account screen
                        if ($outputMessage)
                        {
                            $redirect_to = $outputMessage;
                        }
                        else
                        {
                            $redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/';
                        }
                        header("Location: $redirect_to");
                    }
                }
            }
        }
        catch (Exception $e)
        {
            if ($outputMessage)
            {
                $redirect_to = $outputMessage;
            }
            else
            {
                $redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/';
            }
            header("Location: $redirect_to");
        }
    }

答案 1 :(得分:0)

我知道我迟到了,但

http://amasty.com/sales-reps-and-dealers.html

此扩展程序可帮助您实现所需目标。它将允许创建分层帐户并将销售代表/子管理员分配给订单并提供访问级别。

希望这有帮助。