-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
122 changed files
with
5,229 additions
and
6 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,18 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use Illuminate\Validation\Rules\Password; | ||
|
||
trait PasswordValidationRules | ||
{ | ||
/** | ||
* Get the validation rules used to validate passwords. | ||
* | ||
* @return array<int, \Illuminate\Contracts\Validation\Rule|array<mixed>|string> | ||
*/ | ||
protected function passwordRules(): array | ||
{ | ||
return ['required', 'string', Password::default(), 'confirmed']; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Laravel\Fortify\Contracts\ResetsUserPasswords; | ||
|
||
class ResetUserPassword implements ResetsUserPasswords | ||
{ | ||
use PasswordValidationRules; | ||
|
||
/** | ||
* Validate and reset the user's forgotten password. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function reset(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'password' => $this->passwordRules(), | ||
])->validate(); | ||
|
||
$user->forceFill([ | ||
'password' => Hash::make($input['password']), | ||
])->save(); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Support\Facades\Validator; | ||
use Laravel\Fortify\Contracts\UpdatesUserPasswords; | ||
|
||
class UpdateUserPassword implements UpdatesUserPasswords | ||
{ | ||
use PasswordValidationRules; | ||
|
||
/** | ||
* Validate and update the user's password. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
public function update(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'current_password' => ['required', 'string', 'current_password:web'], | ||
'password' => $this->passwordRules(), | ||
], [ | ||
'current_password.current_password' => __('The provided password does not match your current password.'), | ||
])->validateWithBag('updatePassword'); | ||
|
||
$user->forceFill([ | ||
'password' => Hash::make($input['password']), | ||
])->save(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
apps/app/app/Actions/Fortify/UpdateUserProfileInformation.php
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,56 @@ | ||
<?php | ||
|
||
namespace App\Actions\Fortify; | ||
|
||
use App\Models\User; | ||
use Illuminate\Contracts\Auth\MustVerifyEmail; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation; | ||
|
||
class UpdateUserProfileInformation implements UpdatesUserProfileInformation | ||
{ | ||
/** | ||
* Validate and update the given user's profile information. | ||
* | ||
* @param array<string, mixed> $input | ||
*/ | ||
public function update(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'name' => ['required', 'string', 'max:255'], | ||
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)], | ||
'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'], | ||
])->validateWithBag('updateProfileInformation'); | ||
|
||
if (isset($input['photo'])) { | ||
$user->updateProfilePhoto($input['photo']); | ||
} | ||
|
||
if ($input['email'] !== $user->email && | ||
$user instanceof MustVerifyEmail) { | ||
$this->updateVerifiedUser($user, $input); | ||
} else { | ||
$user->forceFill([ | ||
'name' => $input['name'], | ||
'email' => $input['email'], | ||
])->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Update the given verified user's profile information. | ||
* | ||
* @param array<string, string> $input | ||
*/ | ||
protected function updateVerifiedUser(User $user, array $input): void | ||
{ | ||
$user->forceFill([ | ||
'name' => $input['name'], | ||
'email' => $input['email'], | ||
'email_verified_at' => null, | ||
])->save(); | ||
|
||
$user->sendEmailVerificationNotification(); | ||
} | ||
} |
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,81 @@ | ||
<?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' => false, | ||
])); | ||
|
||
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); | ||
}); | ||
} | ||
} |
Oops, something went wrong.