initial commit
This commit is contained in:
		
							
								
								
									
										93
									
								
								app/Actions/Jetstream/AddTeamMember.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								app/Actions/Jetstream/AddTeamMember.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,93 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Illuminate\Support\Facades\Gate; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use Laravel\Jetstream\Contracts\AddsTeamMembers; | ||||
| use Laravel\Jetstream\Events\AddingTeamMember; | ||||
| use Laravel\Jetstream\Events\TeamMemberAdded; | ||||
| use Laravel\Jetstream\Jetstream; | ||||
| use Laravel\Jetstream\Rules\Role; | ||||
| 
 | ||||
| class AddTeamMember implements AddsTeamMembers | ||||
| { | ||||
|     /** | ||||
|      * Add a new team member to the given team. | ||||
|      * | ||||
|      * @param  mixed  $user | ||||
|      * @param  mixed  $team | ||||
|      * @param  string  $email | ||||
|      * @param  string|null  $role | ||||
|      * @return void | ||||
|      */ | ||||
|     public function add($user, $team, string $email, string $role = null) | ||||
|     { | ||||
|         Gate::forUser($user)->authorize('addTeamMember', $team); | ||||
| 
 | ||||
|         $this->validate($team, $email, $role); | ||||
| 
 | ||||
|         $newTeamMember = Jetstream::findUserByEmailOrFail($email); | ||||
| 
 | ||||
|         AddingTeamMember::dispatch($team, $newTeamMember); | ||||
| 
 | ||||
|         $team->users()->attach( | ||||
|             $newTeamMember, ['role' => $role] | ||||
|         ); | ||||
| 
 | ||||
|         TeamMemberAdded::dispatch($team, $newTeamMember); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Validate the add member operation. | ||||
|      * | ||||
|      * @param  mixed  $team | ||||
|      * @param  string  $email | ||||
|      * @param  string|null  $role | ||||
|      * @return void | ||||
|      */ | ||||
|     protected function validate($team, string $email, ?string $role) | ||||
|     { | ||||
|         Validator::make([ | ||||
|             'email' => $email, | ||||
|             'role' => $role, | ||||
|         ], $this->rules(), [ | ||||
|             'email.exists' => __('We were unable to find a registered user with this email address.'), | ||||
|         ])->after( | ||||
|             $this->ensureUserIsNotAlreadyOnTeam($team, $email) | ||||
|         )->validateWithBag('addTeamMember'); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the validation rules for adding a team member. | ||||
|      * | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function rules() | ||||
|     { | ||||
|         return array_filter([ | ||||
|             'email' => ['required', 'email', 'exists:users'], | ||||
|             'role' => Jetstream::hasRoles() | ||||
|                             ? ['required', 'string', new Role] | ||||
|                             : null, | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Ensure that the user is not already on the team. | ||||
|      * | ||||
|      * @param  mixed  $team | ||||
|      * @param  string  $email | ||||
|      * @return \Closure | ||||
|      */ | ||||
|     protected function ensureUserIsNotAlreadyOnTeam($team, string $email) | ||||
|     { | ||||
|         return function ($validator) use ($team, $email) { | ||||
|             $validator->errors()->addIf( | ||||
|                 $team->hasUserWithEmail($email), | ||||
|                 'email', | ||||
|                 __('This user already belongs to the team.') | ||||
|             ); | ||||
|         }; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										37
									
								
								app/Actions/Jetstream/CreateTeam.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								app/Actions/Jetstream/CreateTeam.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Illuminate\Support\Facades\Gate; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use Laravel\Jetstream\Contracts\CreatesTeams; | ||||
| use Laravel\Jetstream\Events\AddingTeam; | ||||
| use Laravel\Jetstream\Jetstream; | ||||
| 
 | ||||
| class CreateTeam implements CreatesTeams | ||||
| { | ||||
|     /** | ||||
|      * Validate and create a new team for the given user. | ||||
|      * | ||||
|      * @param  mixed  $user | ||||
|      * @param  array  $input | ||||
|      * @return mixed | ||||
|      */ | ||||
|     public function create($user, array $input) | ||||
|     { | ||||
|         Gate::forUser($user)->authorize('create', Jetstream::newTeamModel()); | ||||
| 
 | ||||
|         Validator::make($input, [ | ||||
|             'name' => ['required', 'string', 'max:255'], | ||||
|         ])->validateWithBag('createTeam'); | ||||
| 
 | ||||
|         AddingTeam::dispatch($user); | ||||
| 
 | ||||
|         $user->switchTeam($team = $user->ownedTeams()->create([ | ||||
|             'name' => $input['name'], | ||||
|             'personal_team' => false, | ||||
|         ])); | ||||
| 
 | ||||
|         return $team; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										19
									
								
								app/Actions/Jetstream/DeleteTeam.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								app/Actions/Jetstream/DeleteTeam.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Laravel\Jetstream\Contracts\DeletesTeams; | ||||
| 
 | ||||
| class DeleteTeam implements DeletesTeams | ||||
| { | ||||
|     /** | ||||
|      * Delete the given team. | ||||
|      * | ||||
|      * @param  mixed  $team | ||||
|      * @return void | ||||
|      */ | ||||
|     public function delete($team) | ||||
|     { | ||||
|         $team->purge(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										59
									
								
								app/Actions/Jetstream/DeleteUser.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								app/Actions/Jetstream/DeleteUser.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Illuminate\Support\Facades\DB; | ||||
| use Laravel\Jetstream\Contracts\DeletesTeams; | ||||
| use Laravel\Jetstream\Contracts\DeletesUsers; | ||||
| 
 | ||||
| class DeleteUser implements DeletesUsers | ||||
| { | ||||
|     /** | ||||
|      * The team deleter implementation. | ||||
|      * | ||||
|      * @var \Laravel\Jetstream\Contracts\DeletesTeams | ||||
|      */ | ||||
|     protected $deletesTeams; | ||||
| 
 | ||||
|     /** | ||||
|      * Create a new action instance. | ||||
|      * | ||||
|      * @param  \Laravel\Jetstream\Contracts\DeletesTeams  $deletesTeams | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct(DeletesTeams $deletesTeams) | ||||
|     { | ||||
|         $this->deletesTeams = $deletesTeams; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Delete the given user. | ||||
|      * | ||||
|      * @param  mixed  $user | ||||
|      * @return void | ||||
|      */ | ||||
|     public function delete($user) | ||||
|     { | ||||
|         DB::transaction(function () use ($user) { | ||||
|             $this->deleteTeams($user); | ||||
|             $user->deleteProfilePhoto(); | ||||
|             $user->tokens->each->delete(); | ||||
|             $user->delete(); | ||||
|         }); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Delete the teams and team associations attached to the user. | ||||
|      * | ||||
|      * @param  mixed  $user | ||||
|      * @return void | ||||
|      */ | ||||
|     protected function deleteTeams($user) | ||||
|     { | ||||
|         $user->teams()->detach(); | ||||
| 
 | ||||
|         $user->ownedTeams->each(function ($team) { | ||||
|             $this->deletesTeams->delete($team); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										97
									
								
								app/Actions/Jetstream/InviteTeamMember.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								app/Actions/Jetstream/InviteTeamMember.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,97 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Illuminate\Support\Facades\Gate; | ||||
| use Illuminate\Support\Facades\Mail; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use Illuminate\Validation\Rule; | ||||
| use Laravel\Jetstream\Contracts\InvitesTeamMembers; | ||||
| use Laravel\Jetstream\Events\InvitingTeamMember; | ||||
| use Laravel\Jetstream\Jetstream; | ||||
| use Laravel\Jetstream\Mail\TeamInvitation; | ||||
| use Laravel\Jetstream\Rules\Role; | ||||
| 
 | ||||
| class InviteTeamMember implements InvitesTeamMembers | ||||
| { | ||||
|     /** | ||||
|      * Invite a new team member to the given team. | ||||
|      * | ||||
|      * @param  mixed  $user | ||||
|      * @param  mixed  $team | ||||
|      * @param  string  $email | ||||
|      * @param  string|null  $role | ||||
|      * @return void | ||||
|      */ | ||||
|     public function invite($user, $team, string $email, string $role = null) | ||||
|     { | ||||
|         Gate::forUser($user)->authorize('addTeamMember', $team); | ||||
| 
 | ||||
|         $this->validate($team, $email, $role); | ||||
| 
 | ||||
|         InvitingTeamMember::dispatch($team, $email, $role); | ||||
| 
 | ||||
|         $invitation = $team->teamInvitations()->create([ | ||||
|             'email' => $email, | ||||
|             'role' => $role, | ||||
|         ]); | ||||
| 
 | ||||
|         Mail::to($email)->send(new TeamInvitation($invitation)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Validate the invite member operation. | ||||
|      * | ||||
|      * @param  mixed  $team | ||||
|      * @param  string  $email | ||||
|      * @param  string|null  $role | ||||
|      * @return void | ||||
|      */ | ||||
|     protected function validate($team, string $email, ?string $role) | ||||
|     { | ||||
|         Validator::make([ | ||||
|             'email' => $email, | ||||
|             'role' => $role, | ||||
|         ], $this->rules($team), [ | ||||
|             'email.unique' => __('This user has already been invited to the team.'), | ||||
|         ])->after( | ||||
|             $this->ensureUserIsNotAlreadyOnTeam($team, $email) | ||||
|         )->validateWithBag('addTeamMember'); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get the validation rules for inviting a team member. | ||||
|      * | ||||
|      * @param  mixed  $team | ||||
|      * @return array | ||||
|      */ | ||||
|     protected function rules($team) | ||||
|     { | ||||
|         return array_filter([ | ||||
|             'email' => ['required', 'email', Rule::unique('team_invitations')->where(function ($query) use ($team) { | ||||
|                 $query->where('team_id', $team->id); | ||||
|             })], | ||||
|             'role' => Jetstream::hasRoles() | ||||
|                             ? ['required', 'string', new Role] | ||||
|                             : null, | ||||
|         ]); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Ensure that the user is not already on the team. | ||||
|      * | ||||
|      * @param  mixed  $team | ||||
|      * @param  string  $email | ||||
|      * @return \Closure | ||||
|      */ | ||||
|     protected function ensureUserIsNotAlreadyOnTeam($team, string $email) | ||||
|     { | ||||
|         return function ($validator) use ($team, $email) { | ||||
|             $validator->errors()->addIf( | ||||
|                 $team->hasUserWithEmail($email), | ||||
|                 'email', | ||||
|                 __('This user already belongs to the team.') | ||||
|             ); | ||||
|         }; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										63
									
								
								app/Actions/Jetstream/RemoveTeamMember.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								app/Actions/Jetstream/RemoveTeamMember.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Illuminate\Auth\Access\AuthorizationException; | ||||
| use Illuminate\Support\Facades\Gate; | ||||
| use Illuminate\Validation\ValidationException; | ||||
| use Laravel\Jetstream\Contracts\RemovesTeamMembers; | ||||
| use Laravel\Jetstream\Events\TeamMemberRemoved; | ||||
| 
 | ||||
| class RemoveTeamMember implements RemovesTeamMembers | ||||
| { | ||||
|     /** | ||||
|      * 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'); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										31
									
								
								app/Actions/Jetstream/UpdateTeamName.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								app/Actions/Jetstream/UpdateTeamName.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Actions\Jetstream; | ||||
| 
 | ||||
| use Illuminate\Support\Facades\Gate; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use Laravel\Jetstream\Contracts\UpdatesTeamNames; | ||||
| 
 | ||||
| class UpdateTeamName implements UpdatesTeamNames | ||||
| { | ||||
|     /** | ||||
|      * Validate and update the given team's name. | ||||
|      * | ||||
|      * @param  mixed  $user | ||||
|      * @param  mixed  $team | ||||
|      * @param  array  $input | ||||
|      * @return void | ||||
|      */ | ||||
|     public function update($user, $team, array $input) | ||||
|     { | ||||
|         Gate::forUser($user)->authorize('update', $team); | ||||
| 
 | ||||
|         Validator::make($input, [ | ||||
|             'name' => ['required', 'string', 'max:255'], | ||||
|         ])->validateWithBag('updateTeamName'); | ||||
| 
 | ||||
|         $team->forceFill([ | ||||
|             'name' => $input['name'], | ||||
|         ])->save(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user