Skip to content

Commit f4abfa2

Browse files
committed
Add basics for client UI connection
1 parent b2d8d65 commit f4abfa2

File tree

10 files changed

+85
-22
lines changed

10 files changed

+85
-22
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,7 @@ codeception.yml
105105
.vscode
106106
.php-cs-fixer.cache
107107
.phpunit.*
108-
.env
108+
.env
109+
openapi.json
110+
assets/api
111+
openapitools.json

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"main": "gulpfile.js",
55
"dependencies": {
66
"@fullcalendar/moment": "^6.1.5",
7+
"@openapitools/openapi-generator-cli": "^2.25.0",
78
"@ttskch/select2-bootstrap4-theme": "^1.5.2",
89
"admin-lte": "^3.2.0",
910
"autocompleter": "^6.1.1",
@@ -93,12 +94,15 @@
9394
"develop-OSM": "yarn install && yarn run install-OSM && yarn run assets-OSM",
9495
"install-OSM": "php composer.phar update",
9596
"assets-OSM": "yarn up && yarn run build-OSM",
96-
"build-OSM": "gulp",
97+
"build-OSM": "gulp && npm run-script update-openapi-client",
9798
"dump-OSM": "php composer.phar dump-autoload",
9899
"windows-fix": "yarn global add windows-build-tools",
99100
"php-cs-fix": "vendor/bin/php-cs-fixer fix",
100101
"rector": "vendor/bin/rector process",
101-
"setup-corepack": "npm install -g corepack && corepack enable"
102+
"setup-corepack": "npm install -g corepack && corepack enable",
103+
"export-openapi": "php artisan api:openapi:export --output=openapi.json",
104+
"fix-openapi-js": "node -e \"require('fs').writeFileSync('assets/api/src/main.js', \\\"import * as API from './index';global.window.API = API;\\\");\"",
105+
"update-openapi-client": "npm run-script export-openapi && npx @openapitools/openapi-generator-cli generate -i openapi.json -g javascript -o assets/api --skip-validate-spec && npm run-script fix-openapi-js && npm install --no-save ./assets/api && cd ./assets/api && npx babel src -d dist && cd ../.. && npx browserify ./assets/api/dist/main.js -o ./assets/dist/js/api.js"
102106
},
103107
"packageManager": "yarn@4.6.0"
104108
}

routes/console.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
use Illuminate\Support\Facades\Route;
44

5-
Route::fallback([FallbackPageController::class, '__invoke']);
5+
//Route::fallback([FallbackPageController::class, '__invoke']);

src/API/Controllers/DataTablesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
3434
if (!$data instanceof DataTablesLoadRequest) {
3535
throw new InvalidArgumentException();
3636
}
37-
37+
3838
$id_module = !empty($uriVariables['id_module']) ? $uriVariables['id_module'] : null;
3939
$id_plugin = !empty($uriVariables['id_plugin']) ? $uriVariables['id_plugin'] : null;
4040
$id_parent = !empty($uriVariables['id_parent']) ? $uriVariables['id_parent'] : null;

src/App.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class App
6666
'i18n/moment/|lang|.min.js',
6767
'i18n/locales.min.js',
6868
'i18n/@fullcalendar/|lang|.min.js',
69+
'api.js'
6970
],
7071
];
7172

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Controllers;
4+
5+
use Spatie\RouteAttributes\Attributes\Any;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Http\Request;
8+
9+
use GuzzleHttp\Client;
10+
use GuzzleHttp\Exception\RequestException;
11+
use Illuminate\Http\JsonResponse;
12+
use Spatie\RouteAttributes\Attributes\Where;
13+
14+
class APIForUIController extends PageController
15+
{
16+
#[Any('/api-for-ui/{path}')]
17+
#[Where('path', '(.*)')]
18+
public function __invoke(Request $request, string $path = null)
19+
{
20+
$tokens = Auth::user()->getApiTokens();
21+
22+
// Determine actual API path
23+
$url = str_replace("api-for-ui/", "",$request->fullUrl());
24+
25+
// Create a new Guzzle client
26+
$client = new Client();
27+
28+
// Get headers from the incoming request
29+
$incomingHeaders = $request->headers->all();
30+
31+
// Format headers for Guzzle
32+
$formattedHeaders = [];
33+
foreach ($incomingHeaders as $key => $value) {
34+
$formattedHeaders[$key] = implode(', ', $value); // Convert array to string
35+
}
36+
37+
// Attach API token for auth
38+
$formattedHeaders['X-API-Key'] = $tokens[0]['token'];
39+
40+
try {
41+
// Make the GET request
42+
$response = $client->request($request->method(), $url, [
43+
'headers' => $formattedHeaders,
44+
'json' => $request->all(),
45+
]);
46+
47+
// Get the body of the response
48+
$body = $response->getBody();
49+
$data = json_decode($body, true); // Decode JSON to array
50+
51+
// Return the response data
52+
return response()->json($data);
53+
} catch (RequestException $e) {
54+
// Handle different types of errors
55+
$statusCode = $e->getResponse() ? $e->getResponse()->getStatusCode() : 500;
56+
return response()->json([
57+
'error' => $e->getMessage(),
58+
'status_code' => $statusCode,
59+
], $statusCode);
60+
}
61+
}
62+
}

src/DTO/DataTablesLoadRequest/DataTablesLoadRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
final class DataTablesLoadRequest
77
{
8-
public int $draw;
9-
public int $start;
10-
public int $length;
8+
public int $draw = 0;
9+
public int $start = 0;
10+
public int $length = 200;
1111
/**
1212
* @var Search
1313
*/

src/DTO/DataTablesLoadResponse/DataTablesLoadResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DataTablesLoadResponse
99
* @param int $draw
1010
* @param int $recordsTotal
1111
* @param int $recordsFiltered
12-
* @param array<map<string, string>> $data To be defined as DTO in the future
12+
* @param array<string, string> $data To be defined as DTO in the future
1313
* @param array<string> $summable
1414
* @param array<string> $avg
1515
* @param string|null $error

src/Middlewares/APIAuthMiddleware.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ public function handle(Request $request, \Closure $next): Response
2222

2323
$user = null;
2424
if (!empty($token)) {
25-
$user_match = UserTokens::where('enabled', 1)->find($token);
25+
$user_match = UserTokens::where('enabled', 1)->where('token', $token)->first();
2626

27-
if ($user_match) {
27+
if (!empty($user_match)) {
2828
$user = User::with('group')->find($user_match->id_utente);
2929
}
3030
}
3131

3232
if ($user) {
33-
Auth::once($user);
33+
Auth::login($user, false);
34+
auth_osm()->identifyUser($user->id);
3435

3536
return $next($request);
3637
}
3738
}
3839

3940
// Disabilita autenticazione su base delle opzioni
40-
if (config('osm.api_development', false)) {
41+
if (config('osm.api_development', false) && !$request->headers->has('X-API-Key')) {
4142
return $next($request);
4243
}
4344

src/Models/UserTokens.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ class UserTokens extends Model
3131

3232
protected $table = 'zz_tokens';
3333

34-
public static function build(?Group $gruppo = null, $username = null, $email = null, $password = null)
35-
{
36-
$model = new static();
37-
$model->save();
38-
39-
return $model;
40-
}
41-
4234
/* Relazioni Eloquent */
4335

4436
public function user()
4537
{
46-
return $this->belongsTo(User::class, 'id_utente');
38+
return $this->belongsTo(User::class, 'id_utente', 'id');
4739
}
4840
}

0 commit comments

Comments
 (0)