diff --git a/sourcecode/hub/.env.example b/sourcecode/hub/.env.example index 0bc100735..30305c8c0 100644 --- a/sourcecode/hub/.env.example +++ b/sourcecode/hub/.env.example @@ -64,6 +64,9 @@ AUTH0_CLIENT_SECRET= # e.g. https://dev--abcdef.eu.auth0.com - must not end in a slash AUTH0_BASE_URL= +# Should only be used for legacy environments +EDLIB_LEGACY_DOMAIN=www.edlib.test + # These should not be used, except in development and some legacy environments NDLA_LEGACY_DOMAIN=hub-ndla-legacy.edlib.test NDLA_LEGACY_CONTENTAUTHOR_HOST=ca.edlib.test diff --git a/sourcecode/hub/app/Http/Controllers/ContentController.php b/sourcecode/hub/app/Http/Controllers/ContentController.php index e139139c6..c8fb5c189 100644 --- a/sourcecode/hub/app/Http/Controllers/ContentController.php +++ b/sourcecode/hub/app/Http/Controllers/ContentController.php @@ -463,4 +463,9 @@ public function statistics(ContentStatisticsRequest $request, Content $content): ], ]); } + + public function redirectFromEdlib2Id(Content $edlib2Content): RedirectResponse + { + return redirect()->route('content.embed', [$edlib2Content]); + } } diff --git a/sourcecode/hub/app/Providers/RouteServiceProvider.php b/sourcecode/hub/app/Providers/RouteServiceProvider.php index a969a49e9..d9453a4f7 100644 --- a/sourcecode/hub/app/Providers/RouteServiceProvider.php +++ b/sourcecode/hub/app/Providers/RouteServiceProvider.php @@ -51,6 +51,11 @@ public function boot(): void ])->limit(1)->firstOrFail(); }); + Route::bind('edlib2Content', fn(string $value) => Content::ofTag([ + 'prefix' => 'edlib2_id', + 'name' => $value, + ])->limit(1)->firstOrFail()); + $this->configureRateLimiting(); $this->routes(function (NdlaLegacyConfig $ndlaLegacy) { @@ -63,6 +68,10 @@ public function boot(): void Route::middleware('stateless') ->get('/up', HealthController::class); + Route::middleware('stateless') + ->domain(config('app.edlib-legacy-domain', 'invalid.')) + ->group(base_path('routes/edlib-legacy.php')); + Route::middleware('ndla-legacy') ->domain($ndlaLegacy->isEnabled() ? $ndlaLegacy->getDomain() : 'invalid.') ->group(base_path('routes/ndla-legacy.php')); diff --git a/sourcecode/hub/config/app.php b/sourcecode/hub/config/app.php index e97153ed8..b7e471749 100644 --- a/sourcecode/hub/config/app.php +++ b/sourcecode/hub/config/app.php @@ -21,6 +21,8 @@ 'contact-url' => env('APP_CONTACT_URL', ''), + 'edlib-legacy-domain' => env('EDLIB_LEGACY_DOMAIN'), + /* |-------------------------------------------------------------------------- | Application Environment diff --git a/sourcecode/hub/phpunit.xml b/sourcecode/hub/phpunit.xml index 239429375..cbe09539f 100644 --- a/sourcecode/hub/phpunit.xml +++ b/sourcecode/hub/phpunit.xml @@ -26,6 +26,7 @@ + diff --git a/sourcecode/hub/routes/edlib-legacy.php b/sourcecode/hub/routes/edlib-legacy.php new file mode 100644 index 000000000..69249b923 --- /dev/null +++ b/sourcecode/hub/routes/edlib-legacy.php @@ -0,0 +1,10 @@ +uses([ContentController::class, 'redirectFromEdlib2Id']) + ->whereUuid('edlib2Content'); diff --git a/sourcecode/hub/tests/Feature/ContentTest.php b/sourcecode/hub/tests/Feature/ContentTest.php index e41f069fb..066cc6e0b 100644 --- a/sourcecode/hub/tests/Feature/ContentTest.php +++ b/sourcecode/hub/tests/Feature/ContentTest.php @@ -84,6 +84,17 @@ public function testCanLaunchContentByEdlib2UsageId(): void ->assertRedirect('https://hub-test.edlib.test/content/' . $content->id . '/embed'); } + public function testRedirectsFromLegacyResourceUrls(): void + { + $content = Content::factory() + ->withPublishedVersion() + ->tag('edlib2_id:a3dcbd28-bf37-4123-ac5e-ba2f72a8f420') + ->create(); + + $this->get('http://www.edlib.test/s/resources/a3dcbd28-bf37-4123-ac5e-ba2f72a8f420') + ->assertRedirect('http://hub-test.edlib.test/content/' . $content->id . '/embed'); + } + public function testCannotPreviewUnpublishedContent(): void { $content = Content::factory()