From be765fafec08b17cf2aa184c1046d8509373b3d9 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 11:55:54 +0100 Subject: [PATCH 01/44] Delete & Restore, Index & Show utenti; fix generali --- app/Http/Controllers/SettingsController.php | 2 +- app/Http/Controllers/UserController.php | 47 +++--- app/Models/User.php | 4 + app/Providers/UserServiceProvider.php | 37 ++++- resources/views/devices/index.blade.php | 7 +- resources/views/settings/edit.blade.php | 4 +- resources/views/users/index.blade.php | 149 +++++++++++++------- resources/views/users/show.blade.php | 77 +++++++++- routes/breadcrumbs.php | 4 + routes/web.php | 30 ++-- 10 files changed, 256 insertions(+), 105 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index f48b873a..56b7b622 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -60,7 +60,7 @@ public function update() $user->fill($data); $service = new UserServiceProvider(); - $service->update('/user/' . $user->getAuthIdentifier(), $user); + $service->update($user->getAuthIdentifier(), $user); Auth::login($user); return redirect('/settings/edit'); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 07202080..5bc2a491 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -58,52 +58,37 @@ public function edit($user) public function store() { $data = request()->validate([ - - ]); - $request = new Client([ - 'base_uri' => 'localhost:9999', - 'headers' => [ - 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer ' . session()->get('token') - ] + //TODO ]); $user = new User(); $user->fill($data); - $request->post('/users/create', [ - 'body' => $user - ]); + $this->provider->store($user); } public function update($user) { $data = request()->validate([ - + //TODO ]); $user = $this->provider->retrieveById($user); $user->fill($data); - $request = new Client([ - 'base_uri' => 'localhost:9999', - 'headers' => [ - 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer ' . session()->get('token') - ] - ]); + $this->provider->update($user->getAuthIdentifier(), $user); - $request->put('/user/' . $user->getAuthIdentifier() . '/update', [ - 'body' => $user - ]); } + public function destroy($user) + { + $user = $this->provider->retrieveById($user); + $user->setDeleted(true); + $this->provider->destroy($user->getAuthIdentifier(), $user); + } - public function delete($user) + public function restore($user) { - $request = new Client([ - 'base_uri' => 'localhost:9999', - 'headers' => [ - 'Content-Type' => 'application/json', - 'Authorization' => 'Bearer ' . session()->get('token') - ] - ]); - $request->delete('/user/' . $user); + $user = $this->provider->retrieveById($user); + $user->setDeleted(false); + $this->provider->update($user->getAuthIdentifier(), $user); } + + } diff --git a/app/Models/User.php b/app/Models/User.php index 73a1033d..9a3010ba 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -48,4 +48,8 @@ public function getRole() { return $this->role[$this->type]; } + + public function setDeleted(bool $b){ + $this->deleted = $b; + } } diff --git a/app/Providers/UserServiceProvider.php b/app/Providers/UserServiceProvider.php index 9b020803..9d5bc6f6 100644 --- a/app/Providers/UserServiceProvider.php +++ b/app/Providers/UserServiceProvider.php @@ -153,10 +153,27 @@ private function retriveByCred(Client $request, $credentials) } } - public function update(string $where, string $body) + public function update(string $who, string $body) { + dd($body); try { - $this->request->put($where . '/update', [ + $this->request->put('/user/' . $who . '/update', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'body' => $body + ]); + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + } + } + + public function destroy(string $who, string $body) + { + dd($body); + try { + $this->request->delete('/user/' . $who . '/destroy', [ 'headers' => [ 'Authorization' => 'Bearer ' . session()->get('token') ], @@ -177,6 +194,22 @@ private function isExpired(RequestException $e) } } + public function store(string $body) + { + dd($body); + try { + $this->request->delete('users/store', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'body' => $body + ]); + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + } + } + // =================================================== // Mockup per un utente diff --git a/resources/views/devices/index.blade.php b/resources/views/devices/index.blade.php index 9d2edde8..3281ade0 100644 --- a/resources/views/devices/index.blade.php +++ b/resources/views/devices/index.blade.php @@ -5,7 +5,11 @@

Dispositivi

-
+
+
+
Lista dispositivi
+
+
@@ -58,5 +62,6 @@
+
@endsection diff --git a/resources/views/settings/edit.blade.php b/resources/views/settings/edit.blade.php index 5c0e93ed..62f4bbdb 100644 --- a/resources/views/settings/edit.blade.php +++ b/resources/views/settings/edit.blade.php @@ -3,7 +3,7 @@ @section('content')
-

Impostazioni account

+

Impostazioni account

@@ -138,7 +138,7 @@
-
Notifiche alert attive
+
Notifiche alert
diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index 20aa6772..af01da27 100644 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -2,61 +2,106 @@ @section('breadcrumbs', Breadcrumbs::render('users')) @section('content')
-
-

Dispositivi

+
+
+

Gestione utenti

+
+
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - @foreach($users as $user) +
+
+
Lista utenti
+
+
+
+
IDNomeCognomeEmailRuoloStato
IDNomeCognomeEmailRuoloStato
+ + + + + + + + + + + + + - - - - - - + + + + + + + + - @endforeach - -
IDNomeCognomeEmailRuoloStato
{{$user->userId}}{{$user->name}}{{$user->surname}}{{$user->email}}{{$user->getRole()}} - @if($user->deleted) - Disattivo - @else - Attivo - @endif - - - - - Dettagli - - IDNomeCognomeEmailRuoloStato
+ + + @foreach($users as $user) + + {{$user->userId}} + {{$user->name}} + {{$user->surname}} + {{$user->email}} + {{$user->getRole()}} + + @if($user->deleted) + Disattivo + @else + Attivo + @endif + + + + + Dettagli + + + + @if($user->deleted) + + + + + Ripristina + + + @else + + + + + Elimina + + + @endif + + + + + + @endforeach + + +
diff --git a/resources/views/users/show.blade.php b/resources/views/users/show.blade.php index fba286cf..34827469 100644 --- a/resources/views/users/show.blade.php +++ b/resources/views/users/show.blade.php @@ -1,4 +1,79 @@ @extends('layouts.app') - +@section('breadcrumbs', Breadcrumbs::render('user', $user->userId)) @section('content') +
+
+

Profilo

+
+
+
+
+ + Informazioni +
+
+
+
+
+
+
+ + Dettagli +
+
+
+

ID : {{$user->userId}}

+

Nome e cognome : {{$user->name . ' ' . $user->surname}}

+

Ruolo : {{$user->getRole()}}

+
+
+
+
+
+
+
+ + Contatti +
+
+
+

Email : {{$user->email}}

+

Username Telegram : {{$user->telegramName?? 'NA'}}

+

Chat Telegram : {{$user->telegramChat?? 'NA'}}

+
+
+
+
+
+
+
+ + Sicurezza +
+
+
+

Sicurezza account : + @if($user->tfa) + Attivo + @else + Disattivo + @endif

+

Stato : + @if($user->deleted) + Disattivo + @else + Attivo + @endif

+
+
+
+
+
+
+
+
Sensori accessibili
+
+
+ +
@endsection diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 4b7a4d71..9d5e35a2 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -24,3 +24,7 @@ $trail->parent('home'); $trail->push('Gestione Utenti', route('users.index')); }); +Breadcrumbs::for('user', function ($trail, $userId) { + $trail->parent('users'); + $trail->push('Utente ' . $userId, route('users.show', ['userId' => $userId])); +}); diff --git a/routes/web.php b/routes/web.php index 49a82716..176ff463 100644 --- a/routes/web.php +++ b/routes/web.php @@ -21,32 +21,32 @@ Route::get('/login/tfa', 'Auth\LoginController@showTfaForm')->name('tfaLogin'); //routes per gestione profilo -//Route::get('/settings', 'SettingsController@index')->name('settings.index'); Route::get('/settings/edit', 'SettingsController@edit')->name('settings.edit'); Route::put('/settings', 'SettingsController@update')->name('settings.update'); //routes per gestione user -Route::get('/users', 'UserController@index')->middleware('can:isAdmin')->name('users.index'); // limita users agli admin -Route::get('/user/{userId}/edit', 'UserController@edit')->name('users.edit'); -Route::get('/user/{userId}', 'UserController@show')->name('users.show'); -Route::get('/user/create', 'UserController@create')->name('users.create'); -Route::post('/user', 'UserController@store'); -Route::put('/user/{userId}', 'UserController@update'); -Route::delete('/user/{userId}', 'UserController@delete'); +Route::get('/users', 'UserController@index')/*->middleware('can:isAdmin')*/->name('users.index'); // limita users agli admin +Route::get('/users/create', 'UserController@create')->name('users.create');//TODO +Route::post('/users', 'UserController@store')->name('users.store'); +Route::get('/users/{userId}', 'UserController@show')->name('users.show');//TODO +Route::get('/users/{userId}/edit', 'UserController@edit')->name('users.edit');//TODO +Route::put('/users/{userId}', 'UserController@update')->name('users.update'); +Route::put('/users/{userId}', 'UserController@restore')->name('users.restore'); +Route::delete('/users/{userId}', 'UserController@destroy')->name('users.destroy'); //routes per gestione gateways -Route::get('/gateways', 'GatewayController@index')->name('gateway.index'); -Route::get('/gateway/{gatewayId}', 'GatewayController@show')->name('gateway.show'); +Route::get('/gateways', 'GatewayController@index')->name('gateway.index');//TODO +Route::get('/gateway/{gatewayId}', 'GatewayController@show')->name('gateway.show');//TODO //routes per gestione devices Route::get('/devices', 'DeviceController@index')->name('devices.index'); -Route::get('/device/{deviceId}', 'DeviceController@show')->name('devices.show'); +Route::get('/device/{deviceId}', 'DeviceController@show')->name('devices.show');//TODO //routes per gestione devices -Route::get('/device/{deviceId}/sensors', 'SensorController@index')->name('sensors.index'); -Route::get('/device/{deviceId}/sensor/{sensorId}', 'SensorController@show')->name('sensors.show'); +Route::get('/device/{deviceId}/sensors', 'SensorController@index')->name('sensors.index');//TODO +Route::get('/device/{deviceId}/sensor/{sensorId}', 'SensorController@show')->name('sensors.show');//TODO //routes per gestione entity -Route::get('/entities', 'EntityController@index')->name('entities.index'); -Route::get('/entity/{entityId}', 'EntityController@show')->name('entities.show'); +Route::get('/entities', 'EntityController@index')->name('entities.index');//TODO +Route::get('/entity/{entityId}', 'EntityController@show')->name('entities.show');//TODO From 94f2e1f4e5f9ff3e08235a3a8a396c156a72f938 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 16:53:15 +0100 Subject: [PATCH 02/44] Sistemata la CI ed fix a gli user --- app/Http/Controllers/DeviceController.php | 15 +++++++-- app/Http/Controllers/SettingsController.php | 1 - app/Http/Controllers/UserController.php | 4 --- app/Models/Device.php | 14 +++++++++ app/Models/Entity.php | 5 +++ app/Models/User.php | 3 +- app/Providers/DeviceServiceProvider.php | 3 +- app/Providers/EntityServiceProvider.php | 31 ++++++++++++++++++- app/Providers/SensorServiceProvider.php | 34 +++++++++++++++++++++ app/Providers/UserServiceProvider.php | 15 +++++++-- resources/views/devices/index.blade.php | 4 +-- resources/views/devices/show.blade.php | 2 +- routes/breadcrumbs.php | 13 ++++++-- routes/web.php | 13 +++++--- 14 files changed, 134 insertions(+), 23 deletions(-) diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index a2aec453..c91539d1 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -29,7 +29,8 @@ public function __construct() */ public function index() { - $devices = $this->provider->findAll(); + //$devices = $this->provider->findAll(); + ///FAKER $user = new Device(); $arr = array_combine( array('deviceId', 'name', 'frequency', 'gatewayId'), @@ -37,6 +38,7 @@ public function index() ); $user->fill($arr); $devices[] = $user; + //TODO remove return view('devices.index', compact('devices')); } @@ -48,7 +50,16 @@ public function index() */ public function show($device) { - $device = $this->provider->retrieveById($device); + //$device = $this->provider->retrieveById($device); + ///FAKER + $user = new Device(); + $arr = array_combine( + array('deviceId', 'name', 'frequency', 'gatewayId'), + array("1", "dev1", 123, 1) + ); + $user->fill($arr); + $device = $user; + //TODO remove return view('devices.show', compact('device')); } } diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 56b7b622..82adc9bb 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -61,7 +61,6 @@ public function update() $user->fill($data); $service = new UserServiceProvider(); $service->update($user->getAuthIdentifier(), $user); - Auth::login($user); return redirect('/settings/edit'); } } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 5bc2a491..61b938aa 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,7 +4,6 @@ use App\Models\User; use App\Providers\UserServiceProvider; -use GuzzleHttp\Client; use Illuminate\Contracts\Support\Renderable; class UserController extends Controller @@ -73,7 +72,6 @@ public function update($user) $user = $this->provider->retrieveById($user); $user->fill($data); $this->provider->update($user->getAuthIdentifier(), $user); - } public function destroy($user) @@ -89,6 +87,4 @@ public function restore($user) $user->setDeleted(false); $this->provider->update($user->getAuthIdentifier(), $user); } - - } diff --git a/app/Models/Device.php b/app/Models/Device.php index 711ab481..45f74ddb 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -2,9 +2,23 @@ namespace App\Models; +use App\Providers\EntityServiceProvider; +use App\Providers\SensorServiceProvider; use Illuminate\Database\Eloquent\Model; class Device extends Model { protected $fillable = ['deviceId', 'name', 'frequency', 'gatewayId']; + + public function getSensors() + { + $provider = new SensorServiceProvider(); + return $provider->findAllFromDevice($this->deviceId); + } + + public function getEntity() + { + $provider = new EntityServiceProvider(); + return $provider->retrieveByDevice($this->deviceId); + } } diff --git a/app/Models/Entity.php b/app/Models/Entity.php index 98f7eb74..32287686 100644 --- a/app/Models/Entity.php +++ b/app/Models/Entity.php @@ -7,4 +7,9 @@ class Entity extends Model { protected $fillable = ['entityId', 'name', 'location', 'deleted']; + + public function getName() + { + return $this->name; + } } diff --git a/app/Models/User.php b/app/Models/User.php index 9a3010ba..b32d70bc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -49,7 +49,8 @@ public function getRole() return $this->role[$this->type]; } - public function setDeleted(bool $b){ + public function setDeleted(bool $b) + { $this->deleted = $b; } } diff --git a/app/Providers/DeviceServiceProvider.php b/app/Providers/DeviceServiceProvider.php index 60f0bc86..4592ec91 100644 --- a/app/Providers/DeviceServiceProvider.php +++ b/app/Providers/DeviceServiceProvider.php @@ -35,12 +35,13 @@ public function retrieveById($identifier) 'Authorization' => 'Bearer ' . session()->get('token') ] ])->getBody()); + $device = new Device(); $device->fill((array)$response); return $device; } catch (RequestException $e) { $this->isExpired($e); - return null; + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); } } diff --git a/app/Providers/EntityServiceProvider.php b/app/Providers/EntityServiceProvider.php index 3b8dcf0f..bd8ff645 100644 --- a/app/Providers/EntityServiceProvider.php +++ b/app/Providers/EntityServiceProvider.php @@ -30,7 +30,7 @@ public function __construct() public function retrieveById($identifier) { try { - $response = json_decode($this->request->get('enity/' . $identifier, [ + $response = json_decode($this->request->get('entity/' . $identifier, [ 'headers' => [ 'Authorization' => 'Bearer ' . session()->get('token') ] @@ -39,6 +39,7 @@ public function retrieveById($identifier) $entity->fill((array)$response); return $entity; } catch (RequestException $e) { + $this->isExpired($e); abort($e->getCode(), $e->getResponse()->getReasonPhrase()); return null; } @@ -63,8 +64,36 @@ public function findAll() } return $entities; } catch (RequestException $e) { + $this->isExpired($e); abort($e->getCode(), $e->getResponse()->getReasonPhrase()); return null; } } + + public function retrieveByDevice($deviceId){ + try { + $response = json_decode($this->request->get('entities', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'query' => 'deviceId=' . $deviceId + ])->getBody()); + $entity = new Entity(); + $entity->fill((array)$response); + return $entity; + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + return null; + } + } + + private function isExpired(RequestException $e) + { + if ($e->getCode() == 419/*fai il controllo del token*/) { + session()->invalidate(); + session()->flush(); + return redirect('login'); + } + } } diff --git a/app/Providers/SensorServiceProvider.php b/app/Providers/SensorServiceProvider.php index 5a627698..db779d2d 100644 --- a/app/Providers/SensorServiceProvider.php +++ b/app/Providers/SensorServiceProvider.php @@ -39,6 +39,7 @@ public function retrieveById($identifier) $sensor->fill((array)$response); return $sensor; } catch (RequestException $e) { + $this->isExpired($e); abort($e->getCode(), $e->getResponse()->getReasonPhrase()); return null; } @@ -63,8 +64,41 @@ public function findAll() } return $sensors; } catch (RequestException $e) { + $this->isExpired($e); abort($e->getCode(), $e->getResponse()->getReasonPhrase()); return null; } } + + public function findAllFromDevice($deviceId) + { + try { + $response = json_decode($this->request->get('sensors', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'query' => 'deviceId=' . $deviceId + ])->getBody()); + $sensors = []; + foreach ($response as $d) { + $sensor = new Sensor(); + $sensor->fill((array)$d); + $sensors[] = $sensor; + } + return $sensors; + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + return null; + } + } + + private function isExpired(RequestException $e) + { + if ($e->getCode() == 419/*fai il controllo del token*/) { + session()->invalidate(); + session()->flush(); + return redirect('login'); + } + } } diff --git a/app/Providers/UserServiceProvider.php b/app/Providers/UserServiceProvider.php index 9d5bc6f6..8839660d 100644 --- a/app/Providers/UserServiceProvider.php +++ b/app/Providers/UserServiceProvider.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Auth; class UserServiceProvider extends ServiceProvider implements UserProvider { @@ -157,12 +158,22 @@ public function update(string $who, string $body) { dd($body); try { - $this->request->put('/user/' . $who . '/update', [ + $response = json_decode($this->request->put('/user/' . $who . '/update', [ 'headers' => [ 'Authorization' => 'Bearer ' . session()->get('token') ], 'body' => $body - ]); + ])->getBody()); + if (property_exists($response, 'token')) { + $userarray = (array)$response->user; + $userarray['token'] = $response->token; + + session(['token' => $response->token]); + $user = new User(); + $user->fill($userarray); + Auth::login($user); + //TODO testare sta roba + } } catch (RequestException $e) { $this->isExpired($e); abort($e->getCode(), $e->getResponse()->getReasonPhrase()); diff --git a/resources/views/devices/index.blade.php b/resources/views/devices/index.blade.php index 3281ade0..7880f49a 100644 --- a/resources/views/devices/index.blade.php +++ b/resources/views/devices/index.blade.php @@ -40,8 +40,8 @@ Attivo {{$device->deviceId}} {{$device->name}} - {{ "TBD" }} - {{ "TBD" }} + {{count($device->getSensors())}} + {{$device->getEntity()->getName()}} diff --git a/resources/views/devices/show.blade.php b/resources/views/devices/show.blade.php index 04feac72..29f7d988 100644 --- a/resources/views/devices/show.blade.php +++ b/resources/views/devices/show.blade.php @@ -1,5 +1,5 @@ @extends('layouts.app') - +@section('breadcrumbs', Breadcrumbs::render('device', $device->deviceId)) @section('content')
diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 9d5e35a2..0b62b4a5 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -10,16 +10,21 @@ $trail->parent('home'); $trail->push('Impostazioni account', route('settings.edit')); }); - +// Dispositivi Breadcrumbs::for('devices', function ($trail) { $trail->parent('home'); $trail->push('Dispositivi', route('devices.index')); }); -Breadcrumbs::for('sensors', function ($trail) { +Breadcrumbs::for('device', function ($trail, $deviceId) { $trail->parent('devices'); + $trail->push('Dispositivo ' . $deviceId, route('devices.show', ['userId' => $deviceId])); +}); +// Sensori +Breadcrumbs::for('sensors', function ($trail, $deviceId) { + $trail->parent('device'); $trail->push('Sensori', route('sensors.index')); }); - +// Utenti Breadcrumbs::for('users', function ($trail) { $trail->parent('home'); $trail->push('Gestione Utenti', route('users.index')); @@ -28,3 +33,5 @@ $trail->parent('users'); $trail->push('Utente ' . $userId, route('users.show', ['userId' => $userId])); }); + + diff --git a/routes/web.php b/routes/web.php index 176ff463..410a7113 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,7 +25,7 @@ Route::put('/settings', 'SettingsController@update')->name('settings.update'); //routes per gestione user -Route::get('/users', 'UserController@index')/*->middleware('can:isAdmin')*/->name('users.index'); // limita users agli admin +Route::get('/users', 'UserController@index')->name('users.index'); Route::get('/users/create', 'UserController@create')->name('users.create');//TODO Route::post('/users', 'UserController@store')->name('users.store'); Route::get('/users/{userId}', 'UserController@show')->name('users.show');//TODO @@ -40,13 +40,16 @@ //routes per gestione devices Route::get('/devices', 'DeviceController@index')->name('devices.index'); -Route::get('/device/{deviceId}', 'DeviceController@show')->name('devices.show');//TODO +Route::get('/devices/{deviceId}', 'DeviceController@show')->name('devices.show');//TODO -//routes per gestione devices -Route::get('/device/{deviceId}/sensors', 'SensorController@index')->name('sensors.index');//TODO -Route::get('/device/{deviceId}/sensor/{sensorId}', 'SensorController@show')->name('sensors.show');//TODO +//routes per gestione sensori +Route::get('/devices/{deviceId}/sensors', 'SensorController@index')->name('sensors.index');//TODO +Route::get('/devices/{deviceId}/sensor/{sensorId}', 'SensorController@show')->name('sensors.show');//TODO //routes per gestione entity Route::get('/entities', 'EntityController@index')->name('entities.index');//TODO Route::get('/entity/{entityId}', 'EntityController@show')->name('entities.show');//TODO + + +/*->middleware('can:isAdmin')*/// limita users agli admin From be6d4617296e19deda3c9e6e084a43f64fb1de35 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 16:59:37 +0100 Subject: [PATCH 03/44] fix CI --- app/Providers/EntityServiceProvider.php | 3 ++- routes/breadcrumbs.php | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Providers/EntityServiceProvider.php b/app/Providers/EntityServiceProvider.php index bd8ff645..85da1ca9 100644 --- a/app/Providers/EntityServiceProvider.php +++ b/app/Providers/EntityServiceProvider.php @@ -70,7 +70,8 @@ public function findAll() } } - public function retrieveByDevice($deviceId){ + public function retrieveByDevice($deviceId) + { try { $response = json_decode($this->request->get('entities', [ 'headers' => [ diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 0b62b4a5..b51f1dac 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -33,5 +33,3 @@ $trail->parent('users'); $trail->push('Utente ' . $userId, route('users.show', ['userId' => $userId])); }); - - From 162ef3d4712da2acc6c3ca366501e22c2f64f2e6 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 17:00:22 +0100 Subject: [PATCH 04/44] fix CI --- app/Providers/EntityServiceProvider.php | 3 ++- routes/breadcrumbs.php | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Providers/EntityServiceProvider.php b/app/Providers/EntityServiceProvider.php index bd8ff645..85da1ca9 100644 --- a/app/Providers/EntityServiceProvider.php +++ b/app/Providers/EntityServiceProvider.php @@ -70,7 +70,8 @@ public function findAll() } } - public function retrieveByDevice($deviceId){ + public function retrieveByDevice($deviceId) + { try { $response = json_decode($this->request->get('entities', [ 'headers' => [ diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 0b62b4a5..b51f1dac 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -33,5 +33,3 @@ $trail->parent('users'); $trail->push('Utente ' . $userId, route('users.show', ['userId' => $userId])); }); - - From fbf272038aa6496940ae243a30863ed757aba8d5 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 17:01:02 +0100 Subject: [PATCH 05/44] fix CI --- app/Providers/EntityServiceProvider.php | 3 ++- routes/breadcrumbs.php | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Providers/EntityServiceProvider.php b/app/Providers/EntityServiceProvider.php index bd8ff645..85da1ca9 100644 --- a/app/Providers/EntityServiceProvider.php +++ b/app/Providers/EntityServiceProvider.php @@ -70,7 +70,8 @@ public function findAll() } } - public function retrieveByDevice($deviceId){ + public function retrieveByDevice($deviceId) + { try { $response = json_decode($this->request->get('entities', [ 'headers' => [ diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 0b62b4a5..b51f1dac 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -33,5 +33,3 @@ $trail->parent('users'); $trail->push('Utente ' . $userId, route('users.show', ['userId' => $userId])); }); - - From ab476e5cd9156342760f22d83832bb9c494c3118 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 17:02:11 +0100 Subject: [PATCH 06/44] fix CI --- app/Providers/EntityServiceProvider.php | 3 ++- routes/breadcrumbs.php | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Providers/EntityServiceProvider.php b/app/Providers/EntityServiceProvider.php index bd8ff645..85da1ca9 100644 --- a/app/Providers/EntityServiceProvider.php +++ b/app/Providers/EntityServiceProvider.php @@ -70,7 +70,8 @@ public function findAll() } } - public function retrieveByDevice($deviceId){ + public function retrieveByDevice($deviceId) + { try { $response = json_decode($this->request->get('entities', [ 'headers' => [ diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php index 0b62b4a5..b51f1dac 100644 --- a/routes/breadcrumbs.php +++ b/routes/breadcrumbs.php @@ -33,5 +33,3 @@ $trail->parent('users'); $trail->push('Utente ' . $userId, route('users.show', ['userId' => $userId])); }); - - From 187dd33543e976724b35773a46aeb56de6e6be96 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 19:25:52 +0100 Subject: [PATCH 07/44] completamento dashboard --- app/Http/Controllers/DashboardController.php | 22 +++++++++++++++++- app/Http/Controllers/DeviceController.php | 2 +- app/Models/Device.php | 2 +- app/Providers/DeviceServiceProvider.php | 24 +++++++++++++++++++- app/Providers/EntityServiceProvider.php | 22 +++++++++++++++++- app/Providers/UserServiceProvider.php | 23 +++++++++++++++++++ resources/views/dashboard/index.blade.php | 18 +++++++-------- 7 files changed, 99 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 9f1e47df..2083b9a7 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -2,6 +2,9 @@ namespace App\Http\Controllers; +use App\Providers\DeviceServiceProvider; +use App\Providers\EntityServiceProvider; +use App\Providers\UserServiceProvider; use Illuminate\Support\Facades\Auth; class DashboardController extends Controller @@ -12,7 +15,24 @@ public function __construct() } public function index() { + $userProvider = new UserServiceProvider(); + $entityProvider = new EntityServiceProvider(); + $deviceProvider = new DeviceServiceProvider(); + $user = Auth::user(); - return view('dashboard.index', compact('user')); + $users = $userProvider->findAll();//utenti registrati + $entities = $entityProvider->findAll();//enti presenti + $devices = $deviceProvider->findAll();//dispositivi registrati + + $devicesEntity = $deviceProvider->findAllFromEntity($entityProvider->findFromUser($user->getAuthIdentifier())); + $usersEntity = $userProvider->findAllFromEntity($entityProvider->findFromUser($user->getAuthIdentifier())); + + $usersActive = array_filter($users, function ($u) { + return !$u->deleted; + }); + $usersActiveEntity = array_filter($usersEntity, function ($u) { + return !$u->deleted; + }); + return view('dashboard.index', compact(['user', 'users', 'entities', 'devices', 'devicesEntity', '$usersEntity', 'usersActive', 'usersActiveEntity'])); } } diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index c91539d1..3e32e313 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -50,7 +50,7 @@ public function index() */ public function show($device) { - //$device = $this->provider->retrieveById($device); + //$device = $this->provider->find($device); ///FAKER $user = new Device(); $arr = array_combine( diff --git a/app/Models/Device.php b/app/Models/Device.php index 45f74ddb..bf8a11a1 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -19,6 +19,6 @@ public function getSensors() public function getEntity() { $provider = new EntityServiceProvider(); - return $provider->retrieveByDevice($this->deviceId); + return $provider->findFromDevice($this->deviceId); } } diff --git a/app/Providers/DeviceServiceProvider.php b/app/Providers/DeviceServiceProvider.php index 4592ec91..43924082 100644 --- a/app/Providers/DeviceServiceProvider.php +++ b/app/Providers/DeviceServiceProvider.php @@ -27,7 +27,7 @@ public function __construct() * @param mixed $identifier * @return Device */ - public function retrieveById($identifier) + public function find($identifier) { try { $response = json_decode($this->request->get('device/' . $identifier, [ @@ -77,4 +77,26 @@ private function isExpired(RequestException $e) return redirect('login'); } } + + public function findAllFromEntity($entity) + { + try { + $response = json_decode($this->request->get('devices', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'query' => 'entityId=' . $entity + ])->getBody()); + $devices = []; + foreach ($response as $d) { + $device = new Device(); + $device->fill((array)$d); + $devices[] = $device; + } + return $devices; + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + } + } } diff --git a/app/Providers/EntityServiceProvider.php b/app/Providers/EntityServiceProvider.php index 85da1ca9..e2fdf83f 100644 --- a/app/Providers/EntityServiceProvider.php +++ b/app/Providers/EntityServiceProvider.php @@ -70,7 +70,7 @@ public function findAll() } } - public function retrieveByDevice($deviceId) + public function findFromDevice($deviceId) { try { $response = json_decode($this->request->get('entities', [ @@ -89,6 +89,26 @@ public function retrieveByDevice($deviceId) } } + public function findFromUser($userId) + { + try { + $response = json_decode($this->request->get('entities', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'query' => 'userId=' . $userId + ])->getBody()); + $entity = new Entity(); + $entity->fill((array)$response); + return $entity; + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + return null; + } + } + + private function isExpired(RequestException $e) { if ($e->getCode() == 419/*fai il controllo del token*/) { diff --git a/app/Providers/UserServiceProvider.php b/app/Providers/UserServiceProvider.php index 8839660d..d6c42105 100644 --- a/app/Providers/UserServiceProvider.php +++ b/app/Providers/UserServiceProvider.php @@ -39,6 +39,7 @@ public function retrieveById($identifier) 'Authorization' => 'Bearer ' . session()->get('token') ] ])->getBody()); + $user = new User(); $user->fill((array)$response); return $user; @@ -117,6 +118,28 @@ public function findAll() } } + public function findAllFromEntity($entityId) + { + try { + $response = json_decode($this->request->get('users', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ], + 'query' => 'entityId=' . $entityId + ])->getBody()); + $users = []; + foreach ($response as $u) { + $user = new User(); + $user->fill((array)$u); + $users[] = $user; + } + return $users; + } catch (RequestException $e) { + $this->isExpired($e); + abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + } + } + private function retriveByCode(Client $request, $credentials) { $response = json_decode($request->post('auth/tfa', [ diff --git a/resources/views/dashboard/index.blade.php b/resources/views/dashboard/index.blade.php index 0499897a..397ff90b 100644 --- a/resources/views/dashboard/index.blade.php +++ b/resources/views/dashboard/index.blade.php @@ -14,7 +14,7 @@
Utenti attivi
-
10
+
{{count($usersActive)}}
@@ -29,7 +29,7 @@
utenti attivi nel tuo ente
-
8
+
{{count($usersActiveEntity)}}
@@ -44,7 +44,7 @@
utenti registrati
-
15
+
{{count($users)}}
@@ -59,7 +59,7 @@
utenti registrati nel tuo ente
-
12
+
{{count($usersEntity)}}
@@ -76,7 +76,7 @@
dispositivi registrati
-
5
+
{{count($devices)}}
@@ -91,7 +91,7 @@
dispositivi del tuo ente
-
3
+
{{count($devicesEntity)}}
@@ -106,7 +106,7 @@
enti presenti
-
2
+
{{count($entities)}}
@@ -126,8 +126,8 @@
    -
  • Nome e Cognome: {{Auth::user()->name}} {{Auth::user()->surname}}
  • -
  • Indirizzo email: {{Auth::user()->email}}
  • +
  • Nome e Cognome: {{$user->name}} {{$user->surname}}
  • +
  • Indirizzo email: {{$user->email}}
  • Ente di appartenenza: NOME ENTE
  • Indirizzo IP:   {{ $_SERVER['REMOTE_ADDR'] }}
From 09558d4d162c4dff71768d6b8e7bbcbd252e1a0a Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Tue, 24 Mar 2020 19:31:18 +0100 Subject: [PATCH 08/44] fix CI --- app/Http/Controllers/DashboardController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 2083b9a7..8f0f9d3a 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -33,6 +33,8 @@ public function index() $usersActiveEntity = array_filter($usersEntity, function ($u) { return !$u->deleted; }); - return view('dashboard.index', compact(['user', 'users', 'entities', 'devices', 'devicesEntity', '$usersEntity', 'usersActive', 'usersActiveEntity'])); + return view('dashboard.index', compact([ + 'user', 'users', 'entities', 'devices', 'devicesEntity', '$usersEntity', 'usersActive', 'usersActiveEntity' + ])); } } From 5f8a9d36d101fc1450f5274e8c3beb299136f2b7 Mon Sep 17 00:00:00 2001 From: Giuseppe Vito Bitetti Date: Wed, 25 Mar 2020 11:35:46 +0100 Subject: [PATCH 09/44] Grafi e pullrate personalizzati --- __tests__/ChartManagement.test.js | 7 +-- app/Http/Controllers/SensorController.php | 17 ++++-- app/Providers/SensorServiceProvider.php | 39 +++++++++---- resources/js/components/ChartManagement.vue | 42 ++++++++------ resources/views/sensors/index.blade.php | 61 +++++++++++++++++++++ resources/views/sensors/show.blade.php | 20 +++++++ routes/breadcrumbs.php | 12 +++- routes/web.php | 4 +- 8 files changed, 161 insertions(+), 41 deletions(-) create mode 100644 resources/views/sensors/index.blade.php create mode 100644 resources/views/sensors/show.blade.php diff --git a/__tests__/ChartManagement.test.js b/__tests__/ChartManagement.test.js index 82e1acdc..8b194b5b 100644 --- a/__tests__/ChartManagement.test.js +++ b/__tests__/ChartManagement.test.js @@ -5,11 +5,10 @@ import ChartManagement from "../resources/js/components/ChartManagement.vue"; describe("ChartManagement", () => { test("is a Vue instance", () => { - const user = {}; - const device = {}; - const sensor = { sensorId: 1 }; + const deviceId = 1; + const sensorId = 1; const chart = mount(ChartManagement, { - propsData: { user, device, sensor }, + propsData: { deviceId, sensorId }, }); expect(chart.isVueInstance()).toBeTruthy(); }); diff --git a/app/Http/Controllers/SensorController.php b/app/Http/Controllers/SensorController.php index 1d9384b5..0b0d3adc 100644 --- a/app/Http/Controllers/SensorController.php +++ b/app/Http/Controllers/SensorController.php @@ -26,10 +26,10 @@ public function __construct() * * @return Factory|View */ - public function index() + public function index($device) { - $sensors = $this->provider->findAll(); - return view('sensors.index', compact('sensors')); + $sensors = $this->provider->findAllFromDevice($device); + return view('sensors.index', compact(['sensors', 'device'] )); } /** @@ -38,9 +38,14 @@ public function index() * @param $sensor * @return Factory|View */ - public function show($sensor) + public function show($device, $sensor) { - $sensor = $this->provider->retrieveById($sensor); - return view('sensors.show', compact('sensor')); + $sensor = $this->provider->find($device, $sensor); + return view('sensors.show', compact(['sensor', 'device'])); + } + + public function fetch($device, $sensor) + { + return $this->provider->fetch($device, $sensor); } } diff --git a/app/Providers/SensorServiceProvider.php b/app/Providers/SensorServiceProvider.php index db779d2d..f663ec1e 100644 --- a/app/Providers/SensorServiceProvider.php +++ b/app/Providers/SensorServiceProvider.php @@ -27,21 +27,23 @@ public function __construct() * @param mixed $identifier * @return Sensor */ - public function retrieveById($identifier) + public function find($deviceId ,$sensorId) { try { - $response = json_decode($this->request->get('sensor/' . $identifier, [ + $response = json_decode($this->request->get('device/' . $deviceId . '/sensor/' . $sensorId, [ 'headers' => [ 'Authorization' => 'Bearer ' . session()->get('token') - ] + ], ])->getBody()); $sensor = new Sensor(); $sensor->fill((array)$response); return $sensor; } catch (RequestException $e) { $this->isExpired($e); - abort($e->getCode(), $e->getResponse()->getReasonPhrase()); - return null; + //abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + $s = new Sensor(); + $s->fill(array_combine(['sensorId', 'type', 'deviceSensorId', 'deviceId'], [1,'boh', 1, 1])); + return $s;//null; } } @@ -73,11 +75,10 @@ public function findAll() public function findAllFromDevice($deviceId) { try { - $response = json_decode($this->request->get('sensors', [ + $response = json_decode($this->request->get('device/' . $deviceId . '/sensors', [ 'headers' => [ 'Authorization' => 'Bearer ' . session()->get('token') - ], - 'query' => 'deviceId=' . $deviceId + ] ])->getBody()); $sensors = []; foreach ($response as $d) { @@ -88,8 +89,12 @@ public function findAllFromDevice($deviceId) return $sensors; } catch (RequestException $e) { $this->isExpired($e); - abort($e->getCode(), $e->getResponse()->getReasonPhrase()); - return null; + //abort($e->getCode(), $e->getResponse()->getReasonPhrase()); + $s1 = new Sensor(); + $s2 = new Sensor(); + $s1->fill(array_combine(['sensorId', 'type', 'deviceSensorId', 'deviceId'], [1,'boh', 1, 1])); + $s2->fill(array_combine(['sensorId', 'type', 'deviceSensorId', 'deviceId'], [2,'buh', 2, 1])); + return [$s1, $s2];//null; } } @@ -101,4 +106,18 @@ private function isExpired(RequestException $e) return redirect('login'); } } + + public function fetch($device ,$sensorId) + { + try { + return json_decode($this->request->get('sensor', [ + 'headers' => [ + 'Authorization' => 'Bearer ' . session()->get('token') + ] + ])->getBody()); + } catch (RequestException $e) { + $this->isExpired($e); + return NAN; + } + } } diff --git a/resources/js/components/ChartManagement.vue b/resources/js/components/ChartManagement.vue index e1e5d620..c4e7a303 100644 --- a/resources/js/components/ChartManagement.vue +++ b/resources/js/components/ChartManagement.vue @@ -1,9 +1,16 @@