|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package API |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\api; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use gplcart\core\Config; |
|
14
|
|
|
use gplcart\core\Container; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Main class for API module |
|
18
|
|
|
*/ |
|
19
|
|
|
class Main |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Database class instance |
|
24
|
|
|
* @var \gplcart\core\Database $db |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $db; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Config $config |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Config $config) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->db = $config->getDb(); |
|
34
|
|
|
$this->db->addScheme($this->getDbScheme()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Implements hook "module.install.before" |
|
39
|
|
|
* @param null|string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function hookModuleInstallBefore(&$result) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
$this->db->importScheme('module_api_user', $this->getDbScheme()); |
|
45
|
|
|
} catch (Exception $ex) { |
|
46
|
|
|
$result = $ex->getMessage(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Implements hook "module.uninstall.after" |
|
52
|
|
|
*/ |
|
53
|
|
|
public function hookModuleUninstallAfter() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->db->deleteTable('module_api_user'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Implements hook "route.list" |
|
60
|
|
|
* @param array $routes |
|
61
|
|
|
*/ |
|
62
|
|
|
public function hookRouteList(array &$routes) |
|
63
|
|
|
{ |
|
64
|
|
|
$routes['admin/module/settings/api'] = array( |
|
65
|
|
|
'access' => 'module_edit', |
|
66
|
|
|
'handlers' => array( |
|
67
|
|
|
'controller' => array('gplcart\\modules\\api\\controllers\\Settings', 'editSettings') |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
$routes['admin/user/api'] = array( |
|
72
|
|
|
'access' => 'module_api_user', |
|
73
|
|
|
'menu' => array( |
|
74
|
|
|
'admin' => 'API' // @text |
|
75
|
|
|
), |
|
76
|
|
|
'handlers' => array( |
|
77
|
|
|
'controller' => array('gplcart\\modules\\api\\controllers\\User', 'listUser') |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$routes['admin/user/api/add'] = array( |
|
82
|
|
|
'access' => 'module_api_user_add', |
|
83
|
|
|
'handlers' => array( |
|
84
|
|
|
'controller' => array('gplcart\\modules\\api\\controllers\\User', 'editUser') |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$routes['admin/user/api/edit/(\d+)'] = array( |
|
89
|
|
|
'access' => 'module_api_user_edit', |
|
90
|
|
|
'handlers' => array( |
|
91
|
|
|
'controller' => array('gplcart\\modules\\api\\controllers\\User', 'editUser') |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$routes['api'] = array( |
|
96
|
|
|
'internal' => true, |
|
97
|
|
|
'handlers' => array( |
|
98
|
|
|
'controller' => array('gplcart\\modules\\api\\controllers\\Api', 'callbackApi') |
|
99
|
|
|
) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$routes['api/(.*)'] = array( |
|
103
|
|
|
'internal' => true, |
|
104
|
|
|
'handlers' => array( |
|
105
|
|
|
'controller' => array('gplcart\\modules\\api\\controllers\\Api', 'callbackApi') |
|
106
|
|
|
) |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Implements hook "user.role.permissions" |
|
113
|
|
|
* @param array $permissions |
|
114
|
|
|
*/ |
|
115
|
|
|
public function hookUserRolePermissions(array &$permissions) |
|
116
|
|
|
{ |
|
117
|
|
|
$permissions['module_api_user'] = 'API: access users'; // @text |
|
118
|
|
|
$permissions['module_api_user_add'] = 'API: add user'; // @text |
|
119
|
|
|
$permissions['module_api_user_edit'] = 'API: edit user'; // @text |
|
120
|
|
|
$permissions['module_api_user_delete'] = 'API: delete user'; // @text |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns an array of database scheme |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getDbScheme() |
|
128
|
|
|
{ |
|
129
|
|
|
return array( |
|
130
|
|
|
'module_api_user' => array( |
|
131
|
|
|
'fields' => array( |
|
132
|
|
|
'user_id' => array('type' => 'int', 'length' => 10, 'not_null' => true), |
|
133
|
|
|
'created' => array('type' => 'int', 'length' => 10, 'not_null' => true), |
|
134
|
|
|
'secret' => array('type' => 'varchar', 'length' => 255, 'not_null' => true), |
|
135
|
|
|
'data' => array('type' => 'blob', 'not_null' => true, 'serialize' => true), |
|
136
|
|
|
'status' => array('type' => 'int', 'length' => 1, 'not_null' => true, 'default' => 0), |
|
137
|
|
|
'modified' => array('type' => 'int', 'length' => 10, 'not_null' => true, 'default' => 0), |
|
138
|
|
|
'api_user_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true) |
|
139
|
|
|
) |
|
140
|
|
|
) |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns Api model instance |
|
146
|
|
|
* @return \gplcart\modules\api\models\Api |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getApiModel() |
|
149
|
|
|
{ |
|
150
|
|
|
/** @var \gplcart\modules\api\models\Api $instance */ |
|
151
|
|
|
$instance = Container::get('gplcart\\modules\\api\\models\\Api'); |
|
152
|
|
|
return $instance; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns User model instance |
|
157
|
|
|
* @return \gplcart\modules\api\models\User |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getUserModel() |
|
160
|
|
|
{ |
|
161
|
|
|
/** @var \gplcart\modules\api\models\User $instance */ |
|
162
|
|
|
$instance = Container::get('gplcart\\modules\\api\\models\\User'); |
|
163
|
|
|
return $instance; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|