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

Implement TeamPolicy::create authorization check before creating a team. #82

Merged
merged 1 commit into from
Sep 8, 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
4 changes: 4 additions & 0 deletions src/Http/Middleware/ShareInertiaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Jetstream\Http\Middleware;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
Expand Down Expand Up @@ -42,6 +43,9 @@ public function handle($request, $next)
'all_teams' => Jetstream::hasTeamFeatures() ? $request->user()->allTeams() : null,
]), [
'two_factor_enabled' => ! is_null($request->user()->two_factor_secret),
'can' => [
'create_team' => Jetstream::hasTeamFeatures() && Gate::forUser($user)->authorize('create', Jetstream::newTeamModel()),
],
]);
},
'errorBags' => function () {
Expand Down
4 changes: 4 additions & 0 deletions stubs/app/Actions/Jetstream/CreateTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Actions\Jetstream;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Laravel\Jetstream\Contracts\CreatesTeams;
use Laravel\Jetstream\Jetstream;

class CreateTeam implements CreatesTeams
{
Expand All @@ -16,6 +18,8 @@ class CreateTeam implements CreatesTeams
*/
public function create($user, array $input)
{
Gate::forUser($user)->authorize('create', Jetstream::newTeamModel());

Validator::make($input, [
'name' => 'required|string|max:255',
])->validateWithBag('createTeam');
Expand Down
2 changes: 1 addition & 1 deletion stubs/inertia/resources/js/Layouts/AppLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
Team Settings
</jet-dropdown-link>

<jet-dropdown-link href="/teams/create">
<jet-dropdown-link href="/teams/create" v-if="$page.user.can.create_team">
Create New Team
</jet-dropdown-link>

Expand Down
2 changes: 2 additions & 0 deletions stubs/livewire/resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@
Team Settings
</x-jet-dropdown-link>

@can('create', Laravel\Jetstream\Jetstream::newTeamModel())
<x-jet-dropdown-link href="/teams/create">
Create New Team
</x-jet-dropdown-link>
@endcan

<div class="border-t border-gray-100"></div>

Expand Down
13 changes: 12 additions & 1 deletion tests/CreateTeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
namespace Laravel\Jetstream\Tests;

use App\Actions\Jetstream\CreateTeam;
use App\Models\Team;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Team;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
use Laravel\Jetstream\Tests\Fixtures\User;

class CreateTeamTest extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_team_name_can_be_updated()
{
$this->migrate();
Expand Down
12 changes: 12 additions & 0 deletions tests/CurrentTeamControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
namespace Laravel\Jetstream\Tests;

use App\Actions\Jetstream\CreateTeam;
use App\Models\Team;
use Illuminate\Support\Facades\Gate;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
use Laravel\Jetstream\Tests\Fixtures\User;

class CurrentTeamControllerTest extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_can_switch_to_team_the_user_belongs_to()
{
$this->migrate();
Expand Down
4 changes: 4 additions & 0 deletions tests/DeleteUserWithTeamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use App\Actions\Jetstream\CreateTeam;
use App\Actions\Jetstream\DeleteTeam;
use App\Actions\Jetstream\DeleteUser;
use App\Models\Team;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
use Laravel\Jetstream\Tests\Fixtures\User;

class DeleteUserWithTeamsTest extends OrchestraTestCase
Expand All @@ -16,6 +19,7 @@ public function setUp(): void
{
parent::setUp();

Gate::policy(Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/TeamBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
namespace Laravel\Jetstream\Tests;

use App\Actions\Jetstream\CreateTeam;
use Illuminate\Support\Facades\Gate;
use Laravel\Jetstream\Jetstream;
use Laravel\Jetstream\Team;
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
use Laravel\Jetstream\Tests\Fixtures\User;
use Laravel\Sanctum\TransientToken;

class TeamBehaviorTest extends OrchestraTestCase
{
public function setUp(): void
{
parent::setUp();

Gate::policy(\App\Models\Team::class, TeamPolicy::class);
Jetstream::useUserModel(User::class);
}

public function test_team_relationship_methods()
{
$this->migrate();
Expand Down