Skip to content

Commit

Permalink
Configure Jetstream classes in providers and separate panels into Adm…
Browse files Browse the repository at this point in the history
…in and App
  • Loading branch information
curtisdelicata committed Jun 29, 2024
1 parent 38e67cb commit 7fd4713
Show file tree
Hide file tree
Showing 38 changed files with 912 additions and 207 deletions.
41 changes: 14 additions & 27 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,39 @@

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 Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;

/**
* Create a newly registered user.
* Validate and 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'],
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'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,
]));
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);
}
}
53 changes: 53 additions & 0 deletions app/Actions/Fortify/CreateNewUserWithTeams.php
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,
]));
}
}
16 changes: 9 additions & 7 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
/**
* Validate and update the given user's profile information.
*
* @param array<string, mixed> $input
* @param array<string, string> $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']);
}
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique('users')->ignore($user->id),
],
])->validateWithBag('updateProfileInformation');

if ($input['email'] !== $user->email &&
$user instanceof MustVerifyEmail) {
Expand Down
9 changes: 5 additions & 4 deletions app/Actions/Jetstream/AddTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function add(User $user, Team $team, string $email, string $role = null):
AddingTeamMember::dispatch($team, $newTeamMember);

$team->users()->attach(
$newTeamMember, ['role' => $role]
$newTeamMember,
['role' => $role]
);

TeamMemberAdded::dispatch($team, $newTeamMember);
Expand All @@ -42,7 +43,7 @@ protected function validate(Team $team, string $email, ?string $role): void
{
Validator::make([
'email' => $email,
'role' => $role,
'role' => $role,
], $this->rules(), [
'email.exists' => __('We were unable to find a registered user with this email address.'),
])->after(
Expand All @@ -59,8 +60,8 @@ protected function rules(): array
{
return array_filter([
'email' => ['required', 'email', 'exists:users'],
'role' => Jetstream::hasRoles()
? ['required', 'string', new Role]
'role' => Jetstream::hasRoles()
? ['required', 'string', new Role()]
: null,
]);
}
Expand Down
6 changes: 3 additions & 3 deletions app/Actions/Jetstream/CreateTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CreateTeam implements CreatesTeams
/**
* Validate and create a new team for the given user.
*
* @param array<string, string> $input
* @param array<string, string> $input
*/
public function create(User $user, array $input): Team
{
Expand All @@ -28,8 +28,8 @@ public function create(User $user, array $input): Team
AddingTeam::dispatch($user);

$user->switchTeam($team = $user->ownedTeams()->create([
'name' => $input['name'],
'personal_team' => false,
'name' => $input['name'],
'personal_team' => true,
]));

return $team;
Expand Down
44 changes: 44 additions & 0 deletions app/Actions/Jetstream/DeleteUserWithTeams.php
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);
});
}
}
6 changes: 3 additions & 3 deletions app/Actions/Jetstream/InviteTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function invite(User $user, Team $team, string $email, string $role = nul

$invitation = $team->teamInvitations()->create([
'email' => $email,
'role' => $role,
'role' => $role,
]);

Mail::to($email)->send(new TeamInvitation($invitation));
Expand All @@ -44,7 +44,7 @@ protected function validate(Team $team, string $email, ?string $role): void
{
Validator::make([
'email' => $email,
'role' => $role,
'role' => $role,
], $this->rules($team), [
'email.unique' => __('This user has already been invited to the team.'),
])->after(
Expand All @@ -67,7 +67,7 @@ protected function rules(Team $team): array
}),
],
'role' => Jetstream::hasRoles()
? ['required', 'string', new Role]
? ['required', 'string', new Role()]
: null,
]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Jetstream/RemoveTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function remove(User $user, Team $team, User $teamMember): void
*/
protected function authorize(User $user, Team $team, User $teamMember): void
{
if (! Gate::forUser($user)->check('removeTeamMember', $team) &&
if (!Gate::forUser($user)->check('removeTeamMember', $team) &&
$user->id !== $teamMember->id) {
throw new AuthorizationException;
throw new AuthorizationException();
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Jetstream/UpdateTeamName.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class UpdateTeamName implements UpdatesTeamNames
/**
* Validate and update the given team's name.
*
* @param array<string, string> $input
* @param array<string, string> $input
*/
public function update(User $user, Team $team, array $input): void
{
Expand Down
62 changes: 62 additions & 0 deletions app/Actions/Socialstream/CreateUserWithTeamsFromProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Actions\Socialstream;

use App\Models\Team;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use JoelButcher\Socialstream\Contracts\CreatesConnectedAccounts;
use JoelButcher\Socialstream\Contracts\CreatesUserFromProvider;
use JoelButcher\Socialstream\Socialstream;
use Laravel\Socialite\Contracts\User as ProviderUserContract;

class CreateUserFromProvider implements CreatesUserFromProvider
{
/**
* The creates connected accounts instance.
*/
public CreatesConnectedAccounts $createsConnectedAccounts;

/**
* Create a new action instance.
*/
public function __construct(CreatesConnectedAccounts $createsConnectedAccounts)
{
$this->createsConnectedAccounts = $createsConnectedAccounts;
}

/**
* Create a new user from a social provider user.
*/
public function create(string $provider, ProviderUserContract $providerUser): User
{
return DB::transaction(function () use ($provider, $providerUser) {
return tap(User::create([
'name' => $providerUser->getName(),
'email' => $providerUser->getEmail(),
]), function (User $user) use ($provider, $providerUser) {
$user->markEmailAsVerified();

if (Socialstream::hasProviderAvatarsFeature() && $providerUser->getAvatar()) {
$user->setProfilePhotoFromUrl($providerUser->getAvatar());
}

$this->createsConnectedAccounts->create($user, $provider, $providerUser);

$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,
]));
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/ForgotPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Laravel\Jetstream\Http\Controllers\AuthenticatedSessionController;

class AdminForgotPasswordController extends AuthenticatedSessionController
{
public function showLinkRequestForm()
{
return view('admin.auth.forgot-password');
}

public function sendResetLinkEmail(Request $request)
{
$request->validate(['email' => 'required|email']);

$status = Password::broker('admins')->sendResetLink(
$request->only('email')
);

return $status === Password::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
}
Loading

0 comments on commit 7fd4713

Please sign in to comment.