Skip to content

Commit

Permalink
Merge pull request #227 from Dev-Pulse-Community/authentication-integ…
Browse files Browse the repository at this point in the history
…ration-and-features-implementation

Authentication integration and features implementation
  • Loading branch information
curtisdelicata authored Aug 10, 2024
2 parents b41966b + 439d28c commit b61bc71
Show file tree
Hide file tree
Showing 77 changed files with 2,338 additions and 519 deletions.
34 changes: 13 additions & 21 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,46 @@
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;

/**
* Validate and create a newly registered user.
* Create a newly registered user.
*
* @param array<string, string> $input
* @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',
Rule::unique(User::class),
],
'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'],
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]), function (User $user) {
$team = $this->createTeam($user);
$user->switchTeam($team);
setPermissionsTeamId($team->id);
$user->assignRole('free');
$this->createTeam($user);
});
});
}

/**
* Create a personal team for the user.
*/
protected function createTeam(User $user)
protected function createTeam(User $user): void
{
return $user->ownedTeams()->save(Team::forceCreate([
'user_id' => $user->id,
'name' => explode(' ', $user->name, 2)[0]."'s Team",
$user->ownedTeams()->save(Team::forceCreate([
'user_id' => $user->id,
'name' => explode(' ', $user->name, 2)[0]."'s Team",
'personal_team' => true,
]));
}
Expand Down
16 changes: 7 additions & 9 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
/**
* Validate and update the given user's profile information.
*
* @param array<string, string> $input
* @param array<string, mixed> $input
*/
public function update(User $user, array $input): void
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],

'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique('users')->ignore($user->id),
],
'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);
Expand Down
11 changes: 5 additions & 6 deletions app/Actions/Jetstream/AddTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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
public function add(User $user, Team $team, string $email, ?string $role = null): void
{
Gate::forUser($user)->authorize('addTeamMember', $team);

Expand All @@ -29,8 +29,7 @@ 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 @@ -43,7 +42,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 @@ -60,8 +59,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' => true,
'name' => $input['name'],
'personal_team' => false,
]));

return $team;
Expand Down
38 changes: 30 additions & 8 deletions app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,43 @@

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.
*
* @param mixed $user
* @return void
*/
public function delete($user)
public function delete(User $user): void
{
$user->deleteProfilePhoto();
$user->tokens->each->delete();
$user->connectedAccounts->each->delete();
$user->delete();
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);
});
}
}
8 changes: 4 additions & 4 deletions app/Actions/Jetstream/InviteTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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
public function invite(User $user, Team $team, string $email, ?string $role = null): void
{
Gate::forUser($user)->authorize('addTeamMember', $team);

Expand All @@ -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
15 changes: 15 additions & 0 deletions app/Models/Membership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Models;

use Laravel\Jetstream\Membership as JetstreamMembership;

class Membership extends JetstreamMembership
{
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
}
Loading

0 comments on commit b61bc71

Please sign in to comment.