Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/Http/Controllers/ApplicationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Classes\Repositories\ApplicationRepositoryInterface;
use App\Classes\Transformers\ApplicationTransformer;
use App\Http\Requests\UpsertApplicationRulesRequest;
use App\Models\Application;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -494,4 +496,29 @@ public function deactivate($id)

return response()->json($response->toArray(), 200);
}

/**
* Bulk replace rules for all applications belonging to a tenant user.
*
* @param UpsertApplicationRulesRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function upsertRules(UpsertApplicationRulesRequest $request)
{
$validated = $request->validated();

$updated = Application::query()
->where('tenant_user_id', $validated['tenant_user_id'])
->update([
'rules' => $validated['rules'],
'updated_at' => now(),
]);

return response()->json([
'status' => 200,
'message' => 'Application rules updated successfully.',
'tenant_user_id' => $validated['tenant_user_id'],
'updated_count' => $updated,
], 200);
}
}
38 changes: 27 additions & 11 deletions app/Http/Middleware/ApiAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@
use App\Models\UsageLog;
use Carbon\Carbon;
use Closure;
use Illuminate\Support\Facades\Log;

class ApiAuthMiddleware extends BasicAuthMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$apiKey = $request->header('x-api-key');
$authHeader = $request->header('Authorization');
$isBasicAuth = $authHeader && str_starts_with($authHeader, 'Basic ');

if (!$apiKey && !$isBasicAuth) {
if (!$apiKey && !$isBasicAuth) {
return response()->json(['error' => 'Authentication required. Provide API key or Basic auth'], 401);
}

Expand All @@ -31,15 +25,24 @@ public function handle($request, Closure $next)
}

if ($apiKey) {
$application = Application::where('key', '=', $apiKey)->first();
$application = Application::query()->where('key', '=', $apiKey)->first();

if (!$application) {

return response()->json(['error' => 'Invalid API key'], 401);
}

if (!$application->is_active) {
return response()->json(['error' => 'Application is inactive'], 403);

if ($application->trashed() || !$application->is_active) {
return response()->json(['error' => 'Application is unavailable'], 403);
}

$canAccess = $this->canAccessRequestedVersion($request->path(), (array) $application->rules);

if (!$canAccess) {
return response()->json(['error' => 'Application is not allowed to access this API version'], 403);
}

$usageLog = new UsageLog;
$usageLog->application_id = $application->id;
$usageLog->method = $request->method();
Expand All @@ -55,4 +58,17 @@ public function handle($request, Closure $next)

return $next($request);
}

private function canAccessRequestedVersion(string $path, array $rules): bool
{
if (strpos($path, 'v1/') === 0) {
return $rules['can_access_legacy_whatnow'];
}

if (strpos($path, 'v2/') === 0) {
return $rules['can_access_preparedness_v2'];
}

return true;
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/UpsertApplicationRulesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpsertApplicationRulesRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'tenant_user_id' => 'required|string|max:255',
'rules' => 'required|array',
];
}
}

2 changes: 2 additions & 0 deletions app/Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Application extends Model
'estimated_users_count',
'key',
'is_active',
'rules',
];

protected $dates = ['deleted_at'];
Expand All @@ -42,6 +43,7 @@ class Application extends Model
*/
protected $casts = [
'is_active' => 'boolean',
'rules' => 'array',
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddRulesToApplicationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('applications', function (Blueprint $table) {
$table->json('rules')->nullable()->after('is_active');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropColumn('rules');
});
}
}

1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
// Route::delete('org/{code}/image', 'OrganisationController@deleteImageById');

// "Applications" endpoints
Route::post('applications/rules', 'ApplicationController@upsertRules');
Route::get('apps', 'ApplicationController@getAllForUser');
Route::post('apps', 'ApplicationController@create');
Route::get('apps/{id}', 'ApplicationController@getById');
Expand Down
Loading