Skip to content

Commit 0205ffb

Browse files
committedOct 4, 2018
Add spanish locale, refactor key checking in api controller, refactor to use standardized error messages
1 parent d0c4a6b commit 0205ffb

File tree

6 files changed

+196
-48
lines changed

6 files changed

+196
-48
lines changed
 

‎code/controllers/ApiController.php

+86-35
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,67 @@
33
class Clerk_Clerk_ApiController extends Mage_Core_Controller_Front_Action
44
{
55
/**
6-
* Set content-type header
6+
* Set content-type header and validate keys
77
*
88
* @return Mage_Core_Controller_Front_Action
9+
* @throws Zend_Controller_Request_Exception
910
*/
1011
public function preDispatch()
1112
{
1213
$this->getResponse()->setHeader('Content-type', 'application/json');
1314

15+
$input = $this->getRequest()->getHeader('CLERK-PRIVATE-KEY');
16+
$secret = Mage::helper('clerk')->getSetting('clerk/general/privateapikey');
17+
18+
if (!$secret || $input !== trim($secret)) {
19+
$response = [
20+
'error' => [
21+
'code' => 403,
22+
'message' => 'Invalid public or private key supplied'
23+
]
24+
];
25+
26+
$this->getResponse()
27+
->setHeader('HTTP/1.1', '403', true)
28+
->setBody(json_encode($response))
29+
->sendResponse();
30+
exit;
31+
}
32+
1433
return parent::preDispatch();
1534
}
1635

36+
/**
37+
* Return Clerk module version
38+
*/
39+
public function versionAction()
40+
{
41+
$response = [
42+
'platform' => 'Magento',
43+
'version' => (string) Mage::getConfig()->getNode()->modules->Clerk_Clerk->version,
44+
];
45+
46+
$this->getResponse()->setBody(json_encode($response));
47+
}
48+
1749
/**
1850
* This endpoint will list stores
1951
*
2052
* @throws Zend_Controller_Request_Exception
2153
*/
2254
public function storeAction()
2355
{
24-
$this->authenticate();
56+
$this->setStore();
2557
$data = array();
58+
2659
foreach (Mage::helper('clerk')->getAllStores() as $store) {
2760
$data[] = array(
2861
'id' => $store->getId(),
2962
'name' => $store->getName(),
3063
'active' => (bool) Mage::getStoreConfig('clerk/general/active', $store),
3164
);
3265
}
66+
3367
$this->getResponse()->setBody(json_encode($data));
3468
}
3569

@@ -40,7 +74,7 @@ public function storeAction()
4074
*/
4175
public function productAction()
4276
{
43-
$this->authenticate();
77+
$this->setStore();
4478

4579
// Handler for product endpoint. E.g.
4680
// http://store.com/clerk/api/product/id/24
@@ -51,17 +85,23 @@ public function productAction()
5185
if (Mage::helper('clerk')->isProductIdValid($id)) {
5286
$data = Mage::getModel('clerk/product')->load($id)->getInfo();
5387
} else {
54-
$data = array('Error' => 'Product not found');
88+
$response = [
89+
'error' => [
90+
'code' => 404,
91+
'message' => 'Product not found',
92+
'product_id' => $id
93+
]
94+
];
5595
}
5696
} else {
5797
$page = $this->getIntParam('page');
5898
$limit = $this->getIntParam('limit');
5999
$page = Mage::getModel('clerk/productpage')->load((int)$page, $limit);
60-
$data = $page->array;
100+
$response = $page->array;
61101
$this->getResponse()->setHeader('Total-Page-Count', $page->totalPages);
62102
}
63103

64-
$this->getResponse()->setBody(json_encode($data));
104+
$this->getResponse()->setBody(json_encode($response));
65105
}
66106

67107
/**
@@ -72,7 +112,7 @@ public function productAction()
72112
*/
73113
public function categoryAction()
74114
{
75-
$this->authenticate();
115+
$this->setStore();
76116

77117
$page = $this->getIntParam('page');
78118
$limit = $this->getIntParam('limit');
@@ -120,7 +160,7 @@ public function categoryAction()
120160
*/
121161
public function orderAction()
122162
{
123-
$this->authenticate();
163+
$this->setStore();
124164

125165
$page = $this->getIntParam('page');
126166
$limit = $this->getIntParam('limit');
@@ -136,38 +176,38 @@ public function orderAction()
136176
}
137177

138178
/**
139-
* Validate request
179+
* Get int parameter, show error message if supplied param is not a number
140180
*
141-
* @throws Zend_Controller_Request_Exception
181+
* @param $key
182+
* @param null $errmsg
183+
* @return int
142184
*/
143-
private function authenticate()
144-
{
145-
$this->setStore();
146-
$this->getResponse()->setBody(json_encode(array('Error' => 'Not Authorized')));
147-
148-
$input = $this->getRequest()->getHeader('CLERK-PRIVATE-KEY');
149-
$secret = Mage::helper('clerk')->getSetting('clerk/general/privateapikey');
150-
151-
if (!$secret || $input != trim($secret)) {
152-
$this->getResponse()->setHeader('HTTP/1.0', '401', true);
153-
die($this->getResponse());
154-
}
155-
}
156-
157-
/* Helper function extracting params, this function also does the
158-
* errorhandling is param is missing */
159185
private function getIntParam($key, $errmsg = null)
160186
{
161187
$value = $this->getRequest()->getParam($key);
188+
162189
if (!is_numeric($value)) {
163-
$this->getResponse()->setHeader('HTTP/1.0', '404', true);
190+
$this->getResponse()->setHeader('HTTP/1.0', '400', true);
191+
164192
if (isset($errmsg)) {
165-
$data = array('Error' => $errmsg);
193+
$response = [
194+
'error' => [
195+
'code' => 400,
196+
'message' => $errmsg,
197+
'value' => $value
198+
]
199+
];
166200
} else {
167-
$data = array('Error' => "Query string '".$key."' is required and must be integer");
201+
$response = [
202+
'error' => [
203+
'code' => 400,
204+
'message' => "Query string '".$key."' is required and must be integer",
205+
'value' => $value
206+
]
207+
];
168208
}
169-
$this->getResponse()->setBody(json_encode($data));
170-
die($this->getResponse());
209+
$this->getResponse()->setBody(json_encode($response))->sendResponse();
210+
exit;
171211
}
172212

173213
return (int) $value;
@@ -187,13 +227,24 @@ private function setStore()
187227

188228
return;
189229
} catch (Exception $e) {
190-
$data = array('Error' => 'Store not found');
230+
$response = [
231+
'error' => [
232+
'code' => 400,
233+
'message' => 'Store not found',
234+
'store_id' => $storeid
235+
]
236+
];
191237
}
192238
} else {
193-
$data = array('Error' => "Query string param 'store' is required");
239+
$response = [
240+
'error' => [
241+
'code' => 400,
242+
'message' => 'Query string param "store" is required'
243+
]
244+
];
194245
}
195246

196-
$this->getResponse()->setBody(json_encode($data));
197-
die($this->getResponse());
247+
$this->getResponse()->setBody(json_encode($response))->sendResponse();
248+
exit;
198249
}
199250
}

‎code/etc/config.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<config>
33
<modules>
44
<Clerk_Clerk>
5-
<version>3.4.8</version>
5+
<version>3.4.9</version>
66
</Clerk_Clerk>
77
</modules>
88
<global>

‎locale/en_US/Clerk_Clerk.csv

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1+
"Clerk.io Dashboard","Clerk.io Dashboard"
2+
"Clerk.io - Audience Insights","Clerk.io - Audience Insights"
3+
"Clerk.io - Email Insights","Clerk.io - Email Insights"
4+
"Clerk.io - Recommendations Insights","Clerk.io - Recommendations Insights"
5+
"Clerk.io - Search Insights","Clerk.io - Search Insights"
6+
"Public and private key must be set in order to enable faceted search","Public and private key must be set in order to enable faceted search"
7+
"Public or private key invalid","Public or private key invalid"
8+
"Search results for '%s'","Search results for '%s'"
9+
Page,Page
10+
Popup,Popup
11+
"-- Please Select --","-- Please Select --"
12+
Content,Content
13+
"Clerk Content Options","Clerk Content Options"
14+
Product,Product
15+
"Select Product...","Select Product..."
16+
Category,Category
17+
"Select Category...","Select Category..."
18+
"Select a store to view dashboard","Select a store to view dashboard"
19+
"Clerk is not configured for this store. <a href=""%s"">Click here to configure</a>","Clerk is not configured for this store. <a href=""%s"">Click here to configure</a>"
20+
Filters,Filters
21+
Categories,Categories
22+
Products,Products
123
"%s was successfully added to your shopping cart.","%s was successfully added to your shopping cart."
224
"View Shopping Cart","View Shopping Cart"
325
"You have %s products in your shopping cart","You have %s products in your shopping cart"
4-
"Total","Total"
26+
Total,Total
527
"Continue Shopping","Continue Shopping"
6-
"Checkout","Checkout"
7-
"Search settings","Search settings"
8-
"Categories","Categories"
9-
"Products","Products"
28+
Checkout,Checkout
29+
Clerk,Clerk
30+
Dashboard,Dashboard
31+
"Search Insights","Search Insights"
32+
"Recommendations Insights","Recommendations Insights"
33+
"Email Insights","Email Insights"
34+
"Audience Insights","Audience Insights"
35+
"Configuration Section","Configuration Section"
36+
"Clerk Content","Clerk Content"
37+
"Insert Clerk Content","Insert Clerk Content"

‎locale/es_ES/Clerk_Clerk.csv

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"Clerk.io Dashboard","Clerk.io - Panel de Herramientas"
2+
Clerk.io - Audience Insights,Clerk.io - Información sobre el Público
3+
Clerk.io - Email Insights,Clerk.io - Información sobre Correo Electronicos
4+
Clerk.io - Recommendations Insights,Clerk.io - Información sobre Recomendaciones
5+
Clerk.io - Search Insights,Clerk.io - Información sobre Busqueda / Estadísticas de búsqueda
6+
"Public and private key must be set in order to enable faceted searchsearch""",La clave pública y privada deben configurarse para permitir la búsqueda facetada
7+
Public or private key invalid,Clave pública o privada inválida
8+
Search results for '%s',Resultados de busqueda de '%s'
9+
Page,Página
10+
Popup,Ventanas Emergentes
11+
-- Please Select --,Por favor seleccione
12+
Content,Contenido
13+
Clerk Content Options,Opciones de contenido de Clerk
14+
Product,Producto
15+
Select Product...,Seleccione el Producto
16+
Category,Categoría
17+
Select Category...,Seleccione la Categoría
18+
Select a store to view dashboard,Seleccione una tienda para ver el panel de herramientas
19+
"Clerk is not configured for this store. <a href=""%s"">Click here to configure</a>href=""""%s"""">Click here to configure</a>""","Clerk no está configurado para esta tienda <a href=""%s"">Haga clic aquí para configurar</a>href=""""%s"""">Haga clic aquí para configurar</a>"""
20+
Filters,Filtros
21+
Categories,Categorías
22+
Products,Productos
23+
%s was successfully added to your shopping cart.,%s se ha agragado a su canasta de compras.
24+
View Shopping Cart,Ver canasta de compras
25+
You have %s products in your shopping cart,Tiene %s productos en su canasta de compras
26+
Total,Total
27+
Continue Shopping,Seguir comprando
28+
Checkout,Caja de pago / Preceder a la caja de pago / Prosiga a la caja de pago
29+
Clerk,Vendedor
30+
Dashboard,Panel de herramientas
31+
Search Insights,Información sobre búsquedas / Estadísticas de búsqueda
32+
Recommendations Insights,Información sobre recomendaciones
33+
Email Insights,Información sobre correos electronicos /
34+
Audience Insights,Información sobre el público
35+
Configuration Section,Sección de configuración
36+
Clerk Content,Contenido de Clerk
37+
Insert Clerk Content,Insertar contenido de clerk

‎locale/it_IT/Clerk_Clerk.csv

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
"%s was successfully added to your shopping cart.","%s è stato aggiunto con successo al tuo carrello"
2-
"View Shopping Cart","Vedi carrello"
3-
"You have %s products in your shopping cart","Tu hai %s prodotti nel tuo corrello"
4-
"Total","Totale"
5-
"Continue Shopping","Continua l'acquisto"
6-
"Checkout","Vai alla cassa"
7-
"Search settings","impostazioni ricerca"
1+
Clerk.io Dashboard,Pannello di Controllo di Clerk.io
2+
Clerk.io - Audience Insights,Clerk.io - Analisi dell'Audience
3+
Clerk.io - Email Insights,Clerk.io - Analisi delle Email
4+
Clerk.io - Recommendations Insights,Clerk.io - Analisi delle Recommendation
5+
Clerk.io - Search Insights,Clerk.io - Analisi delle Ricerche
6+
Public and private key must be set in order to enable faceted searchsearch,La public e private key devono essere inserite nel sistema per attivare la ricerca con filtri
7+
Public or private key invalid,La public o private key non e' valida
8+
Search results for '%s',"Risultati di ricerca per ""%s"""
9+
Page,Pagina
10+
Popup,Popup
11+
-- Please Select --,-- Per favore seleziona --
12+
Content,Contenuto
13+
Clerk Content Options,Opzioni del Contenuto di Clerk
14+
Product,Prodotto
15+
Select Product...,Seleziona un Prodotto...
16+
Category,Categoria
17+
Select Category...,Sleeziona Categoria...
18+
Select a store to view dashboard,Seleziona una negozio per accedere al suo pannellodi controllo
19+
"Clerk is not configured for this store. <a href=""%s"">Click here to configure</a>","Clerk non e' selezionato in questo negozio. <a href=""%s"">Clicca qui per configurare</a>"
20+
Filters,Filtri
21+
Categories,Categorie
22+
Products,Prodotti
23+
%s was successfully added to your shopping cart.,%s e' stato aggiunto con successo al tuo carrello.
24+
View Shopping Cart,Il tuo Carrello
25+
You have %s products in your shopping cart,Hai %s prodotti nel tuo carrello
26+
Total,Totale
27+
Continue Shopping,Continua gli Acquisti
28+
Checkout,Cassa
29+
Clerk,Clerk
30+
Dashboard,Pannello di Controllo
31+
Search Insights,Analisi delle Ricerche
32+
Recommendations Insights,Analisi delle Raccomandazioni
33+
Email Insights,Analisi delle Email
34+
Audience Insights,Analisi dell'Audience
35+
Configuration Section,Sezione della Configurazione
36+
Clerk Content,Contenuto di Clerk
37+
Insert Clerk Content,Inserisci il Contenuto di Clerk

‎modman

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ design/frontend/layout/clerk.xml app/design/frontend/base/default/layout/cler
66
design/adminhtml/template app/design/adminhtml/default/default/template/clerk
77
design/adminhtml/layout/clerk.xml app/design/adminhtml/default/default/layout/clerk.xml
88
tests tests
9+
locale/en_US/Clerk_Clerk.csv app/locale/en_US/Clerk_Clerk.csv
910
locale/da_DK/Clerk_Clerk.csv app/locale/da_DK/Clerk_Clerk.csv
11+
locale/es_ES/Clerk_Clerk.csv app/locale/es_ES/Clerk_Clerk.csv
1012
locale/it_IT/Clerk_Clerk.csv app/locale/it_IT/Clerk_Clerk.csv
1113
locale/nl_NL/Clerk_Clerk.csv app/locale/nl_NL/Clerk_Clerk.csv

0 commit comments

Comments
 (0)
Please sign in to comment.