forked from DivanteLtd/magento1-vsbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/DivanteLtd/magento1-vsbridge
into feature/product-mappings # Conflicts: # magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/AttributesController.php # magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/ProductsController.php
- Loading branch information
Showing
10 changed files
with
307 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 95 additions & 34 deletions
129
...nto1-module/app/code/local/Divante/VueStorefrontBridge/controllers/AbstractController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,129 @@ | ||
<?php | ||
define('MAX_PAGESIZE', 5000); | ||
require_once(__DIR__.'/../helpers/JWT.php'); | ||
|
||
function _filterDTO($dtoToFilter, array $blackList = null) { | ||
foreach($dtoToFilter as $key => $val) { | ||
if ($blackList && in_array($key, $blackList)) { | ||
unset ($dtoToFilter[$key]); | ||
} else { | ||
if (strstr($key, 'is_') || strstr($key, 'has_')) { | ||
$dtoToFilter[$key] = boolval($val); | ||
} | ||
} | ||
} | ||
require_once(__DIR__ . '/../helpers/JWT.php'); | ||
|
||
return $dtoToFilter; | ||
} | ||
/** | ||
* Divante VueStorefrontBridge AbstractController Class | ||
* | ||
* @category Divante | ||
* @package VueStorefrontBridge | ||
* @author Piotr Karwatka <[email protected]> | ||
* @author Björn Kraus PhoenixPM - BK | ||
* @author Dariusz Oliwa <[email protected]> | ||
* @copyright Copyright (C) 2018 | ||
* @license MIT License | ||
*/ | ||
class Divante_VueStorefrontBridge_AbstractController extends Mage_Core_Controller_Front_Action | ||
{ | ||
|
||
/** | ||
* JWT secret passphrase | ||
*/ | ||
const XML_CONFIG_JWT_SECRET = 'vsbridge/general/jwt_secret'; | ||
/** | ||
* Maximum page size | ||
*/ | ||
const XML_CONFIG_MAX_PAGE_SIZE = 'vsbridge/general/max_page_size'; | ||
|
||
/** | ||
* Sets response header content type to json | ||
*/ | ||
public function init() | ||
{ | ||
$this->getResponse()->setHeader('Content-Type', 'application/json'); | ||
} | ||
$this->getResponse()->setHeader('Content-Type', 'application/json'); | ||
} | ||
|
||
protected function _authorize($request) { | ||
$apikey = $request->getParam('apikey'); | ||
/** | ||
* Checks authorization token | ||
* | ||
* @param Mage_Core_Controller_Request_Http $request | ||
* | ||
* @return bool | ||
*/ | ||
protected function _authorize(Mage_Core_Controller_Request_Http $request) | ||
{ | ||
$apikey = $request->getParam('apikey'); | ||
$secretKey = trim(Mage::getStoreConfig(self::XML_CONFIG_JWT_SECRET)); | ||
|
||
try { | ||
$tokenData = JWT::decode($apikey, $secretKey, 'HS256'); | ||
if($tokenData->id > 0){ | ||
if ($tokenData->id > 0) { | ||
return true; | ||
} else { | ||
} else { | ||
$this->_result(401, 'Unauthorized request'); | ||
|
||
return false; | ||
} | ||
} catch (Exception $err) { | ||
$this->_result(500, $err->getMessage()); | ||
|
||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected function _processParams($request) { | ||
$paramsDTO = array(); | ||
$paramsDTO['page'] = max(abs(intval($request->getParam('page'))), 1); | ||
$paramsDTO['pageSize'] = min(abs(intval($request->getParam('pageSize'))), MAX_PAGESIZE); | ||
if($typeId = $request->getParam('type_id')) { | ||
/** | ||
* Processes parameters | ||
* | ||
* @param Mage_Core_Controller_Request_Http $request | ||
* | ||
* @return array | ||
*/ | ||
protected function _processParams(Mage_Core_Controller_Request_Http $request) | ||
{ | ||
$paramsDTO = []; | ||
$paramsDTO['page'] = max(abs(intval($request->getParam('page'))), 1); | ||
$paramsDTO['pageSize'] = min( | ||
abs(intval($request->getParam('pageSize'))), | ||
intval(trim(Mage::getStoreConfig(self::XML_CONFIG_MAX_PAGE_SIZE))) | ||
); | ||
if ($typeId = $request->getParam('type_id')) { | ||
$paramsDTO['type_id'] = $typeId; | ||
} | ||
|
||
return $paramsDTO; | ||
} | ||
|
||
protected function _filterDTO($dtoToFilter, array $blackList = null) { | ||
return _filterDTO($dtoToFilter, $blackList); | ||
/** | ||
* Filters parameters map removing blacklisted | ||
* | ||
* @param array $dtoToFilter | ||
* @param array|null $blackList | ||
* | ||
* @return mixed | ||
*/ | ||
protected function _filterDTO(array $dtoToFilter, array $blackList = null) | ||
{ | ||
foreach ($dtoToFilter as $key => $val) { | ||
if ($blackList && in_array($key, $blackList)) { | ||
unset ($dtoToFilter[$key]); | ||
} else { | ||
if (strstr($key, 'is_') || strstr($key, 'has_')) { | ||
$dtoToFilter[$key] = boolval($val); | ||
} | ||
} | ||
} | ||
|
||
return $dtoToFilter; | ||
} | ||
|
||
protected function _result($code, $result){ | ||
$this->getResponse()->setBody(json_encode(array( | ||
'code' => $code, | ||
'result' => $result | ||
), JSON_NUMERIC_CHECK))->setHttpResponseCode($code)->setHeader('Content-Type', 'application/json'); | ||
/** | ||
* Sends back code and result of performed operation | ||
* | ||
* @param $code | ||
* @param $result | ||
*/ | ||
protected function _result($code, $result) | ||
{ | ||
$this->getResponse()->setBody( | ||
json_encode( | ||
[ | ||
'code' => $code, | ||
'result' => $result, | ||
], | ||
JSON_NUMERIC_CHECK | ||
) | ||
)->setHttpResponseCode($code)->setHeader('Content-Type', 'application/json'); | ||
} | ||
} | ||
?> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
<?php | ||
|
||
require_once('AbstractController.php'); | ||
|
||
/** | ||
* Divante VueStorefrontBridge AttributesController Class | ||
* | ||
* @category Divante | ||
* @package VueStorefrontBridge | ||
* @author Piotr Karwatka <[email protected]> | ||
* @author Dariusz Oliwa <[email protected]> | ||
* @copyright Copyright (C) 2018 | ||
* @license MIT License | ||
*/ | ||
class Divante_VueStorefrontBridge_AttributesController extends Divante_VueStorefrontBridge_AbstractController | ||
{ | ||
const ES_DATA_TYPE_STRING = 'text'; | ||
|
@@ -26,27 +37,27 @@ class Divante_VueStorefrontBridge_AttributesController extends Divante_VueStoref | |
'static' => self::ES_DATA_TYPE_STRING, | ||
); | ||
|
||
|
||
/** | ||
* index action | ||
*/ | ||
public function indexAction() | ||
{ | ||
if ($this->_authorize($this->getRequest())) { | ||
$attrList = array(); | ||
$params = $this->_processParams($this->getRequest()); | ||
$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection'); | ||
|
||
$attrList = []; | ||
foreach ($productAttrs as $productAttr) { | ||
$options = array(); | ||
$productAttrDTO = $productAttr->getData(); | ||
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */ | ||
$attribute = Mage::getSingleton('eav/config') | ||
->getAttribute( | ||
Mage_Catalog_Model_Product::ENTITY, | ||
$productAttr->getAttributeCode() | ||
); | ||
|
||
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $productAttr->getAttributeCode()); | ||
$options = []; | ||
if ($attribute->usesSource()) { | ||
$options = $attribute->getSource()->getAllOptions(false); | ||
} | ||
|
||
$productAttrDTO = $productAttr->getData(); | ||
|
||
if (in_array($productAttrDTO['source_model'], array('core/design_source_design'))) { | ||
continue; | ||
} // exception - this attribute has string typed values; this is not acceptable by VS | ||
|
@@ -71,7 +82,7 @@ public function productMappingAction() | |
$attributeMapping = array(); | ||
$productAttributes = Mage::getResourceModel('catalog/product_attribute_collection'); | ||
foreach ($productAttributes as $productAttribute) { | ||
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */ | ||
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttribute */ | ||
$attribute = Mage::getSingleton('eav/config') | ||
->getAttribute( | ||
Mage_Catalog_Model_Product::ENTITY, | ||
|
44 changes: 28 additions & 16 deletions
44
magento1-module/app/code/local/Divante/VueStorefrontBridge/controllers/AuthController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,49 @@ | ||
<?php | ||
require_once('AbstractController.php'); | ||
require_once(__DIR__.'/../helpers/JWT.php'); | ||
require_once(__DIR__ . '/../helpers/JWT.php'); | ||
|
||
/** | ||
* Divante VueStorefrontBridge AuthController Class | ||
* | ||
* @category Divante | ||
* @package VueStorefrontBridge | ||
* @author Piotr Karwatka <[email protected]> | ||
* @author Björn Kraus PhoenixPM - BK | ||
* @author Dariusz Oliwa <[email protected]> | ||
* @copyright Copyright (C) 2018 | ||
* @license MIT License | ||
*/ | ||
class Divante_VueStorefrontBridge_AuthController extends Divante_VueStorefrontBridge_AbstractController | ||
{ | ||
|
||
/** | ||
* admin action | ||
*/ | ||
public function adminAction() | ||
{ | ||
if($this->getRequest()->getMethod() !== 'POST'){ | ||
if ($this->getRequest()->getMethod() !== 'POST') { | ||
return $this->_result(500, 'Only POST method allowed'); | ||
} else { | ||
|
||
$request = @json_decode($this->getRequest()->getRawBody()); | ||
|
||
if(!$request) { | ||
if (!$request) { | ||
return $this->_result(500, 'No JSON object found in the request body'); | ||
} else { | ||
if(!$request->username || !$request->password) { | ||
if (!$request->username || !$request->password) { | ||
return $this->_result(500, 'No username or password given!'); | ||
} else { | ||
$session = Mage::getSingleton('admin/session'); | ||
$session = Mage::getSingleton('admin/session'); | ||
$secretKey = trim(Mage::getStoreConfig(self::XML_CONFIG_JWT_SECRET)); | ||
|
||
$user = $session->login($request->username, $request->password); | ||
$user = $session->login($request->username, $request->password); | ||
if ($user->getId()) { | ||
return $this->_result(200, JWT::encode(array('id' => $user->getId()),$secretKey)); | ||
return $this->_result(200, JWT::encode(['id' => $user->getId()], $secretKey)); | ||
} else { | ||
return $this->_result(500, 'You did not sign in correctly or your account is temporarily disabled.'); | ||
return $this->_result( | ||
500, | ||
'You did not sign in correctly or your account is temporarily disabled.' | ||
); | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
?> | ||
} |
Oops, something went wrong.