|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Modules\Api\Controllers; |
|
26
|
|
|
|
|
27
|
|
|
use DI\Container; |
|
28
|
|
|
use Klein\Klein; |
|
29
|
|
|
use Psr\Container\ContainerInterface; |
|
30
|
|
|
use SP\Core\Context\StatelessContext; |
|
31
|
|
|
use SP\Core\Events\EventDispatcher; |
|
32
|
|
|
use SP\Core\Exceptions\SPException; |
|
33
|
|
|
use SP\Http\Json; |
|
34
|
|
|
use SP\Services\Api\ApiResponse; |
|
35
|
|
|
use SP\Services\Api\ApiService; |
|
36
|
|
|
use SP\Services\Api\JsonRpcResponse; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class ControllerBase |
|
40
|
|
|
* |
|
41
|
|
|
* @package SP\Modules\Api\Controllers |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract class ControllerBase |
|
44
|
|
|
{ |
|
45
|
|
|
const SEARCH_COUNT_ITEMS = 25; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var ContainerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $dic; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $controllerName; |
|
54
|
|
|
/** |
|
55
|
|
|
* @var |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $actionName; |
|
58
|
|
|
/** |
|
59
|
|
|
* @var StatelessContext |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $context; |
|
62
|
|
|
/** |
|
63
|
|
|
* @var EventDispatcher |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $eventDispatcher; |
|
66
|
|
|
/** |
|
67
|
|
|
* @var ApiService |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $apiService; |
|
70
|
|
|
/** |
|
71
|
|
|
* @var Klein |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $router; |
|
74
|
|
|
/** |
|
75
|
|
|
* @var bool |
|
76
|
|
|
*/ |
|
77
|
|
|
private $isAuthenticated = false; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Constructor |
|
81
|
|
|
* |
|
82
|
|
|
* @param Container $container |
|
83
|
|
|
* @param string $actionName |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \DI\DependencyException |
|
86
|
|
|
* @throws \DI\NotFoundException |
|
87
|
|
|
*/ |
|
88
|
|
|
public final function __construct(Container $container, $actionName) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->dic = $container; |
|
91
|
|
|
$this->context = $container->get(StatelessContext::class); |
|
92
|
|
|
$this->eventDispatcher = $container->get(EventDispatcher::class); |
|
93
|
|
|
$this->router = $container->get(Klein::class); |
|
94
|
|
|
$this->apiService = $container->get(ApiService::class); |
|
95
|
|
|
|
|
96
|
|
|
$this->controllerName = $this->getControllerName(); |
|
97
|
|
|
$this->actionName = $actionName; |
|
98
|
|
|
|
|
99
|
|
|
if (method_exists($this, 'initialize')) { |
|
100
|
|
|
$this->initialize(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
final protected function getControllerName() |
|
108
|
|
|
{ |
|
109
|
|
|
$class = static::class; |
|
110
|
|
|
|
|
111
|
|
|
return substr($class, strrpos($class, '\\') + 1, -strlen('Controller')) ?: ''; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function isAuthenticated() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->isAuthenticated; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param int $actionId |
|
124
|
|
|
* |
|
125
|
|
|
* @throws SPException |
|
126
|
|
|
* @throws \SP\Services\ServiceException |
|
127
|
|
|
*/ |
|
128
|
|
|
final protected function setupApi($actionId) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->apiService->setup($actionId); |
|
131
|
|
|
|
|
132
|
|
|
$this->isAuthenticated = true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Devuelve una respuesta en formato JSON con el estado y el mensaje. |
|
137
|
|
|
* |
|
138
|
|
|
* {"jsonrpc": "2.0", "result": 19, "id": 3} |
|
139
|
|
|
* |
|
140
|
|
|
* @param \SP\Services\Api\ApiResponse $apiResponse |
|
141
|
|
|
*/ |
|
142
|
|
|
final protected function returnResponse(ApiResponse $apiResponse) |
|
143
|
|
|
{ |
|
144
|
|
|
try { |
|
145
|
|
|
if ($this->isAuthenticated === false) { |
|
146
|
|
|
throw new SPException(__u('Acceso no permitido')); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->sendJsonResponse(JsonRpcResponse::getResponse($apiResponse, $this->apiService->getRequestId())); |
|
150
|
|
|
} catch (SPException $e) { |
|
151
|
|
|
processException($e); |
|
152
|
|
|
|
|
153
|
|
|
$this->returnResponseException($e); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns a JSON response back to the browser |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $response |
|
161
|
|
|
*/ |
|
162
|
|
|
final private function sendJsonResponse(string $response) |
|
163
|
|
|
{ |
|
164
|
|
|
$json = Json::factory($this->router->response()); |
|
165
|
|
|
$json->returnRawJson($response); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param \Exception $e |
|
170
|
|
|
*/ |
|
171
|
|
|
final protected function returnResponseException(\Exception $e) |
|
172
|
|
|
{ |
|
173
|
|
|
$this->sendJsonResponse(JsonRpcResponse::getResponseException($e, $this->apiService->getRequestId())); |
|
174
|
|
|
} |
|
175
|
|
|
} |