-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Jetstream classes in providers and separate panels into Adm…
…in and App
- Loading branch information
1 parent
a4c8ee6
commit 8825cd4
Showing
61 changed files
with
1,861 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\Team; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Laravel\Fortify\Contracts\CreatesNewUsers; | ||
use Laravel\Jetstream\Jetstream; | ||
|
||
class CreateNewUser implements CreatesNewUsers | ||
{ | ||
use PasswordValidationRules; | ||
|
||
/** | ||
* Create a newly registered user. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function create(array $input): User | ||
{ | ||
Validator::make($input, [ | ||
'name' => ['required', 'string', 'max:255'], | ||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], | ||
'password' => $this->passwordRules(), | ||
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '', | ||
])->validate(); | ||
|
||
return DB::transaction(function () use ($input) { | ||
return tap(User::create([ | ||
'name' => $input['name'], | ||
'email' => $input['email'], | ||
'password' => Hash::make($input['password']), | ||
]), function (User $user) { | ||
$this->createTeam($user); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Create a personal team for the user. | ||
*/ | ||
protected function createTeam(User $user): void | ||
{ | ||
$user->ownedTeams()->save(Team::forceCreate([ | ||
'user_id' => $user->id, | ||
'name' => explode(' ', $user->name, 2)[0]."'s Team", | ||
'personal_team' => true, | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Models\Team; | ||
use App\Models\User; | ||
use Closure; | ||
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. | ||
*/ | ||
public function add(User $user, Team $team, string $email, string $role = null): void | ||
{ | ||
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. | ||
*/ | ||
protected function validate(Team $team, string $email, ?string $role): void | ||
{ | ||
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<string, \Illuminate\Contracts\Validation\Rule|array|string> | ||
*/ | ||
protected function rules(): array | ||
{ | ||
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. | ||
*/ | ||
protected function ensureUserIsNotAlreadyOnTeam(Team $team, string $email): Closure | ||
{ | ||
return function ($validator) use ($team, $email) { | ||
$validator->errors()->addIf( | ||
$team->hasUserWithEmail($email), | ||
'email', | ||
__('This user already belongs to the team.') | ||
); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Models\Team; | ||
use App\Models\User; | ||
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 array<string, string> $input | ||
*/ | ||
public function create(User $user, array $input): Team | ||
{ | ||
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' => true, | ||
])); | ||
|
||
return $team; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Models\Team; | ||
use Laravel\Jetstream\Contracts\DeletesTeams; | ||
|
||
class DeleteTeam implements DeletesTeams | ||
{ | ||
/** | ||
* Delete the given team. | ||
*/ | ||
public function delete(Team $team): void | ||
{ | ||
$team->purge(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Models\Team; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\DB; | ||
use Laravel\Jetstream\Contracts\DeletesTeams; | ||
use Laravel\Jetstream\Contracts\DeletesUsers; | ||
|
||
class DeleteUser implements DeletesUsers | ||
{ | ||
/** | ||
* Create a new action instance. | ||
*/ | ||
public function __construct(protected DeletesTeams $deletesTeams) | ||
{ | ||
} | ||
|
||
/** | ||
* Delete the given user. | ||
*/ | ||
public function delete(User $user): void | ||
{ | ||
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. | ||
*/ | ||
protected function deleteTeams(User $user): void | ||
{ | ||
$user->teams()->detach(); | ||
|
||
$user->ownedTeams->each(function (Team $team) { | ||
$this->deletesTeams->delete($team); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Models\Team; | ||
use App\Models\User; | ||
use Closure; | ||
use Illuminate\Database\Query\Builder; | ||
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. | ||
*/ | ||
public function invite(User $user, Team $team, string $email, string $role = null): void | ||
{ | ||
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. | ||
*/ | ||
protected function validate(Team $team, string $email, ?string $role): void | ||
{ | ||
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. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string> | ||
*/ | ||
protected function rules(Team $team): array | ||
{ | ||
return array_filter([ | ||
'email' => [ | ||
'required', 'email', | ||
Rule::unique(Jetstream::teamInvitationModel())->where(function (Builder $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. | ||
*/ | ||
protected function ensureUserIsNotAlreadyOnTeam(Team $team, string $email): Closure | ||
{ | ||
return function ($validator) use ($team, $email) { | ||
$validator->errors()->addIf( | ||
$team->hasUserWithEmail($email), | ||
'email', | ||
__('This user already belongs to the team.') | ||
); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Actions\Jetstream; | ||
|
||
use App\Models\Team; | ||
use App\Models\User; | ||
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. | ||
*/ | ||
public function remove(User $user, Team $team, User $teamMember): void | ||
{ | ||
$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. | ||
*/ | ||
protected function authorize(User $user, Team $team, User $teamMember): void | ||
{ | ||
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. | ||
*/ | ||
protected function ensureUserDoesNotOwnTeam(User $teamMember, Team $team): void | ||
{ | ||
if ($teamMember->id === $team->owner->id) { | ||
throw ValidationException::withMessages([ | ||
'team' => [__('You may not leave a team that you created.')], | ||
])->errorBag('removeTeamMember'); | ||
} | ||
} | ||
} |
Oops, something went wrong.