Skip to content

Commit

Permalink
feat: add expose_public to modules (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonostendorf authored Oct 15, 2024
1 parent ce3179a commit 0d202ec
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
28 changes: 28 additions & 0 deletions app/Http/Middleware/ActivePublicModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Middleware;

use App\Models\Module;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ActivePublicModule
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
*/
public function handle(Request $request, Closure $next, string $module): Response
{
if ($module && ! Module::where('key', $module)->first()->active) {
return redirect()->route('app.index');
}
if ($module && ! Module::where('key', $module)->first()->expose_public) {
return redirect()->route('app.index');
}

return $next($request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->boolean('expose_public')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('modules', function (Blueprint $table) {
$table->dropColumn(['expose_public']);
});
}
};
5 changes: 5 additions & 0 deletions database/seeders/ModuleDemoSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ public function run(): void
[
'key' => 'registration',
'active' => true,
'expose_public' => false,
],
[
'key' => 'randomGenerator',
'active' => true,
'expose_public' => false,
],
[
'key' => 'scoreSystem',
'active' => true,
'expose_public' => false,
],
[
'key' => 'countdown',
'active' => true,
'expose_public' => false,
],
];

Expand All @@ -40,6 +44,7 @@ public function run(): void

// update module
$existingModule->active = $module['active'];
$existingModule->expose_public = $module['expose_public'];

// save module
$existingModule->save();
Expand Down
4 changes: 3 additions & 1 deletion database/seeders/ModuleErstiwocheSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ public function run(): void
[
'key' => 'registration',
'active' => true,
'expose_public' => false,
],
];

foreach ($modules as $module) {
// check if module with key already exists
$existingModule = Module::where('key', $module['key'])->first();
if (! $existingModule) {
throw new \Exception('Module with key "'.$module['key'].'" not found.');
throw new \Exception('Module with key "' . $module['key'] . '" not found.');
}

// update module
$existingModule->active = $module['active'];
$existingModule->expose_public = $module['expose_public'];

// save module
$existingModule->save();
Expand Down
4 changes: 4 additions & 0 deletions database/seeders/ModuleGerolsteinSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ public function run(): void
[
'key' => 'randomGenerator',
'active' => true,
'expose_public' => true,
],
[
'key' => 'scoreSystem',
'active' => true,
'expose_public' => true,
],
[
'key' => 'countdown',
'active' => true,
'expose_public' => true,
],
];

Expand All @@ -36,6 +39,7 @@ public function run(): void

// update module
$existingModule->active = $module['active'];
$existingModule->expose_public = $module['expose_public'];

// save module
$existingModule->save();
Expand Down
5 changes: 5 additions & 0 deletions database/seeders/ModuleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ public function run(): void
[
'key' => 'registration',
'active' => false,
'expose_public' => false,
],
[
'key' => 'randomGenerator',
'active' => false,
'expose_public' => false,
],
[
'key' => 'scoreSystem',
'active' => false,
'expose_public' => false,
],
[
'key' => 'countdown',
'active' => false,
'expose_public' => false,
],
];

Expand All @@ -42,6 +46,7 @@ public function run(): void
$newModule = new Module;
$newModule->key = $module['key'];
$newModule->active = $module['active'];
$newModule->expose_public = $module['expose_public'];

// save module
$newModule->save();
Expand Down
15 changes: 15 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Http\Controllers\DashboardEventController;
use App\Http\Controllers\DashboardTutorController;
use App\Http\Middleware\ActiveModule;
use App\Http\Middleware\ActivePublicModule;
use App\Http\Middleware\IsLoggedInTutor;
use App\Http\Middleware\RedirectIfTutor;
use Illuminate\Auth\Middleware\Authenticate;
Expand Down Expand Up @@ -107,6 +108,20 @@
Route::get('{slug?}', [DashboardController::class, 'cmsPage'])->where('slug', '.*');
});

//public routes without authentication
Route::prefix('public')->group(function () {
Route::middleware(ActivePublicModule::class . ':randomGenerator')->group(function () {
Route::get('/random-generator', [DashboardAdminRandomGeneratorController::class, 'display'])->name('dashboard.admin.randomGenerator.display');
});

Route::middleware(ActivePublicModule::class . ':scoreSystem')->group(function () {
Route::get('/score-system', [DashboardAdminScoreSystemController::class, 'display'])->name('dashboard.admin.scoreSystem.display');
});

Route::middleware(ActivePublicModule::class . ':countdown')->group(function () {
Route::get('/countdown', [DashboardAdminCountdownController::class, 'display'])->name('dashboard.admin.countdown.display');
});
});
// api routes with authentication
Route::prefix('api')->middleware(Authenticate::class)->group(function () {
Route::middleware(IsLoggedInTutor::class)->group(function () {
Expand Down

0 comments on commit 0d202ec

Please sign in to comment.