-
Notifications
You must be signed in to change notification settings - Fork 847
/
Copy pathRemoveTeamMember.php
62 lines (54 loc) · 1.62 KB
/
RemoveTeamMember.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Laravel\Jetstream\Actions;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Laravel\Jetstream\Events\TeamMemberRemoved;
class RemoveTeamMember
{
/**
* Remove the team member from the given team.
*
* @param mixed $user
* @param mixed $team
* @param mixed $teamMember
* @return void
*/
public function remove($user, $team, $teamMember)
{
$this->authorize($user, $team, $teamMember);
$this->ensureUserDoesNotOwnTeam($teamMember, $team);
$team->removeUser($teamMember);
TeamMemberRemoved::dispatch($team, $teamMember);
}
/**
* Authorize that the user can remove the team member.
*
* @param mixed $user
* @param mixed $team
* @param mixed $teamMember
* @return void
*/
protected function authorize($user, $team, $teamMember)
{
if (! Gate::forUser($user)->check('removeTeamMember', $team) &&
$user->id !== $teamMember->id) {
throw new AuthorizationException;
}
}
/**
* Ensure that the currently authenticated user does not own the team.
*
* @param mixed $teamMember
* @param mixed $team
* @return void
*/
protected function ensureUserDoesNotOwnTeam($teamMember, $team)
{
if ($teamMember->id === $team->owner->id) {
throw ValidationException::withMessages([
'team' => [__('You may not leave a team that you created.')],
])->errorBag('removeTeamMember');
}
}
}