In diesem Beitrag stelle ich eine einfache Lösung vor, um der Magento-API eine Kundenauthentifizierung hinzuzufügen. Wie man Funktionalität zur Magento-API hinzufügt, ist im Magento-Wiki bereits gut dokumentiert: Creating a custom API or extending the Core API — darauf gehen wir hier nicht weiter ein.
Die Idee ist, einfach dieselbe login()-Funktion aufzurufen, die im Frontend verwendet würde, und in jedem API-Aufruf, der Kundenauthentifizierung erfordert, auf eine authentifizierte Kunden-Session zu prüfen.
Hinweis: Bitte beachten Sie, dass dieser Beitrag in Arbeit ist; Ihre Erfahrungen können abweichen — testen Sie also gründlich, bevor Sie diese Lösung in einer Produktivumgebung einsetzen.
Update: Geändert, sodass jetzt die Kunden-Session zur Speicherung der aktuellen Website und Filiale verwendet wird.
Bitte lassen Sie mich wissen, wenn Sie Probleme mit dieser Lösung finden — danke!
Vergessen Sie nicht, dass Sie auch den API-Zugriff im Magento-Adminbereich einrichten müssen.
Zuerst müssen Sie eine Funktion in Ihrer API-Klasse definieren, die das Kunden-Login behandelt:
<?php
/**
* Custom API model
*/
class MyCompany_MyModule_Model_Api extends Mage_Api_Model_Resource_Abstract
{
/** @var Mage_Customer_Model_Session */
protected $_customerSession = null;
/**
* Customer authentication.
*
* @param string $website Website code of website to authenticate customer against
* @param string $username Username of customer to authenticate
* @param string $password Password of customer to authenticate
* @return boolean True, if successfully authenticated customer for supplied website; false, otherwise.
*/
public function login( $website, $email, $password )
{
// determine store to login to
$store = $this->_getStore($website);
// get customer session object
$session = $this->_getCustomerSession();
// authenticate customer
$authenticated = $session->login($email, $password);
// return authentication result
return $authenticated;
}
/**
* Logout authenticated customer, if any.
* @return boolean True.
*/
public function logout()
{
// get customer session object
$session = $this->_getCustomerSession();
// logout customer
$session->logout();
return true;
}
Anschließend müssen Sie in den API-Funktionen, die das erfordern, auf einen authentifizierten Kunden prüfen:
/**
* Do something for an authenticated customer.
*/
public function doSomethingThatRequiresCustomerAuthentication()
{
// check whether customer is actually authenticated
$this->_checkCustomerAuthentication();
// retrieve customer object
$customer = $this->_getAuthenticatedCustomer()
// do something for authenticated customer
...
}
Schließlich müssen Sie die verwendeten Helper-Funktionen definieren:
/**
* Check whether a customer has been authenticated in this session.
*
* @return void
* @throws Mage_Core_Exception If customer is not authenticated.
*/
protected function _checkCustomerAuthentication()
{
// get customer session object
$session = $this->_getCustomerSession();
// check whether customer is logged in
if ( !$session->isLoggedIn() ) {
// if customer is not logged in throw an exception
Mage::throwException(Mage::helper('mymodule')->__('Not logged in'));
}
}
/**
* Get authenticated customer object.
*
* @return Mage_Customer_Model_Customer Authenticated customer object.
* @throws Mage_Core_Exception If customer is not authenticated or does not exist.
*/
protected function _getAuthenticatedCustomer()
{
// retrieve authenticated customer ID
$customerId = $this->_getAuthenticatedCustomerId();
if ( $customerId )
{
// load customer
/** @var Mage_Customer_Model_Customer $customer */
$customer = Mage::getModel('customer/customer')
->load($customerId);
if ( $customer->getId() ) {
// if customer exists, return customer object
return $customer;
}
}
// customer not authenticated or does not exist, so throw exception
Mage::throwException(Mage::helper('mymodule')->__('Unknown Customer'));
}
/**
* Get authenticated customer ID.
*
* @return integer Authenticated customer ID, if any; null, otherwise.
*/
protected function _getAuthenticatedCustomerId()
{
// get customer session object
$session = $this->_getCustomerSession();
// return authenticated customer ID, if any
return $session->getCustomerId();
}
/**
* Get store object from supplied website code or from register or session.
*
* @param string $code Code
*/
protected function _getStore( $code = null )
{
// get customer session
$session = $this->_getCustomerSession();
// if website code not supplied, check for selected store in register or selected website in session
if ( null === $code ) {
// try to get selected store from register
$store = Mage::registry('current_store');
if ( $store ) {
return $store;
}
// try to get selected website code from session
$code = $session->getCurrentWebsiteCode();
if ( !$code ) {
// if no store in register or website code in session, throw an exception
Mage::throwException(Mage::helper('mymodule')->__('No Store set'));
}
}
// load website from code
/** @var Mage_Core_Model_Website $website */
$website = Mage::getModel('core/website')
->load($code, 'code');
if ( !$website->getId() ) {
// if unknown website, throw an exception
Mage::throwException(Mage::helper('mymodule')->__('Invalid Store') . $code);
}
// get the default store of the website
$store = $website->getDefaultStore();
// register the current store
Mage::app()->setCurrentStore($store);
Mage::register('current_store', $store, true);
// set the current website code in the session
$session->setCurrentWebsiteCode($website->getCode());
// return store object
return $store;
}
/**
* @return Mage_Customer_Model_Session
*/
protected function _getCustomerSession()
{
if ( !$this->_customerSession ) {
$this->_customerSession = Mage::getSingleton('customer/session');
}
return $this->_customerSession;
}
...
}
Lassen Sie mich wissen, ob das bei Ihnen funktioniert!