ReadyCloud_App - Version 1.0.0

Version Notes

First public release

Download this release

Release Info

Developer TrueShip LLC
Extension ReadyCloud_App
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/ReadyCloud/Authorize/controllers/ReadyCloudController.php ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class ReadyCloud_Authorize_ReadyCloudController extends Mage_Core_Controller_Front_Action
4
+ {
5
+ const TITLE = 'ReadyCloud';
6
+ const EMAIL = 'email@readycloud.com';
7
+ const CALLBACK_URL = 'http://263c9dbe.ngrok.com/call_back';
8
+
9
+ /**
10
+ * Create the SOAP user
11
+ * @param $role_id
12
+ * @return array - SOAP login and SOAP password
13
+ */
14
+ private function createSoapUser($role_id)
15
+ {
16
+ $apiLogin = Mage::helper('core')->getRandomString(32);
17
+ $apiPass = Mage::helper('core')->getRandomString(32);
18
+
19
+ $m = Mage::getModel('api/user')
20
+ ->getCollection()
21
+ ->addFieldToFilter('email', self::EMAIL)
22
+ ->getFirstItem();
23
+
24
+ if (!$m->isEmpty()) {
25
+ $m->setUsername($apiLogin);
26
+ $m->setApiKey($apiPass);
27
+ $m->save();
28
+ return array(
29
+ 'api_login' => $apiLogin,
30
+ 'api_pass' => $apiPass
31
+ );
32
+ }
33
+
34
+ $userapi = Mage::getModel('api/user')
35
+ ->setData(array(
36
+ 'username' => $apiLogin,
37
+ 'firstname' => self::TITLE,
38
+ 'lastname' => self::TITLE,
39
+ 'email' => self::EMAIL,
40
+ 'api_key' => $apiPass,
41
+ 'api_key_confirmation' => $apiPass,
42
+ 'is_active' => 1,
43
+ 'user_roles' => '',
44
+ 'assigned_user_role' => '',
45
+ 'role_name' => self::TITLE,
46
+ 'roles' => array($role_id)
47
+ ));
48
+ $userapi->save();
49
+ $userapi->setRoleIds(array($role_id))
50
+ ->setRoleUserId($userapi->getUserId())
51
+ ->saveRelations();
52
+ return array(
53
+ 'api_login' => $apiLogin,
54
+ 'api_pass' => $apiPass
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Create the SOAP role and set important values for access
60
+ * @return - SOAP role id
61
+ */
62
+ private function createSoapRole()
63
+ {
64
+ $role = Mage::getModel('api/roles')
65
+ ->getCollection()
66
+ ->addFieldToFilter('role_name', self::TITLE)
67
+ ->getFirstItem();
68
+ if ($role->isEmpty()) {
69
+ $role = Mage::getModel('api/roles')
70
+ ->setName(self::TITLE)
71
+ ->setPid(false)
72
+ ->setRoleType('G')
73
+ ->save();
74
+ }
75
+ Mage::getModel("api/rules")
76
+ ->setRoleId($role->getId())
77
+ ->setResources(array('all'))
78
+ ->saveRel();
79
+
80
+ return $role->getId();
81
+ }
82
+
83
+ /**
84
+ * Create the new consumer if not exists and return the ID
85
+ * @return - return REST consumer ID
86
+ */
87
+ private function getRestConsumerId()
88
+ {
89
+ $consumer = Mage::getModel('oauth/consumer')
90
+ ->getCollection()
91
+ ->addFieldToFilter('name', self::TITLE)
92
+ ->getFirstItem();
93
+ if (!$consumer->isEmpty())
94
+ return $consumer->getId();
95
+
96
+ $model = Mage::getModel('oauth/consumer');
97
+ $helper = Mage::helper('oauth');
98
+ $model->setName(self::TITLE);
99
+ $model->setKey($helper->generateConsumerKey());
100
+ $model->setSecret($helper->generateConsumerSecret());
101
+ $model->save();
102
+ return $model->getId();
103
+ }
104
+
105
+
106
+ /**
107
+ * Create the auth token if not exists and return the ID
108
+ * @param $consumer_id - consumer ID
109
+ * @return int - consumer ID
110
+ */
111
+ private function getRestTokenIdByConsumerId($consumer_id)
112
+ {
113
+ $token = Mage::getModel('oauth/token')
114
+ ->getCollection()
115
+ ->addFieldToFilter('consumer_id', $consumer_id)
116
+ ->getFirstItem();
117
+ if (!$token->isEmpty())
118
+ return $token->getId();
119
+
120
+ // create oauth token
121
+ $m = Mage::getModel('oauth/token');
122
+ $m->createRequestToken($consumer_id, 'oob');
123
+ $m->convertToAccess();
124
+
125
+ $admin_id = Mage::getModel('admin/user')->getCollection()->getFirstItem()->getId();
126
+ $m->authorize($admin_id, 'admin');
127
+
128
+ $token_id = $m->getId();
129
+ return $token_id;
130
+ }
131
+
132
+
133
+ /**
134
+ * Create the REST role if not exists and return the ID
135
+ * @return int - REST role ID
136
+ */
137
+ private function createRestRole()
138
+ {
139
+ $m = Mage::getModel('api2/acl_global_role')
140
+ ->getCollection()
141
+ ->addFieldToFilter('role_name', self::TITLE)
142
+ ->getFirstItem();
143
+
144
+ if (!$m->isEmpty())
145
+ return $m->getId();
146
+
147
+ Mage::getModel('api2/acl_global_role')
148
+ ->setRoleName(self::TITLE)
149
+ ->save();
150
+ }
151
+
152
+ /**
153
+ * Create the REST rule if not exists and add the role
154
+ * @param $role_id - REST role ID
155
+ */
156
+ private function createRestRule($role_id)
157
+ {
158
+ $rule = Mage::getModel('api2/acl_global_rule');
159
+
160
+ $m = $rule->getCollection()
161
+ ->addFieldToFilter('role_id', $role_id)
162
+ ->getFirstItem();
163
+ if ($m->isEmpty()) {
164
+ $rule->setRoleId($role_id)
165
+ ->setResourceId('all')
166
+ ->setPrivilege(null)
167
+ ->save();
168
+ }
169
+ $this->addUserToRole($role_id);
170
+ }
171
+
172
+
173
+ /**
174
+ * Add user to the REST role
175
+ * @param $role_id - REST role ID
176
+ */
177
+ private function addUserToRole($role_id)
178
+ {
179
+ $admin_id = Mage::getModel('admin/user')->getCollection()->getFirstItem()->getId();
180
+ $resourceModel = Mage::getResourceModel('api2/acl_global_role');
181
+ $resourceModel->saveAdminToRoleRelation($admin_id, $role_id);
182
+ }
183
+
184
+ /**
185
+ * Create REST API keys
186
+ * @return array - keys and access tokens
187
+ */
188
+ private function createRestApiKeys()
189
+ {
190
+ $role_id = $this->createRestRole();
191
+ $this->createRestRule($role_id);
192
+
193
+ // create new or get existing consumer and token
194
+ $consumer_id = $this->getRestConsumerId();
195
+ $token_id = $this->getRestTokenIdByConsumerId($consumer_id);
196
+
197
+ $consumer = Mage::getModel('oauth/consumer')->load($consumer_id)->getData();
198
+ $token = Mage::getModel('oauth/token')->load($token_id)->getData();
199
+
200
+ return array(
201
+ 'key' => $consumer['key'],
202
+ 'secret' => $consumer['secret'],
203
+ 'token' => $token['token'],
204
+ 'token_secret' => $token['secret']
205
+ );
206
+ }
207
+
208
+ /**
209
+ * Create the SOAP keys and return the ID
210
+ * @return int - role ID
211
+ */
212
+ private function createSoapKeys()
213
+ {
214
+ $role_id = $this->createSoapRole();
215
+ return $this->createSoapUser($role_id);
216
+ }
217
+
218
+ /**
219
+ * Return HTTP request with an appropriate status code based on $success argument
220
+ * @param $success - boolean value
221
+ */
222
+ private function printSuccessResponse($success)
223
+ {
224
+ $this->getResponse()
225
+ ->clearHeaders()
226
+ ->setHttpResponseCode(($success)? 200 : 400)
227
+ ->setHeader('Content-Type', 'application/json; charset=utf-8')
228
+ ->setBody(json_encode(array('success' => $success)));
229
+ }
230
+
231
+ /**
232
+ * Basic Controller to create the new keys for the user by received uuid
233
+ */
234
+ public function postAction()
235
+ {
236
+ if ($this->getRequest()->isPost()) {
237
+ $uuid = $this->getRequest()->getPost('uuid');
238
+
239
+ // access to additional functionality
240
+ Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
241
+
242
+ $data = array(
243
+ 'rest_keys' => $this->createRestApiKeys(),
244
+ 'soap_keys' => $this->createSoapKeys()
245
+ );
246
+
247
+ $this->callBack($uuid, $data);
248
+ } else {
249
+ $this->printSuccessResponse(false);
250
+ }
251
+ }
252
+
253
+
254
+ /**
255
+ * Send keys to ReadyCloud
256
+ * @param $uuid - ReadyCloud user unique ID
257
+ * @param $data - REST and SOAP keys
258
+ */
259
+ private function callBack($uuid, $data)
260
+ {
261
+ $data['uuid'] = $uuid;
262
+ $data = json_encode($data);
263
+
264
+ ob_start();
265
+ $ch = curl_init(self::CALLBACK_URL);
266
+ curl_setopt($ch, CURLOPT_POST, 1);
267
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
268
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
269
+ $sent = curl_exec($ch);
270
+ ob_end_clean();
271
+
272
+ if ($sent) {
273
+ Mage::log('success: sending keys to ' . self::CALLBACK_URL, null, 'readycloud.log');
274
+ } else {
275
+ Mage::log('***FAIL: when sending data to ' . self::CALLBACK_URL . "\t" .
276
+ curl_error ( $ch ) . "\t" .
277
+ null, 'readycloud.log');
278
+ }
279
+ $this->printSuccessResponse($sent);
280
+ }
281
+
282
+
283
+ /**
284
+ * Controller to check if this extension has installed
285
+ */
286
+ public function checkAction()
287
+ {
288
+ if ($this->getRequest()->isPost()) {
289
+ $request = $this->getRequest()->getPost('verify');
290
+ if ($request == 'readycloud') {
291
+ $this->printSuccessResponse(true);
292
+ } else {
293
+ $this->printSuccessResponse(false);
294
+ }
295
+ }
296
+ }
297
+ }
app/code/community/ReadyCloud/Authorize/etc/config.xml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <ReadyCloud_Authorize>
5
+ <version>0.0.1</version>
6
+ </ReadyCloud_Authorize>
7
+ </modules>
8
+
9
+ <frontend>
10
+ <routers>
11
+ <authorize>
12
+ <use>standard</use>
13
+ <args>
14
+ <module>ReadyCloud_Authorize</module>
15
+ <frontName>authorize</frontName>
16
+ </args>
17
+ </authorize>
18
+ </routers>
19
+ </frontend>
20
+ </config>
app/etc/modules/ReadyCloud_Authorize.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <ReadyCloud_Authorize>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </ReadyCloud_Authorize>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>ReadyCloud_App</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>MIT</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>ReadyCloud Integration extension</summary>
10
+ <description>ReadyCloud Integration extension</description>
11
+ <notes>First public release</notes>
12
+ <authors><author><name>TrueShip LLC</name><user>trueship</user><email>magento@trueship.com</email></author></authors>
13
+ <date>2015-08-24</date>
14
+ <time>09:53:01</time>
15
+ <contents><target name="mage"><dir name="."><dir name="app"><dir name="code"><dir name="community"><dir name="ReadyCloud"><dir name="Authorize"><dir name="controllers"><file name="ReadyCloudController.php" hash="c26d7425301a41dad8fb34a5890486e0"/></dir><dir name="etc"><file name="config.xml" hash="2a132dbf168950a227d4f0b79344fb0f"/></dir></dir></dir></dir></dir><dir name="etc"><dir name="modules"><file name="ReadyCloud_Authorize.xml" hash="a7f3c8d4026b984fe32acfc54d3cd1f4"/></dir></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>