Skip to content

Commit a279cdc

Browse files
committed
Implement TeamPolicy::create authorization check before creating a team.
Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent a8f584a commit a279cdc

File tree

8 files changed

+49
-2
lines changed

8 files changed

+49
-2
lines changed

Diff for: src/Http/Middleware/ShareInertiaData.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Jetstream\Http\Middleware;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Illuminate\Support\Facades\Route;
67
use Illuminate\Support\Facades\Session;
78
use Inertia\Inertia;
@@ -42,6 +43,9 @@ public function handle($request, $next)
4243
'all_teams' => Jetstream::hasTeamFeatures() ? $request->user()->allTeams() : null,
4344
]), [
4445
'two_factor_enabled' => ! is_null($request->user()->two_factor_secret),
46+
'can' => [
47+
'create_team' => Jetstream::hasTeamFeatures() && Gate::forUser($user)->authorize('create', Jetstream::newTeamModel()),
48+
],
4549
]);
4650
},
4751
'errorBags' => function () {

Diff for: stubs/app/Actions/Jetstream/CreateTeam.php

+4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace App\Actions\Jetstream;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Illuminate\Support\Facades\Validator;
67
use Laravel\Jetstream\Contracts\CreatesTeams;
8+
use Laravel\Jetstream\Jetstream;
79

810
class CreateTeam implements CreatesTeams
911
{
@@ -16,6 +18,8 @@ class CreateTeam implements CreatesTeams
1618
*/
1719
public function create($user, array $input)
1820
{
21+
Gate::forUser($user)->authorize('create', Jetstream::newTeamModel());
22+
1923
Validator::make($input, [
2024
'name' => 'required|string|max:255',
2125
])->validateWithBag('createTeam');

Diff for: stubs/inertia/resources/js/Layouts/AppLayout.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
Team Settings
5858
</jet-dropdown-link>
5959

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

Diff for: stubs/livewire/resources/views/layouts/app.blade.php

+2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@
7878
Team Settings
7979
</x-jet-dropdown-link>
8080

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

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

Diff for: tests/CreateTeamTest.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
namespace Laravel\Jetstream\Tests;
44

55
use App\Actions\Jetstream\CreateTeam;
6+
use App\Models\Team;
7+
use Illuminate\Support\Facades\Gate;
68
use Illuminate\Validation\ValidationException;
7-
use Laravel\Jetstream\Team;
9+
use Laravel\Jetstream\Jetstream;
10+
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
811
use Laravel\Jetstream\Tests\Fixtures\User;
912

1013
class CreateTeamTest extends OrchestraTestCase
1114
{
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
Gate::policy(Team::class, TeamPolicy::class);
20+
Jetstream::useUserModel(User::class);
21+
}
22+
1223
public function test_team_name_can_be_updated()
1324
{
1425
$this->migrate();

Diff for: tests/CurrentTeamControllerTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
namespace Laravel\Jetstream\Tests;
44

55
use App\Actions\Jetstream\CreateTeam;
6+
use App\Models\Team;
7+
use Illuminate\Support\Facades\Gate;
8+
use Laravel\Jetstream\Jetstream;
9+
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
610
use Laravel\Jetstream\Tests\Fixtures\User;
711

812
class CurrentTeamControllerTest extends OrchestraTestCase
913
{
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
Gate::policy(Team::class, TeamPolicy::class);
19+
Jetstream::useUserModel(User::class);
20+
}
21+
1022
public function test_can_switch_to_team_the_user_belongs_to()
1123
{
1224
$this->migrate();

Diff for: tests/DeleteUserWithTeamsTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
use App\Actions\Jetstream\CreateTeam;
66
use App\Actions\Jetstream\DeleteTeam;
77
use App\Actions\Jetstream\DeleteUser;
8+
use App\Models\Team;
89
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Gate;
911
use Illuminate\Support\Str;
1012
use Laravel\Jetstream\Jetstream;
13+
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
1114
use Laravel\Jetstream\Tests\Fixtures\User;
1215

1316
class DeleteUserWithTeamsTest extends OrchestraTestCase
@@ -16,6 +19,7 @@ public function setUp(): void
1619
{
1720
parent::setUp();
1821

22+
Gate::policy(Team::class, TeamPolicy::class);
1923
Jetstream::useUserModel(User::class);
2024
}
2125

Diff for: tests/TeamBehaviorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33
namespace Laravel\Jetstream\Tests;
44

55
use App\Actions\Jetstream\CreateTeam;
6+
use Illuminate\Support\Facades\Gate;
67
use Laravel\Jetstream\Jetstream;
78
use Laravel\Jetstream\Team;
9+
use Laravel\Jetstream\Tests\Fixtures\TeamPolicy;
810
use Laravel\Jetstream\Tests\Fixtures\User;
911
use Laravel\Sanctum\TransientToken;
1012

1113
class TeamBehaviorTest extends OrchestraTestCase
1214
{
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
Gate::policy(\App\Models\Team::class, TeamPolicy::class);
20+
Jetstream::useUserModel(User::class);
21+
}
22+
1323
public function test_team_relationship_methods()
1424
{
1525
$this->migrate();

0 commit comments

Comments
 (0)