2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Brand;
|
|
|
|
|
use App\Models\Service;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class ServiceController extends Controller
|
|
|
|
|
{
|
2021-02-28 12:04:49 +01:00
|
|
|
public function index()
|
|
|
|
|
{
|
2020-12-01 17:02:10 +01:00
|
|
|
$services = Service::all();
|
|
|
|
|
return view('services.index', compact('services'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
public function manage(Request $request, $id = null)
|
|
|
|
|
{
|
2020-12-01 17:02:10 +01:00
|
|
|
$service = null;
|
|
|
|
|
$brands = Brand::all();
|
|
|
|
|
$edit = false;
|
|
|
|
|
|
|
|
|
|
if ($id !== null) {
|
|
|
|
|
$service = Service::find($id);
|
|
|
|
|
$edit = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
|
$request->validate([
|
|
|
|
|
'name' => 'required|max:255',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$service) {
|
|
|
|
|
$service = new Service();
|
|
|
|
|
$apiToken = Str::random(80);
|
|
|
|
|
$service->api_token = hash('sha256', $apiToken);
|
|
|
|
|
$service->api_token_date = now();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$service->name = filter_var($request->name, FILTER_SANITIZE_STRING);
|
|
|
|
|
$service->brand_id = filter_var($request->brand_id, FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
|
$service->save();
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
return redirect()->route('services')
|
|
|
|
|
->with('success', $edit ? 'Modifications enregistrées !' : 'Token du service : ' . $apiToken);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('services.manage', compact('service', 'brands', 'edit'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
public function generateApiToken($serviceId)
|
|
|
|
|
{
|
2020-12-01 17:02:10 +01:00
|
|
|
$service = Service::find($serviceId);
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
if (!$service) {
|
2020-12-01 17:02:10 +01:00
|
|
|
return back()->with('error', "Génération impossible : Service introuvable.");
|
2021-02-28 12:04:49 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
|
|
|
|
|
$apiToken = Str::random(80);
|
|
|
|
|
$service->api_token = hash('sha256', $apiToken);
|
|
|
|
|
$service->api_token_date = now();
|
|
|
|
|
|
|
|
|
|
$service->save();
|
|
|
|
|
|
2020-12-02 18:29:16 +01:00
|
|
|
return back()->with('success', "Nouveau token d'API de " . $service->name . " : " . $apiToken);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
}
|