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

Issue 101 permissions for clubs #121

Merged
merged 3 commits into from
Jun 7, 2020
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
5 changes: 5 additions & 0 deletions app/Http/Controllers/ClubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class ClubController extends Controller
{
public function __construct()
{
$this->authorizeResource(Club::class, 'club');
}

public function index(): View
{
return view('CRUD.clubs.index', ['clubs' => Club::orderBy('name')->paginate(15)]);
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ class TeamController extends Controller
{
public function index(Club $club): View
{
$this->authorize('viewAny', [Team::class, $club]);

$teams = Team::query()->inClub($club)->orderByName()->get();

return view('CRUD.teams.index', compact('club', 'teams'));
}

public function create(Club $club): View
{
$this->authorize('create', [Team::class, $club]);

$venues = Venue::all();

return view('CRUD.teams.create', compact('club', 'venues'));
}

public function store(Request $request, Club $club): RedirectResponse
{
$this->authorize('create', [Team::class, $club]);

$this->validate($request,
[
'club_id' => 'required|exists:clubs,id',
Expand All @@ -55,13 +61,17 @@ public function store(Request $request, Club $club): RedirectResponse

public function edit(Club $club, Team $team): View
{
$this->authorize('update', $team);

$venues = Venue::all();

return view('CRUD.teams.edit', compact('club', 'team', 'venues'));
}

public function update(Request $request, Club $club, Team $team): RedirectResponse
{
$this->authorize('update', $team);

$this->validate($request,
[
'name' => [
Expand Down Expand Up @@ -89,6 +99,8 @@ public function update(Request $request, Club $club, Team $team): RedirectRespon

public function destroy(Club $club, Team $team): RedirectResponse
{
$this->authorize('delete', $team);

$team->delete();

return redirect()
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/VenueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

class VenueController extends Controller
{
public function __construct()
{
$this->authorizeResource(Venue::class, 'venue');
}

public function index(): View
{
return view('CRUD.venues.index', ['venues' => Venue::paginate(15)]);
Expand Down
8 changes: 6 additions & 2 deletions app/Policies/CheckRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ public function hasAnyClubSecretaryRole(User $user): bool
})->toArray());
}

public function hasAnyTeamSecretaryRole(User $user): bool
public function hasAnyTeamSecretaryRole(User $user, ?Club $club = null): bool
{
return $user->hasRole(Team::all()->map(function (Team $team): string {
$teams = $club
? $club->getTeams()
: Team::all();

return $user->hasRole($teams->map(function (Team $team): string {
return RolesHelper::teamSecretaryName($team);
})->toArray());
}
Expand Down
35 changes: 35 additions & 0 deletions app/Policies/ClubPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Policies;

use App\Helpers\RolesHelper;
use App\Models\Club;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class ClubPolicy
{
use HandlesAuthorization, CheckRoles;

public function viewAny(User $user): bool
{
return $this->hasAnyClubSecretaryRole($user);
}

public function create(User $user): bool
{
// No-one but Site Administrator can create a new season
return false;
}

public function update(User $user, Club $club): bool
{
return $user->hasRole(RolesHelper::clubSecretaryName($club));
}

public function delete(User $user, Club $club): bool
{
// No-one but Site Administrator can create a new season
return false;
}
}
41 changes: 41 additions & 0 deletions app/Policies/TeamPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Policies;

use App\Helpers\RolesHelper;
use App\Models\Club;
use App\Models\Team;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class TeamPolicy
{
use HandlesAuthorization, CheckRoles;

public function viewAny(User $user, Club $club): bool
{
if ($user->hasRole(RolesHelper::clubSecretaryName($club))) {
return true;
}

return $this->hasAnyTeamSecretaryRole($user, $club);
}

public function create(User $user, Club $club): bool
{
return $user->hasRole(RolesHelper::clubSecretaryName($club));
}

public function update(User $user, Team $team): bool
{
return $user->hasAnyRole(
RolesHelper::clubSecretaryName($team->getClub()),
RolesHelper::teamSecretaryName($team)
);
}

public function delete(User $user, Team $team): bool
{
return $user->hasRole(RolesHelper::clubSecretaryName($team->getClub()));
}
}
38 changes: 38 additions & 0 deletions app/Policies/VenuePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Policies;

use App\Helpers\RolesHelper;
use App\Models\Club;
use App\Models\User;
use App\Models\Venue;
use Illuminate\Auth\Access\HandlesAuthorization;

class VenuePolicy
{
use HandlesAuthorization, CheckRoles;

public function viewAny(User $user): bool
{
// No-one but Site Administrator can create a new season
return false;
}

public function create(User $user): bool
{
// No-one but Site Administrator can create a new season
return false;
}

public function update(User $user, Venue $venue): bool
{
// No-one but Site Administrator can create a new season
return false;
}

public function delete(User $user, Venue $venue): bool
{
// No-one but Site Administrator can create a new season
return false;
}
}
9 changes: 9 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace App\Providers;

use App\Models\Club;
use App\Models\Competition;
use App\Models\Division;
use App\Models\Fixture;
use App\Models\Season;
use App\Models\Team;
use App\Models\Venue;
use App\Policies\ClubPolicy;
use App\Policies\CompetitionPolicy;
use App\Policies\DivisionPolicy;
use App\Policies\FixturePolicy;
use App\Policies\SeasonPolicy;
use App\Policies\TeamPolicy;
use App\Policies\VenuePolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;
Expand All @@ -26,6 +32,9 @@ class AuthServiceProvider extends ServiceProvider
Competition::class => CompetitionPolicy::class,
Division::class => DivisionPolicy::class,
Fixture::class => FixturePolicy::class,
Club::class => ClubPolicy::class,
Team::class => TeamPolicy::class,
Venue::class => VenuePolicy::class,
];

/**
Expand Down
25 changes: 16 additions & 9 deletions database/seeds/UsersTableSeeder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Helpers\RolesHelper;
use App\Models\Club;
use App\Models\Competition;
use App\Models\Division;
Expand All @@ -15,7 +16,13 @@ class UsersTableSeeder extends Seeder

public function run(): void
{
$this->initProgressBar(Role::count());
$this->initProgressBar(Role::count() + 1);

factory(User::class)->create([
'name' => "Test User",
'email' => "[email protected]",
]);
$this->advanceProgressBar();

$user = factory(User::class)->create([
'name' => "Site Administrator",
Expand All @@ -25,34 +32,34 @@ public function run(): void
$this->advanceProgressBar();

$user = factory(User::class)->create([
'name' => "Referee Administrator",
'email' => "referee[email protected]",
'name' => "Referees Administrator",
'email' => "referees[email protected]",
]);
$user->assignRole("Referee Administrator");
$user->assignRole("Referees Administrator");
$this->advanceProgressBar();

Season::all()->each(function (Season $season) {
$user = factory(User::class)->create([
'name' => "Season {$season->getId()} Administrator",
'email' => "season-{$season->getId()}[email protected]",
]);
$user->assignRole("Season {$season->getId()} Administrator");
$user->assignRole(RolesHelper::seasonAdminName($season));
$this->advanceProgressBar();
});
Competition::all()->each(function (Competition $competition) {
$user = factory(User::class)->create([
'name' => "Competition {$competition->getId()} Administrator",
'email' => "competition-{$competition->getId()}[email protected]",
]);
$user->assignRole("Competition {$competition->getId()} Administrator");
$user->assignRole(RolesHelper::competitionAdminName($competition));
$this->advanceProgressBar();
});
Division::all()->each(function (Division $division) {
$user = factory(User::class)->create([
'name' => "Division {$division->getId()} Administrator",
'email' => "division-{$division->getId()}[email protected]",
]);
$user->assignRole("Division {$division->getId()} Administrator");
$user->assignRole(RolesHelper::divisionAdminName($division));
$this->advanceProgressBar();
});

Expand All @@ -61,15 +68,15 @@ public function run(): void
'name' => "Club {$club->getId()} Secretary",
'email' => "club-{$club->getId()}[email protected]",
]);
$user->assignRole("Club {$club->getId()} Secretary");
$user->assignRole(RolesHelper::clubSecretaryName($club));
$this->advanceProgressBar();
});
Team::all()->each(function (Team $team) {
$user = factory(User::class)->create([
'name' => "Team {$team->getId()} Secretary",
'email' => "team-{$team->getId()}[email protected]",
]);
$user->assignRole("Team {$team->getId()} Secretary");
$user->assignRole(RolesHelper::teamSecretaryName($team));
$this->advanceProgressBar();
});

Expand Down
11 changes: 4 additions & 7 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
Route::get('fixtures')
->uses('FixturesController@index')
->name('fixtures.index');
Route::middleware(['can:view-seasons'])
Route::resource('clubs', 'ClubController')->except('show');
Route::prefix('clubs/{club}')
->group(function (): void {
Route::resource('clubs', 'ClubController')->except('show');
Route::prefix('clubs/{club}')
->group(function (): void {
Route::resource('teams', 'TeamController')->except('show');
});
Route::resource('venues', 'VenueController');
Route::resource('teams', 'TeamController')->except('show');
});
Route::resource('venues', 'VenueController');
});
10 changes: 5 additions & 5 deletions tests/Browser/CRUD/ClubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function setUp(): void
public function testListingAllClubs(): void
{
$this->browse(function (Browser $browser): void {
$browser->loginAs(factory(User::class)->create()->givePermissionTo('view-seasons'));
$browser->loginAs($this->siteAdmin);

$browser->visit('/clubs')
->assertSeeIn('@list', 'There are no clubs yet.');
Expand Down Expand Up @@ -77,7 +77,7 @@ public function testListingAllClubs(): void
public function testAddingAClub(): void
{
$this->browse(function (Browser $browser): void {
$browser->loginAs(factory(User::class)->create()->givePermissionTo('view-seasons'));
$browser->loginAs($this->siteAdmin);

// Check we can add a club from the landing page
$browser->visit('/clubs')
Expand Down Expand Up @@ -137,7 +137,7 @@ public function testAddingAClub(): void
public function testEditingAClub(): void
{
$this->browse(function (Browser $browser): void {
$browser->loginAs(factory(User::class)->create()->givePermissionTo('view-seasons'));
$browser->loginAs($this->siteAdmin);

$browser->visit("/clubs/1/edit")
->assertTitle('Not Found')
Expand Down Expand Up @@ -212,7 +212,7 @@ public function testEditingAClub(): void
public function testDeletingAClub(): void
{
$this->browse(function (Browser $browser): void {
$browser->loginAs(factory(User::class)->create()->givePermissionTo('view-seasons'));
$browser->loginAs($this->siteAdmin);

$clubId = aClub()->build()->getId();

Expand Down Expand Up @@ -248,7 +248,7 @@ public function testDeletingAClub(): void
public function testViewingTheClubTeams(): void
{
$this->browse(function (Browser $browser): void {
$browser->loginAs(factory(User::class)->create()->givePermissionTo('view-seasons'));
$browser->loginAs($this->siteAdmin);

/** @var Club $club */
$club = aClub()->build();
Expand Down
Loading