Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 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
VladimirPlastovets committed Sep 13, 2018
2 parents 9964587 + b734b67 commit 4318dea
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 139 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ nano config.json
In the config file please setup the following variables:
- [`auth`](https://github.com/DivanteLtd/magento1-vsbridge/blob/5d4b9285c2dd2a20900e6075f50ebc2d7802499e/node-app/config.example.json#L9) section to setup Magento's user login and password and the previously set [JWT secret](https://github.com/DivanteLtd/magento1-vsbridge/blob/5d4b9285c2dd2a20900e6075f50ebc2d7802499e/magento1-module/app/code/local/Divante/VueStorefrontBridge/etc/config.xml#L6)
- ['endpoint'](https://github.com/DivanteLtd/magento1-vsbridge/blob/5d4b9285c2dd2a20900e6075f50ebc2d7802499e/node-app/config.example.json#L14) should match your Magento 1.9 URL,
- [`elasticsearch.indexName`](https://github.com/DivanteLtd/magento1-vsbridge/blob/5d4b9285c2dd2a20900e6075f50ebc2d7802499e/node-app/config.example.json#L4) should be set to Your ElasticSearch index which then will be connected to the Vue Storefront. It can be fresh / non-existient index as well (will be created then). For example You may have: `vue_storefront_mangento1`
- [`elasticsearch.indexName`](https://github.com/DivanteLtd/magento1-vsbridge/blob/5d4b9285c2dd2a20900e6075f50ebc2d7802499e/node-app/config.example.json#L4) should be set to Your ElasticSearch index which then will be connected to the Vue Storefront. It can be fresh / non-existient index as well (will be created then). For example You may have: `vue_storefront_magento1`

## Vue Storefront setup

Expand All @@ -76,7 +76,7 @@ Restart `vue-storefront` and `vue-storefront-api`.
# Available commands
The bridge works on temporary, versioned ES indexes. You decide when the index should be published (when all data objects are properly set). All commands exec from `node-app/src` directory.

Create new version of index (for example: vue_storefront_mangento1_1):
Create new version of index (for example: vue_storefront_magento1_1):
```
cd node-app/src
node index.js new
Expand Down
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');
}
}
?>
}
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';
Expand All @@ -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
Expand All @@ -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,
Expand Down
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.'
);
}


}
}

}
}
}
?>
}
Loading

0 comments on commit 4318dea

Please sign in to comment.