Skip to content

Commit 21a4469

Browse files
authored
Extract switch team logic into HasTeams trait (#312)
1 parent 096d7dd commit 21a4469

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/HasTeams.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,30 @@ public function isCurrentTeam($team)
2424
public function currentTeam()
2525
{
2626
if (is_null($this->current_team_id) && $this->id) {
27-
$this->forceFill([
28-
'current_team_id' => $this->personalTeam()->id,
29-
])->save();
27+
$this->switchTeam($this->personalTeam());
3028
}
3129

3230
return $this->belongsTo(Jetstream::teamModel(), 'current_team_id');
3331
}
3432

33+
/**
34+
* Switch the user's context to the given team.
35+
*
36+
* @return bool
37+
*/
38+
public function switchTeam($team)
39+
{
40+
if (! $this->belongsToTeam($team)) {
41+
return false;
42+
}
43+
44+
$this->forceFill([
45+
'current_team_id' => $team->id,
46+
])->save();
47+
48+
return true;
49+
}
50+
3551
/**
3652
* Get all of the teams the user owns or belongs to.
3753
*

src/Http/Controllers/CurrentTeamController.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@ public function update(Request $request)
1818
{
1919
$team = Jetstream::newTeamModel()->findOrFail($request->team_id);
2020

21-
if (! $request->user()->belongsToTeam($team)) {
21+
if (! $request->user()->switchTeam($team)) {
2222
abort(403);
2323
}
2424

25-
$request->user()->forceFill([
26-
'current_team_id' => $team->id,
27-
])->save();
28-
2925
return redirect(config('fortify.home'), 303);
3026
}
3127
}

0 commit comments

Comments
 (0)