Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/publish events simple #181

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions laravel/app/Http/Controllers/DepartmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct()
{
$this->middleware('auth');
$this->middleware('bindings');
$this->middleware('published:department');
}

// Display list of departments in an event
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct()
{
$this->middleware('auth');
$this->middleware('bindings');
$this->middleware('published:event');
}

// Private function to manage file uploads
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Controllers/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct()
{
$this->middleware('auth');
$this->middleware('bindings');
$this->middleware('published:schedule');
}

// Helper function to convert form input into database-friendly information
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Controllers/ShiftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct()
{
$this->middleware('auth');
$this->middleware('bindings');
$this->middleware('published:shift');
}

// Display list of shifts in an event
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Controllers/SlotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct()
{
$this->middleware('auth');
$this->middleware('bindings');
$this->middleware('published:slot');
}

// Helper function to determine if an event has passed
Expand Down
1 change: 1 addition & 0 deletions laravel/app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ class Kernel extends HttpKernel
'admin' => \App\Http\Middleware\IsAdmin::class,
'lead' => \App\Http\Middleware\IsLead::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'published' => \App\Http\Middleware\CheckPublished::class,
];
}
61 changes: 61 additions & 0 deletions laravel/app/Http/Middleware/CheckPublished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use App\Models\Slot;

class CheckPublished
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;

/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $model_name)
{
$event = $request->{$model_name};
if($request->{$model_name} === 'department') {
$event = $request->{$model_name}->event;
}
if($request->{$model_name} === 'shift') {
$event = $request->{$model_name}->event;
}
if($request->{$model_name} === 'schedule') {
$event = $request->{$model_name}->shift->event;
}
if($request->{$model_name} === 'slot') {
$event = $request->{$model_name}->schedule->shift->event;
}

$is_published = ($event->published_at !== null);
$is_admin= $this->auth->user()->hasRole('admin');
Meleeman01 marked this conversation as resolved.
Show resolved Hide resolved
$is_department_lead = $this->auth->user()->hasRole('department-lead');
if(!$is_published && !$is_admin && !$is_department_lead)
{
return response('Unauthorized.', 401);
}
return $next($request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

class AddPublishedAtToEventsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('events', function (Blueprint $table) {
$table->timestamp('published_at');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('events', function (Blueprint $table) {
$table->drop('published_at');
});
}
}
20 changes: 20 additions & 0 deletions laravel/tests/Feature/CheckPublishedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CheckPublishedTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}