Api MIse au propre
This commit is contained in:
parent
4419445105
commit
1d983f3ff4
12 changed files with 170 additions and 62 deletions
|
|
@ -3,30 +3,21 @@
|
|||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\V2\LDCNetworkResource;
|
||||
use App\Models\LDCNetwork;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LDCNetworksController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
/**
|
||||
* Api LDC Networks
|
||||
*
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$token = $request->token;
|
||||
|
||||
if ($token !== 'CE8S52Qq7stf5k8frF4T') {
|
||||
return response('Unauthorized', 401);
|
||||
}
|
||||
|
||||
$ips = [];
|
||||
|
||||
$networks = LDCNetwork::all();
|
||||
foreach ($networks as $network) {
|
||||
$infos = explode('.', $network->first_ip);
|
||||
$min = $infos[3];
|
||||
$max = explode('.', $network->last_ip)[3];
|
||||
for ($i = $min; $i <= $max; $i++) {
|
||||
$ips[] = "$infos[0].$infos[1].$infos[2].$i";
|
||||
}
|
||||
}
|
||||
return response($ips);
|
||||
return \response()->json(new LDCNetworkResource($networks));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,46 +3,24 @@
|
|||
namespace App\Http\Controllers\API;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Project;
|
||||
use App\Http\Requests\Api\UpdateProjectStatusRequest;
|
||||
use App\Models\ProjectsStatusChecker;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class StatusCheckerController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @param \App\Http\Requests\Api\UpdateProjectStatusRequest $request
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
|
||||
*/
|
||||
public function index(UpdateProjectStatusRequest $request)
|
||||
{
|
||||
$token = $request->token;
|
||||
$datas = $request->datas;
|
||||
/** @var \App\Models\ProjectsStatusChecker */
|
||||
$project_sc = ProjectsStatusChecker::where('project_uuid', '=', $request->uuid)
|
||||
->firstOrNew();
|
||||
|
||||
if ($token !== 'CE8S52Qq7stf5k8frF4T') {
|
||||
return response('Unauthorized', 401);
|
||||
}
|
||||
|
||||
$oDatas = json_decode($datas);
|
||||
|
||||
$app_uuid = $oDatas->uuid;
|
||||
$sitemap = $oDatas->sitemap;
|
||||
$robots = $oDatas->robots;
|
||||
$code = json_encode($oDatas->code);
|
||||
$logs = $oDatas->logs;
|
||||
|
||||
$project_sc = ProjectsStatusChecker::where('project_uuid', '=', $app_uuid)->first();
|
||||
if (!$project_sc) {
|
||||
$project = Project::where('uuid', '=', $app_uuid)->first();
|
||||
if (!$project) {
|
||||
return response('Unknown', 404);
|
||||
}
|
||||
$project_sc = new ProjectsStatusChecker();
|
||||
$project_sc->project_uuid = $app_uuid;
|
||||
}
|
||||
|
||||
$project_sc->code = $code;
|
||||
$project_sc->sitemap = $sitemap;
|
||||
$project_sc->robots = $robots;
|
||||
$project_sc->logs = $logs;
|
||||
$project_sc->updated_at = NOW();
|
||||
$project_sc->save();
|
||||
|
||||
return response('OK');
|
||||
$project_sc->fill($request->validated());
|
||||
return $project_sc->save() ? response('OK') : response('KO');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ class Kernel extends HttpKernel
|
|||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
'htmlsanitize' => \App\Http\Middleware\HtmlSanitizer::class,
|
||||
'apiCheck' => \App\Http\Middleware\ApiCheckCredentialMiddleware::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
23
app/Http/Middleware/ApiCheckCredentialMiddleware.php
Normal file
23
app/Http/Middleware/ApiCheckCredentialMiddleware.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
class ApiCheckCredentialMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param mixed $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
if ($request->token !== \config('api.key')) {
|
||||
return response('Unauthorized', 401);
|
||||
}
|
||||
|
||||
/** @var \Illuminate\Http\Request $request */
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,11 +10,12 @@ class Authenticate extends Middleware
|
|||
/**
|
||||
* Get the path the user should be redirected to when they are not authenticated.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param mixed $request
|
||||
* @return string|null
|
||||
*/
|
||||
protected function redirectTo($request)
|
||||
{
|
||||
/** @var \Illuminate\Http\Request $request */
|
||||
if (!$request->expectsJson()) {
|
||||
session(['redirectURL' => URL::full()]);
|
||||
return route('saml2_login', 'ldc');
|
||||
|
|
|
|||
|
|
@ -9,14 +9,19 @@ class LDCNetwork
|
|||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @param mixed $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// IpUtils::checkIp();
|
||||
// https://github.com/symfony/http-foundation/blob/master/IpUtils.php#L36
|
||||
/**
|
||||
* IpUtils::checkIp();
|
||||
*/
|
||||
|
||||
// * https://github.com/symfony/http-foundation/blob/master/IpUtils.php#L36 .
|
||||
|
||||
/** @var \Illuminate\Http\Request $request */
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace App\Http\Middleware;
|
|||
use Aacotroneo\Saml2\Saml2Auth;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class RedirectIfAuthenticated
|
||||
|
|
@ -14,14 +15,13 @@ class RedirectIfAuthenticated
|
|||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @param string|null $guard
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next, $guard = null)
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
if ($request->ajax()) {
|
||||
// Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login' .
|
||||
return response('Unauthorized.', 401);
|
||||
// Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'
|
||||
} else {
|
||||
$saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('ldc'));
|
||||
session(['redirectURL' => URL::full()]);
|
||||
|
|
|
|||
51
app/Http/Requests/Api/UpdateProjectStatusRequest.php
Normal file
51
app/Http/Requests/Api/UpdateProjectStatusRequest.php
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateProjectStatusRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return $this->token === \config('api.key');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data for validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function prepareForValidation()
|
||||
{
|
||||
$data = \json_decode($this->datas, true);
|
||||
$this->merge([
|
||||
'project_uuid' => collect($data)->get('uuid'),
|
||||
'sitemap' => collect($data)->get('sitemap'),
|
||||
'robots' => collect($data)->get('robots'),
|
||||
'code' => collect($data)->get('code'),
|
||||
'logs' => collect($data)->get('logs')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'project_uuid' => 'required|exists:projects,uuid',
|
||||
'sitemap' => 'required|boolean',
|
||||
'robots' => 'required|boolean',
|
||||
'code' => 'required|json',
|
||||
'logs' => 'required|numeric',
|
||||
];
|
||||
}
|
||||
}
|
||||
31
app/Http/Resources/LDCNetworkResource.php
Normal file
31
app/Http/Resources/LDCNetworkResource.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\V2;
|
||||
|
||||
use App\Models\LDCNetwork;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection as JsonResourceCollection;
|
||||
|
||||
class LDCNetworkResource extends JsonResourceCollection
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param mixed $request
|
||||
* @return array
|
||||
*/
|
||||
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass, Squiz.Commenting.FunctionComment.Missing
|
||||
public function toArray($request)
|
||||
{
|
||||
return $this->collection->map(function (LDCNetwork $network) {
|
||||
$infos = collect(explode('.', strval($network->first_ip)));
|
||||
$min = intval($infos->get(3));
|
||||
$max = collect(explode('.', strval($network->last_ip)))->get(3);
|
||||
for ($i = $min; $i <= $max; $i++) {
|
||||
$ips[] = $infos->get(0) . '.' .
|
||||
$infos->get(1) . '.' .
|
||||
$infos->get(2) . ".$i";
|
||||
}
|
||||
return $ips;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -12,4 +12,24 @@ class ProjectsStatusChecker extends Model
|
|||
* @var string
|
||||
*/
|
||||
protected $table = 'projects_status_checker';
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'code' => 'array',
|
||||
'sitemap' => 'boolean',
|
||||
'robots' => 'boolean'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'project_uuid', 'code', 'sitemap', 'robots'
|
||||
];
|
||||
}
|
||||
|
|
|
|||
5
config/api.php
Normal file
5
config/api.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'key' => 'CE8S52Qq7stf5k8frF4T'
|
||||
];
|
||||
|
|
@ -3,5 +3,7 @@
|
|||
use App\Http\Controllers\API\StatusCheckerController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::post('/status-checker', [StatusCheckerController::class, 'index']);
|
||||
Route::get('/ldc-networks', [StatusCheckerController::class, 'index']);
|
||||
Route::middleware('apiCheck')->group(function () {
|
||||
Route::post('/status-checker', [StatusCheckerController::class, 'index']);
|
||||
Route::get('/ldc-networks', [LDCNetworksController::class, 'index']);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue