/
EditApiKey.php
228 lines (205 loc) · 7.13 KB
/
EditApiKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2021 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Controller;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
use FacturaScripts\Core\Lib\ExtendedController\EditController;
use FacturaScripts\Dinamic\Model\ApiAccess;
/**
* Controller to edit a single item from the ApiKey model.
*
* @author Francesc Pineda Segarra <francesc.pineda.segarra@gmail.com>
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
class EditApiKey extends EditController
{
/**
* @return array
*/
public function getAccessRules(): array
{
$rules = [];
foreach ($this->getResources() as $resource) {
$rules[$resource] = [
'allowget' => false,
'allowpost' => false,
'allowput' => false,
'allowdelete' => false
];
}
$accessModel = new ApiAccess();
$where = [new DataBaseWhere('idapikey', $this->request->query->get('code'))];
foreach ($accessModel->all($where, [], 0, 0) as $access) {
$rules[$access->resource]['allowget'] = $access->allowget;
$rules[$access->resource]['allowpost'] = $access->allowpost;
$rules[$access->resource]['allowput'] = $access->allowput;
$rules[$access->resource]['allowdelete'] = $access->allowdelete;
}
return $rules;
}
/**
* Returns the model name.
*
* @return string
*/
public function getModelClassName()
{
return 'ApiKey';
}
/**
* Returns basic page attributes.
*
* @return array
*/
public function getPageData()
{
$data = parent::getPageData();
$data['menu'] = 'admin';
$data['title'] = 'api-key';
$data['icon'] = 'fas fa-key';
return $data;
}
/**
* Load views.
*/
protected function createViews()
{
parent::createViews();
$this->createViewsAccess();
$this->setTabsPosition('bottom');
}
/**
* @param string $viewName
*/
protected function createViewsAccess(string $viewName = 'ApiAccess')
{
$this->addHtmlView($viewName, 'Tab/ApiAccess', 'ApiAccess', 'rules', 'fas fa-check-square');
}
/**
* @return bool
*/
protected function editRulesAction(): bool
{
// check user permissions
if (false === $this->permissions->allowUpdate) {
$this->toolBox()->i18nLog()->warning('not-allowed-update');
return true;
} elseif (false === $this->validateFormToken()) {
return true;
}
$allowGet = $this->request->request->get('allowget');
$allowPut = $this->request->request->get('allowput');
$allowPost = $this->request->request->get('allowpost');
$allowDelete = $this->request->request->get('allowdelete');
// update current access rules
$accessModel = new ApiAccess();
$where = [new DataBaseWhere('idapikey', $this->request->query->get('code'))];
$rules = $accessModel->all($where, [], 0, 0);
foreach ($rules as $access) {
$access->allowget = is_array($allowGet) && in_array($access->resource, $allowGet);
$access->allowput = is_array($allowPut) && in_array($access->resource, $allowPut);
$access->allowpost = is_array($allowPost) && in_array($access->resource, $allowPost);
$access->allowdelete = is_array($allowDelete) && in_array($access->resource, $allowDelete);
$access->save();
}
// add new rules
foreach ($allowGet as $resource) {
$found = false;
foreach ($rules as $rule) {
if ($rule->resource === $resource) {
$found = true;
break;
}
}
if ($found) {
continue;
}
// add
$newAccess = new ApiAccess();
$newAccess->idapikey = $this->request->query->get('code');
$newAccess->resource = $resource;
$newAccess->allowget = is_array($allowGet) && in_array($resource, $allowGet);
$newAccess->allowput = is_array($allowPut) && in_array($resource, $allowPut);
$newAccess->allowpost = is_array($allowPost) && in_array($resource, $allowPost);
$newAccess->allowdelete = is_array($allowDelete) && in_array($resource, $allowDelete);
$newAccess->save();
}
$this->toolBox()->i18nLog()->notice('record-updated-correctly');
return true;
}
/**
* Run the actions that alter data before reading it.
*
* @param string $action
*
* @return bool
*/
protected function execPreviousAction($action)
{
switch ($action) {
case 'edit-rules':
return $this->editRulesAction();
}
return parent::execPreviousAction($action);
}
/**
* List of all available resources.
*
* @source Based on Core/App/AppAPI.php function getResourcesMap()
*
* @return array
*/
protected function getResources(): array
{
$resources = [];
$path = FS_FOLDER . DIRECTORY_SEPARATOR . 'Dinamic' . DIRECTORY_SEPARATOR . 'Lib' . DIRECTORY_SEPARATOR . 'API';
foreach (scandir($path, SCANDIR_SORT_NONE) as $resource) {
if (substr($resource, -4) !== '.php') {
continue;
}
$class = substr('\\FacturaScripts\\Dinamic\\Lib\\API\\' . $resource, 0, -4);
$APIClass = new $class($this->response, $this->request, []);
if (isset($APIClass) && method_exists($APIClass, 'getResources')) {
foreach ($APIClass->getResources() as $name => $data) {
$resources[] = $name;
}
}
}
sort($resources);
return $resources;
}
/**
* Load view data.
*
* @param string $viewName
* @param BaseView $view
*/
protected function loadData($viewName, $view)
{
$mainViewName = $this->getMainViewName();
switch ($viewName) {
case $mainViewName:
parent::loadData($viewName, $view);
if (false === $view->model->exists()) {
$view->model->nick = $this->user->nick;
}
break;
}
}
}