feat: allow users to delete/create own profile
This commit is contained in:
parent
85962de188
commit
dcbff0f52e
10
app/Exceptions/ProfileAlreadyExistsException.php
Normal file
10
app/Exceptions/ProfileAlreadyExistsException.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class ProfileAlreadyExistsException extends Exception
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
10
app/Exceptions/ProfileCreationFailedException.php
Normal file
10
app/Exceptions/ProfileCreationFailedException.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class ProfileCreationFailedException extends Exception
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
@ -21,6 +21,9 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Exceptions\ProfileAlreadyExistsException;
|
||||||
|
use App\Exceptions\ProfileCreationFailedException;
|
||||||
|
use App\Exceptions\ProfileNotFoundException;
|
||||||
use App\Facades\IP;
|
use App\Facades\IP;
|
||||||
use App\Http\Requests\ProfileSave;
|
use App\Http\Requests\ProfileSave;
|
||||||
use App\Services\ProfileService;
|
use App\Services\ProfileService;
|
||||||
@ -123,4 +126,44 @@ class ProfileController extends Controller
|
|||||||
->back()
|
->back()
|
||||||
->with('success', __('Profile updated.'));
|
->with('success', __('Profile updated.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function createProfile(Request $request)
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->profileService->createProfile($request->user());
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->back()
|
||||||
|
->with('error', $e->getMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->back()
|
||||||
|
->with('success', __('Your profile has been created.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function deleteProfile(Request $request)
|
||||||
|
{
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->profileService->deleteProfile($request->user());
|
||||||
|
} catch (ProfileNotFoundException $e) {
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->back()
|
||||||
|
->with('error', $e->getMessage());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirect()
|
||||||
|
->back()
|
||||||
|
->with('success', __('Profile deleted successfully.'));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,10 @@
|
|||||||
|
|
||||||
namespace App\Observers;
|
namespace App\Observers;
|
||||||
|
|
||||||
|
use App\Exceptions\ProfileAlreadyExistsException;
|
||||||
|
use App\Exceptions\ProfileCreationFailedException;
|
||||||
use App\Profile;
|
use App\Profile;
|
||||||
|
use App\Services\ProfileService;
|
||||||
use App\User;
|
use App\User;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
@ -38,17 +41,24 @@ class UserObserver
|
|||||||
* @param \App\User $user
|
* @param \App\User $user
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function created(User $user)
|
public function created(ProfileService $profileService, User $user)
|
||||||
{
|
{
|
||||||
// TODO: Make sure that the profile is created, throw an exception if otherwise, or try to recreate the profile.
|
try
|
||||||
Profile::create([
|
{
|
||||||
|
$profileService->createProfile($user);
|
||||||
'profileShortBio' => 'Write a one-liner about you here!',
|
}
|
||||||
'profileAboutMe' => 'Tell us a bit about you.',
|
catch (ProfileAlreadyExistsException $exception)
|
||||||
'socialLinks' => '{}',
|
{
|
||||||
'userID' => $user->id,
|
Log::error('Attempting to create profile that already exists!', [
|
||||||
|
'trace' => $exception->getTrace()
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
catch (ProfileCreationFailedException $e)
|
||||||
|
{
|
||||||
|
Log::error('Failed creating a new profile!', [
|
||||||
|
'trace' => $e->getTrace()
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,15 +4,56 @@
|
|||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Exceptions\ProfileAlreadyExistsException;
|
||||||
|
use App\Exceptions\ProfileCreationFailedException;
|
||||||
use App\Exceptions\ProfileNotFoundException;
|
use App\Exceptions\ProfileNotFoundException;
|
||||||
|
use App\Profile;
|
||||||
use App\User;
|
use App\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class ProfileService
|
class ProfileService
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Creates a new profile for the specified $targetUser.
|
||||||
|
*
|
||||||
|
* @param User $targetUser The user to create the profile for.
|
||||||
|
* @return bool
|
||||||
|
* @throws ProfileAlreadyExistsException
|
||||||
|
* @throws ProfileCreationFailedException
|
||||||
|
*/
|
||||||
|
public function createProfile(User $targetUser): Profile {
|
||||||
|
|
||||||
|
if (is_null($targetUser->profile)) {
|
||||||
|
|
||||||
|
$profile = Profile::create([
|
||||||
|
|
||||||
|
'profileShortBio' => 'Write a one-liner about you here!',
|
||||||
|
'profileAboutMe' => 'Tell us a bit about you.',
|
||||||
|
'socialLinks' => '{}',
|
||||||
|
'userID' => $targetUser->id,
|
||||||
|
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (is_null($profile)) {
|
||||||
|
throw new ProfileCreationFailedException(__('Could not create profile! Please try again later.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::info('Created profile for new user', [
|
||||||
|
'userid' => $targetUser->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ProfileAlreadyExistsException(__('Profile already exists!'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the user's profile.
|
||||||
|
*
|
||||||
* @throws ProfileNotFoundException
|
* @throws ProfileNotFoundException
|
||||||
*/
|
*/
|
||||||
public function updateProfile($userID, Request $request) {
|
public function updateProfile($userID, Request $request) {
|
||||||
@ -47,4 +88,26 @@ class ProfileService
|
|||||||
throw new ProfileNotFoundException("This profile does not exist.");
|
throw new ProfileNotFoundException("This profile does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete specified user's profile
|
||||||
|
*
|
||||||
|
* @param User $targetUser
|
||||||
|
* @return bool
|
||||||
|
* @throws ProfileNotFoundException
|
||||||
|
*/
|
||||||
|
public function deleteProfile(User $targetUser): bool
|
||||||
|
{
|
||||||
|
|
||||||
|
if (!is_null($targetUser->profile)) {
|
||||||
|
|
||||||
|
Log::alert('Deleted user profile', [
|
||||||
|
'userid' => $targetUser->id
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $targetUser->profile->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ProfileNotFoundException(__('Attempting to delete non-existant profile!'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
@ -192,6 +192,7 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => ['lo
|
|||||||
|
|
||||||
// Further locking down the profile section by adding the middleware to everything but the required routes
|
// Further locking down the profile section by adding the middleware to everything but the required routes
|
||||||
Route::group(['prefix' => '/profile'], function () {
|
Route::group(['prefix' => '/profile'], function () {
|
||||||
|
|
||||||
Route::get('/settings', [ProfileController::class, 'showProfile'])
|
Route::get('/settings', [ProfileController::class, 'showProfile'])
|
||||||
->name('showProfileSettings')
|
->name('showProfileSettings')
|
||||||
->middleware('passwordredirect');
|
->middleware('passwordredirect');
|
||||||
@ -204,6 +205,13 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => ['lo
|
|||||||
->name('showSingleProfile')
|
->name('showSingleProfile')
|
||||||
->middleware('passwordredirect');
|
->middleware('passwordredirect');
|
||||||
|
|
||||||
|
Route::post('user/profile', [ProfileController::class, 'createProfile'])
|
||||||
|
->name('createProfile')
|
||||||
|
->middleware('passwordredirect');
|
||||||
|
|
||||||
|
Route::delete('user/profile', [ProfileController::class, 'deleteProfile'])
|
||||||
|
->name('deleteProfile')
|
||||||
|
->middleware('passwordredirect');
|
||||||
|
|
||||||
|
|
||||||
Route::get('/settings/account', [UserController::class, 'showAccount'])
|
Route::get('/settings/account', [UserController::class, 'showAccount'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user