Procházet zdrojové kódy

Endpoint FestAgent: disattiva allineata e token solo se attivo.

Un agent disabilitato non pesca piu i job di stampa.

Co-authored-by: Cursor <cursoragent@cursor.com>
marcofalabretti před 2 týdny
rodič
revize
f175056148

+ 24
- 3
app/DataTables/EndpointDataTableEditor.php Zobrazit soubor

@@ -11,6 +11,8 @@ use Illuminate\Http\Request;
11 11
 use Illuminate\Validation\ValidationException;
12 12
 use Illuminate\Support\Facades\Storage;
13 13
 use Illuminate\Support\Str;
14
+use App\Services\Attivita\AttivitaService;
15
+use Illuminate\Support\Facades\Session;
14 16
 
15 17
 class EndpointDataTableEditor extends DataTablesEditor
16 18
 {
@@ -81,14 +83,33 @@ class EndpointDataTableEditor extends DataTablesEditor
81 83
 
82 84
   public function creating(Model $model, array $data): array
83 85
   {
84
-    // $model->roles()->sync([$data['ruolo_id']]);
86
+    $data = $this->senzaSegreti($data);
87
+    $data['attivita_id'] = Session::get('attivita_attuale');
88
+    AttivitaService::garantisciAttivita($data['attivita_id']);
89
+
85 90
     return $data;
86 91
   }
87 92
 
88 93
   public function updating(Model $model, array $data): array
89 94
   {
90
-    // dd($data['ruolo']);
91
-    // $model->roles()->sync([$data['ruolo_id']]);
95
+    AttivitaService::garantisciAttivita($model->attivita_id);
96
+    $data = $this->senzaSegreti($data);
97
+    unset($data['attivita_id']);
98
+
99
+    return $data;
100
+  }
101
+
102
+  public function deleting(Model $model, array $data): array
103
+  {
104
+    AttivitaService::garantisciAttivita($model->attivita_id);
105
+
106
+    return $data;
107
+  }
108
+
109
+  private function senzaSegreti(array $data): array
110
+  {
111
+    unset($data['token'], $data['product_key'], $data['secret'], $data['deeplink_token']);
112
+
92 113
     return $data;
93 114
   }
94 115
 

+ 86
- 30
app/Http/Controllers/EndpointController.php Zobrazit soubor

@@ -2,20 +2,23 @@
2 2
 
3 3
 namespace App\Http\Controllers;
4 4
 
5
+
6
+use App\Models\Endpoint;
7
+use App\Models\PrintJob;
8
+use App\Models\Attivita;
9
+use App\Models\Dispositivo;
5 10
 use Illuminate\Http\Request;
6 11
 use Illuminate\Support\Facades\Log;
7 12
 use Illuminate\Support\Facades\Http;
8
-use App\Models\Endpoint;
9
-use App\Models\Dispositivo;
10
-use App\Models\PrintJob;
11
-use App\DataTables\EndpointDataTable;
13
+use Illuminate\Routing\Controllers\Middleware;
12 14
 use App\DataTables\EndpointDataTableEditor;
15
+use App\DataTables\EndpointDataTable;
13 16
 use Illuminate\Support\Facades\Validator;
14
-use App\Models\Attivita;
15
-use Illuminate\Support\Str;
16 17
 use Illuminate\Support\Facades\Session;
17
-use Illuminate\Support\Facades\Auth;
18 18
 use Illuminate\Support\Facades\Storage;
19
+use Illuminate\Support\Facades\Auth;
20
+use Illuminate\Support\Str;
21
+use App\Services\Attivita\AttivitaService;
19 22
 
20 23
 class EndpointController extends Controller
21 24
 {
@@ -31,9 +34,9 @@ class EndpointController extends Controller
31 34
     public static function middleware(): array
32 35
     {
33 36
         return [
34
-            new Middleware('permission:view-endpoint', only: ['index', 'show', 'istruzioni']),
37
+            new Middleware('permission:view-endpoint', only: ['index', 'show', 'istruzioni', 'download_festagent']),
35 38
             new Middleware('permission:create-endpoint|edit-endpoint|delete-endpoint', only: ['store', 'update', 'destroy']),
36
-            new Middleware('permission:create-endpoint', only: ['richiedi_nuovo_product_key']),
39
+            new Middleware('permission:create-endpoint', only: ['richiedi_nuovo_product_key', 'collega_questo_pc']),
37 40
         ];
38 41
     }
39 42
 
@@ -52,6 +55,8 @@ class EndpointController extends Controller
52 55
             abort(404);
53 56
         }
54 57
 
58
+        AttivitaService::garantisciAttivita($endpoint->attivita_id);
59
+        
55 60
         return view('endpoint.show', compact('endpoint'));
56 61
     }
57 62
 
@@ -67,7 +72,10 @@ class EndpointController extends Controller
67 72
     //deeplink
68 73
     public function collega_questo_pc(Request $request)
69 74
     {
70
-        $endpoint = Endpoint::where('attivita_id', Session::get('attivita_attuale'))
75
+        $attivitaId = Session::get('attivita_attuale');
76
+        AttivitaService::garantisciAttivita($attivitaId);
77
+
78
+        $endpoint = Endpoint::where('attivita_id', $attivitaId)
71 79
         ->where('user_id', $request->user()->id)
72 80
         ->where('status', Endpoint::NON_REGISTRATO)
73 81
         ->first();
@@ -78,7 +86,7 @@ class EndpointController extends Controller
78 86
         }else{
79 87
 
80 88
         $endpoint = new Endpoint();
81
-        $endpoint->attivita_id = $request->attivita_id;
89
+        $endpoint->attivita_id = $attivitaId;
82 90
         $endpoint->user_id = $request->user()->id;
83 91
         $endpoint->deeplink_token = ['token' => Str::random(32) , 'timestamp' => now()->timestamp , 'status' => 'pending'];
84 92
         $endpoint->product_key = $this->creaProductKey();
@@ -95,7 +103,7 @@ class EndpointController extends Controller
95 103
             'message' => 'Endpoint collegato con successo',
96 104
             'status' => 'success',
97 105
             'success' => true,
98
-            'endpoint' => $endpoint,
106
+            'endpoint' => $endpoint->makeHidden(['product_key', 'secret', 'deeplink_token', 'token']),
99 107
             'url' => url('fest-agent://pair?token=' . $endpoint->deeplink_token['token'] . '&server=' . $server),
100 108
         ]);
101 109
     }
@@ -132,6 +140,7 @@ class EndpointController extends Controller
132 140
         }
133 141
         $endpoint->update([
134 142
             'status' => Endpoint::DISABILITATO,
143
+            'token' => null,
135 144
         ]);
136 145
         return response()->json([
137 146
             'message' => 'Endpoint disattivato con successo',
@@ -145,13 +154,29 @@ class EndpointController extends Controller
145 154
             'token' => 'required|string|max:255',
146 155
         ]);
147 156
         $endpoint = Endpoint::where('deeplink_token->token', $request->token)->first();
157
+
158
+        //Se non c'è return
148 159
         if(!$endpoint){
149 160
             return response()->json(['message' => 'Endpoint non trovato', 'status' => 'error', 'success' => false], 404);
150 161
         }
162
+
163
+        // Se scaduto dopo 10 minuti return
164
+        if($endpoint->deeplink_token['timestamp'] < now()->subMinutes(10)->timestamp){
165
+            $endpoint->deeplink_token['status'] = 'expired';
166
+            $endpoint->save();
167
+            return response()->json(['message' => 'Token scaduto', 'status' => 'error', 'success' => false], 400);
168
+        }
169
+
170
+        //Se già usato return
171
+        if($endpoint->deeplink_token['status'] == 'paired'){
172
+            return response()->json(['message' => 'Endpoint già registrato', 'status' => 'error', 'success' => false], 400);
173
+        }
174
+
175
+
151 176
         $deeplink_token = $endpoint->deeplink_token;
152 177
         $deeplink_token['status'] = 'paired';
153 178
         $endpoint->update([
154
-            'deeplink_token' => $deeplink_token,
179
+            'deeplink_token' => '',//$deeplink_token,
155 180
             'status' => Endpoint::REGISTRATO,
156 181
             'token' => Str::random(32),
157 182
         ]);
@@ -159,7 +184,7 @@ class EndpointController extends Controller
159 184
             'message' => 'Endpoint registrato con successo',
160 185
             'status' => 'success',
161 186
             'success' => true,
162
-            'endpoint' => $endpoint,
187
+            'endpoint' => $endpoint->makeHidden(['product_key', 'secret', 'deeplink_token']),
163 188
             'token' => $endpoint->token,
164 189
         ]);
165 190
     }
@@ -192,8 +217,8 @@ class EndpointController extends Controller
192 217
 
193 218
     public function stampanti_register(Request $request)
194 219
     {
195
-        Log::info('stampanti_register');
196
-        Log::info($request->all());
220
+        // Log::info('stampanti_register');
221
+        // Log::info($request->all());
197 222
 
198 223
         $stampanti_da_registrare = $request->input('stampanti', []);
199 224
         $endpoint = $request->endpoint;
@@ -287,12 +312,16 @@ class EndpointController extends Controller
287 312
     public function esito_stampa(Request $request)
288 313
 {
289 314
     $request->validate([
290
-        'printJob_id' => 'required|integer|exists:print_job,id',
315
+        'printJob_id' => 'required|integer',
291 316
         'esito' => 'required|string|in:success,error',
292 317
     ]);
293 318
 
294 319
     $printJob = PrintJob::query()
295 320
         ->whereKey($request->printJob_id)
321
+        ->whereIn(
322
+            'stampante_id',
323
+            $request->endpoint->stampanti()->pluck('id')
324
+        )
296 325
         ->first();
297 326
 
298 327
     if (! $printJob) {
@@ -365,11 +394,28 @@ class EndpointController extends Controller
365 394
                     break;
366 395
             }
367 396
         }
397
+        if (in_array($request->input('action'), ['edit', 'remove'], true)) {
398
+            foreach (array_keys($request->input('data', [])) as $id) {
399
+                AttivitaService::garantisciAttivita(Endpoint::find($id)?->attivita_id);
400
+            }
401
+        }
402
+
368 403
         return $editor->process($request);
369 404
     }
370 405
     public function index_api(Request $request)
371 406
     {
372
-        $endpoints = Endpoint::all();
407
+        if($request->attivita_id){
408
+            $attivita_id = $request->attivita_id;
409
+        }else{
410
+            return response()->json([
411
+                'message' => 'Attività non trovata',
412
+                'status' => 'error',
413
+                'success' => false,
414
+            ], 404);
415
+        }
416
+        AttivitaService::api_garantisciAttivita($attivita_id);
417
+
418
+        $endpoints = Endpoint::where('attivita_id', $attivita_id)->get()->makeHidden(['product_key', 'secret', 'deeplink_token','token']);
373 419
         return response()->json([
374 420
          'message' => 'Endpoint trovati con successo',
375 421
          'count' => $endpoints->count(),
@@ -384,11 +430,11 @@ class EndpointController extends Controller
384 430
 
385 431
         if($request->has('first_time') && $request->first_time == true){
386 432
             //la prima registrazione
387
-            Log::info('api_register_product_key');
388
-            Log::info('FIRST TIME');
389
-            Log::info('Request: ' . json_encode($request->all()));
433
+            // Log::info('api_register_product_key');
434
+            // Log::info('FIRST TIME');
435
+            // Log::info('Request: ' . json_encode($request->all()));
390 436
         $validator = Validator::make($request->all(), [
391
-            'product_key' => 'required|string|max:25,exists:endpoint,product_key',
437
+            'product_key' => 'required|string|max:25|exists:endpoint,product_key',
392 438
         ], [
393 439
             'product_key.required' => 'Il product key è richiesto',
394 440
             'product_key.string' => 'Il product key deve essere una stringa',
@@ -398,7 +444,7 @@ class EndpointController extends Controller
398 444
 
399 445
         if($validator->fails()){
400 446
             Log::error('api_register_product_key');
401
-            Log::error('Request: ' . $request->all());
447
+            // Log::error('Request: ' . $request->all());
402 448
             Log::error('Validator fails: ' . $validator->messages());
403 449
             return response()->json([
404 450
                 'message' => $validator->messages(),
@@ -416,10 +462,9 @@ class EndpointController extends Controller
416 462
                 'status' => 'error',
417 463
                 'success' => false,
418 464
             ], 404);
419
-        }elseif($endpoint->status == Endpoint::REGISTRATO){
420
-            Log::error('Endpoint già registrato');
465
+        }elseif($endpoint->status != Endpoint::NON_REGISTRATO){
421 466
             return response()->json([
422
-                'message' => 'Endpoint già registrato',
467
+                'message' => 'Endpoint non può essere registrato',
423 468
                 'status' => 'error',
424 469
                 'success' => false,
425 470
             ], 400);
@@ -451,7 +496,7 @@ class EndpointController extends Controller
451 496
             'message' => 'Endpoint trovato con successo',
452 497
             'status' => 'success',
453 498
             'success' => true,
454
-            'endpoint' => $endpoint,
499
+            'endpoint' => $endpoint->makeHidden(['product_key', 'secret', 'deeplink_token','token']),
455 500
             'licenza' => $licenza??null,
456 501
             'attivita' => $attivita??null,
457 502
         ]);
@@ -460,13 +505,14 @@ class EndpointController extends Controller
460 505
         //se non è la prima registrazione può modificare gli attributi dell'endpoint
461 506
         Log::info('api_register_product_key');
462 507
         Log::info('NOT ft');
463
-        Log::info('Request: ' . json_encode($request->all()));
508
+        // Log::info('Request: ' . json_encode($request->all()));
464 509
         // dd($request->all() , 'NOT ft');
465 510
 
466 511
         $validator = Validator::make($request->all(), [
467 512
             'label' => 'required|string|max:255',
468 513
             'descrizione' => 'nullable|string|max:255',
469 514
             'ubicazione' => 'nullable|string|max:255',
515
+            'product_key' => 'nullable|string|max:25|exists:endpoint,product_key',
470 516
         ], [
471 517
             'label.required' => 'Il label è richiesto',
472 518
             'label.string' => 'Il label deve essere una stringa',
@@ -475,6 +521,9 @@ class EndpointController extends Controller
475 521
             'descrizione.max' => 'La descrizione deve essere lungo al massimo 255 caratteri',
476 522
             'ubicazione.string' => 'L\'ubicazione deve essere una stringa',
477 523
             'ubicazione.max' => 'L\'ubicazione deve essere lungo al massimo 255 caratteri',
524
+            'product_key.string' => 'Il product key deve essere una stringa',
525
+            'product_key.max' => 'Il product key deve essere lungo al massimo 25 caratteri',
526
+            'product_key.exists' => 'Il product key non è valido',
478 527
         ]);
479 528
 
480 529
         if($validator->fails()){
@@ -490,7 +539,7 @@ class EndpointController extends Controller
490 539
         $endpoint = Endpoint::where('product_key', $request->product_key)->where('status', Endpoint::REGISTRATO)->first();
491 540
         if(!$endpoint){
492 541
             Log::error('Endpoint non trovato');
493
-            Log::error('Request: ' . json_encode($request->all()));
542
+            // Log::error('Request: ' . json_encode($request->all()));
494 543
             return response()->json([
495 544
                 'message' => 'Endpoint non trovato',
496 545
                 'status' => 'error',
@@ -510,7 +559,7 @@ class EndpointController extends Controller
510 559
             'message' => 'Endpoint aggiornato con successo',
511 560
             'status' => 'success',
512 561
             'success' => true,
513
-            'endpoint' => $endpoint,
562
+            'endpoint' => $endpoint->makeHidden(['product_key', 'secret', 'deeplink_token','token']),
514 563
             'token' => $endpoint->token,
515 564
         ]);
516 565
 
@@ -527,6 +576,7 @@ class EndpointController extends Controller
527 576
             'attivita_id.exists' => 'L\'attività non è valida',
528 577
         ]);
529 578
 
579
+        AttivitaService::garantisciAttivita($request->attivita_id);
530 580
 
531 581
         $attivita = Attivita::find($request->attivita_id);
532 582
         
@@ -551,6 +601,12 @@ class EndpointController extends Controller
551 601
 
552 602
     public function download_festagent(Request $request)
553 603
     {
604
+        $filePath = '001_fest.exe';
605
+
606
+        if (!Storage::disk('festAgent')->exists($filePath)) {
607
+            abort(404, 'Il file richiesto non esiste.');
608
+        }
609
+   
554 610
         return Storage::disk('festAgent')->download('001_fest.exe', '001_fest.exe');   
555 611
     }
556 612
 }

+ 13
- 4
app/Http/Controllers/MappaDispositiviController.php Zobrazit soubor

@@ -14,6 +14,7 @@ use App\DataTables\DispositivoDataTableEditor;
14 14
 use Illuminate\Support\Facades\Auth;
15 15
 use Illuminate\Support\Facades\Session;
16 16
 use Illuminate\Routing\Controllers\Middleware;
17
+use App\Services\Attivita\AttivitaService;
17 18
 
18 19
 class MappaDispositiviController extends Controller
19 20
 {
@@ -28,13 +29,14 @@ class MappaDispositiviController extends Controller
28 29
     public static function middleware(): array
29 30
     {
30 31
         return [
31
-            new Middleware('permission:view-mappa_dispositivi', only: ['index']),
32
+            new Middleware('permission:view-mappa_dispositivi', only: ['index', 'show']),
32 33
             new Middleware('permission:create-mappa_dispositivi|edit-mappa_dispositivi|delete-mappa_dispositivi', only: ['store', 'update', 'destroy']),
33 34
         ];
34 35
     }
35 36
     
36 37
     public function index(DispositivoDataTable $dataTable)
37 38
     {
39
+        $dataTable->attivita_id = Session::get('attivita_attuale');
38 40
         if (request()->ajax()) {
39 41
             return $dataTable->ajax();
40 42
         }
@@ -93,6 +95,11 @@ class MappaDispositiviController extends Controller
93 95
     public function show($dispositivo_id)
94 96
     {
95 97
         $dispositivo = Dispositivo::find($dispositivo_id);
98
+        if (! $dispositivo) {
99
+            abort(404);
100
+        }
101
+        AttivitaService::garantisciAttivita($dispositivo->attivita_id);
102
+
96 103
         return view('mappa_dispositivi.show', ['dispositivo' => $dispositivo]);
97 104
     }
98 105
 
@@ -105,9 +112,11 @@ class MappaDispositiviController extends Controller
105 112
         $grouped = array_fill_keys($tipi, []);
106 113
         $grouped['_altro'] = [];
107 114
 
108
-        $attivitaAttiveIds = Attivita::query()
109
-            ->where('is_attiva', true)
110
-            ->pluck('id');
115
+        $attivitaQuery = Attivita::query()->where('is_attiva', true);
116
+        if (! Auth::user()?->hasRole('superadmin')) {
117
+            $attivitaQuery->where('user_id', Auth::id());
118
+        }
119
+        $attivitaAttiveIds = $attivitaQuery->pluck('id');
111 120
 
112 121
         $devices = Dispositivo::query()
113 122
             ->with('attivita:id,nome')

+ 16
- 7
app/Http/Middleware/EndpointTokenMiddleware.php Zobrazit soubor

@@ -20,18 +20,27 @@ class EndpointTokenMiddleware
20 20
         if(!$token){
21 21
             Log::error('token-endpoint');
22 22
             Log::error('Token endpoint non trovato');
23
-            Log::error('Request: ' . json_encode($request->all()));
24
-            Log::error('Headers: ' . json_encode($request->header()));
23
+            // Log::error('Request: ' . json_encode($request->all()));
24
+            // Log::error('Headers: ' . json_encode($request->header()));
25 25
             return response()->json(['message' => 'Token endpoint non trovato', 'status' => 'error', 'success' => false], 401);
26 26
         }
27 27
         $endpoint = Endpoint::where('token', $token)->first();
28 28
         if(!$endpoint){
29
-            Log::error('token-endpoint');
30
-            Log::error('Endpoint non trovato con token ' . $token);
31
-            Log::error('Request: ' . json_encode($request->all()));
32
-            return response()->json(['message' => 'Endpoint non trovato con token ' . $token, 'status' => 'error', 'success' => false], 404);
29
+            Log::error('endpoint non trovato con token ');
30
+            return response()->json(['message' => 'Endpoint non trovato con token ', 'status' => 'error', 'success' => false], 404);
31
+        }
32
+        if ($endpoint->status !== Endpoint::ATTIVO) {
33
+            Log::warning('Endpoint non attivo', [
34
+                'endpoint_id' => $endpoint->id,
35
+                'status' => $endpoint->status,
36
+            ]);
37
+            return response()->json([
38
+                'message' => 'Endpoint non attivo',
39
+                'status' => 'error',
40
+                'success' => false,
41
+            ], 403);
33 42
         }
34
-        $request->merge(['endpoint' => $endpoint]);
43
+        $request->merge(['endpoint' => $endpoint->makeHidden(['product_key', 'secret', 'deeplink_token', 'token'])]);
35 44
         return $next($request);
36 45
     }
37 46
 }

+ 6
- 4
resources/views/endpoint/index.blade.php Zobrazit soubor

@@ -243,11 +243,13 @@ $configData = Helper::appClasses();
243 243
     $.ajax({
244 244
       url: "{{ route('endpoint.collega-questo-pc' , ['attivita_id' => session()->get('attivita_attuale')]) }}",
245 245
       type: 'POST',
246
-      success: function(response){
247
-        window.open(response.url, '_blank');
248
-      }
246
+      // success: function(response){
247
+      //   window.open(response.url, '_blank');
248
+      // }
249 249
     }).done(function(response){
250
-      window.open(response.url, '_blank');
250
+      if(response.url){
251
+        window.location.href = response.url;
252
+      }
251 253
       $('#dataTable_endpoint').DataTable().ajax.reload();
252 254
     }).fail(function(xhr, status, error){
253 255
       alert('Errore nel collegamento del PC. Riprova.');

+ 1
- 1
routes/api.php Zobrazit soubor

@@ -56,7 +56,7 @@ Route::middleware(['endpoint-token'])->group(function () {
56 56
     Route::get('/endpoint/get-lavoro', [EndpointController::class, 'get_lavoro']);
57 57
     Route::post('/endpoint/stampanti/esito-stampa', [EndpointController::class, 'esito_stampa']);
58 58
     Route::get('/endpoint-check', [EndpointController::class, 'api_endpoint_check'])->name('api.endpoint.check');
59
-    Route::post('/endpoint/disattiva', [EndpointController::class, 'api_disattiva_endpoint'])->name('api.endpoint.disattiva');
59
+    Route::post('/endpoint/disattiva', [EndpointController::class, 'api_endpoint_disattiva'])->name('api.endpoint.disattiva');
60 60
 });
61 61
 
62 62
 Route::get('/endpoint/registra-product-key', [EndpointController::class, 'api_register_product_key'])->middleware('throttle:10,1');

Loading…
Zrušit
Uložit