Skip to content

Commit

Permalink
Improving usability from the app
Browse files Browse the repository at this point in the history
  • Loading branch information
butburg committed Sep 2, 2024
1 parent 7bbccd6 commit d17482b
Show file tree
Hide file tree
Showing 20 changed files with 409 additions and 271 deletions.
2 changes: 1 addition & 1 deletion .timetracker

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class AdminController extends Controller
public function dashboard()
{
$users = User::withCount('posts', 'comments')->get();
return view('admin.dashboard', compact('users'));
return view('admin.index', compact('users'));
}
}
50 changes: 18 additions & 32 deletions src/app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class PostController extends Controller
{
class PostController extends Controller {
/**
* Display a listing of the resource.
*/
public function index(): View
{
public function index(): View {
$userId = Auth::id();
$userType = Auth::user()->usertype;

// Retrieve published and draft posts from the database
$draftPosts = Post::whereNotNull('user_id')
->where('user_id', $userId)
Expand All @@ -46,44 +44,40 @@ public function index(): View
/**
* Display a special listing of the resources with all only for admin.
*/
public function all(): View
{
public function all(): View {
// Retrieve published and draft posts from the database and pass them to the view
return view('posts.index', [
'draftPosts' => Post::where('is_published', false)
->orderBy('updated_at', 'desc')
->paginate(10),
->paginate(15),
'publishedPosts' => Post::where('is_published', true)
->orderBy('updated_at', 'desc')
->paginate(10),
->paginate(15),
]);
}
/**
* Display the gallery with pagination.
*/
public function gallery(Request $request): View
{
public function gallery(Request $request): View {
$posts = Post::where('is_published', true)
->orderBy('created_at', 'desc')
->paginate(10);
->paginate(15);

return view('welcome', compact('posts'));
}

/**
* Show the form for creating a new resource.
*/
public function create(): View
{
public function create(): View {
// Return the view for creating a new post
return view('posts.edit');
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreRequest $request, CreateImageVariants $createImageVariants): RedirectResponse
{
public function store(StoreRequest $request, CreateImageVariants $createImageVariants): RedirectResponse {
// Validate the incoming request
$validated = $request->validated();

Expand Down Expand Up @@ -128,17 +122,15 @@ public function store(StoreRequest $request, CreateImageVariants $createImageVar
/**
* Display the specified resource.
*/
public function show(string $id): View
{
public function show(string $id): View {
$post = Post::findOrFail($id);
return view('posts.show', compact('post'));
}

/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): View
{
public function edit(string $id): View {
// Retrieve the post with the specified ID
$post = Post::findOrFail($id);

Expand All @@ -158,8 +150,7 @@ public function edit(string $id): View
/**
* Update the specified resource in storage.
*/
public function update(UpdateRequest $request, string $id, CreateImageVariants $createImageVariants): RedirectResponse
{
public function update(UpdateRequest $request, string $id, CreateImageVariants $createImageVariants): RedirectResponse {
// Find the post with the specified ID
$post = Post::findOrFail($id);

Expand Down Expand Up @@ -208,8 +199,7 @@ public function update(UpdateRequest $request, string $id, CreateImageVariants $
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id): RedirectResponse
{
public function destroy(string $id): RedirectResponse {
$post = Post::findOrFail($id);

$this->userIsOwner($post->user_id);
Expand Down Expand Up @@ -240,8 +230,7 @@ public function destroy(string $id): RedirectResponse
/**
* Mark the specified post as published.
*/
public function publish(string $id): RedirectResponse
{
public function publish(string $id): RedirectResponse {
// Find the post with the specified ID and update its publication status
$post = Post::findOrFail($id);

Expand All @@ -260,8 +249,7 @@ public function publish(string $id): RedirectResponse
/**
* Mark the specified post as a draft.
*/
public function makedraft(string $id): RedirectResponse
{
public function makedraft(string $id): RedirectResponse {
// Find the post with the specified ID and update its publication status
$post = Post::findOrFail($id);
// Check if the authenticated user owns the post
Expand All @@ -286,8 +274,7 @@ public function makedraft(string $id): RedirectResponse
/*
* Hide the specified post (unpublish it without making it a draft).
*/
public function hide(string $id): RedirectResponse
{
public function hide(string $id): RedirectResponse {
$post = Post::findOrFail($id);

// Check if the authenticated user owns the post
Expand All @@ -309,8 +296,7 @@ public function hide(string $id): RedirectResponse
return redirect()->route('posts.index');
}

private function userIsOwner($user_id_from_post): void
{
private function userIsOwner($user_id_from_post): void {
if ($user_id_from_post !== Auth::id() and Auth::user()->usertype !== 'admin') {
abort(403, 'Unauthorized action.');
}
Expand Down
92 changes: 55 additions & 37 deletions src/app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,63 @@
use Illuminate\View\View;

class ProfileController extends Controller {
/**
* Update the user's profile information.
*/
public function update(ProfileUpdateRequest $request): RedirectResponse {
$user = $request->user();
$validatedData = $request->validated();

// Check if the name has changed
if ($user->name !== $validatedData['name']) {
public function updateName(ProfileUpdateRequest $request): RedirectResponse {

// Validate the name change interval
$statusMessage = LastNameChange::getNameChangeStatus($user->last_name_change, 30);
if ($statusMessage) {
return Redirect::back()->withErrors(['name' => $statusMessage]);
}
$user = Auth::user();
$name = $request->validated()['name'];
// Check if the name has changed
if ($user->name === $name) {
// No change detected, return to the same route with a message
return Redirect::back()->withErrors(['name' => 'That\'s your name already :P']);
}

// Update previous_name and last_name_change
$user->previous_name = $user->name;
$user->last_name_change = now();
// Validate the name change interval
$statusMessage = LastNameChange::getNameChangeStatus($user->last_name_change, 30);
if ($statusMessage) {
session()->flash('notif.success', 'Failed!');
return Redirect::back()->withErrors(['name' => $statusMessage]);
}

// Update user profile information
$user->fill($validatedData);
// Update previous_name and last_name_change
$user->previous_name = $user->name;
$user->last_name_change = now();

// Update the username
$user->name = $name;
$user->save();


// Handle email verification reset if email has changed
if ($user->isDirty('email')) {
$user->email_verified_at = null;
session()->flash('notif.success', 'Your username updated successfully!');
return Redirect::route('profile.edit');
}

public function updateEmail(ProfileUpdateRequest $request): RedirectResponse {

$user = Auth::user();
$email = $request->validated()['email'];

if ($user->email === $email) {
// No change detected, return to the same route with a message
return Redirect::back()->withErrors(['email' => 'That\'s your email already :P']);
}

// Handle email verification reset
$user->email_verified_at = null;
$user->email = $email;
$user->save();

return Redirect::route('profile.edit')->with('status', 'profile-updated');
session()->flash('notif.success', 'Your mail updated successfully!');
return Redirect::route('profile.edit');
}
/**
* Display the user's profile form.
*/
public function edit(Request $request): View {
return view('profile.edit', [
'user' => $request->user(),
]);
}


/**
* Update the user's profile image.
*/
public function updateImage(FormRequest $request, CreateImageVariants $createImageVariants): RedirectResponse {
$validated = $request->validate([
'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif|max:4096',
]);

$imageFile = $validated['profile_image'];
public function updateImage(ProfileUpdateRequest $request, CreateImageVariants $createImageVariants): RedirectResponse {

$user = Auth::user();
$imageFile = $request->validated()['profile_image'];

// Check if the user already has a profile image
$image = $user->image ?: new Image(['user_id' => $user->id]);
Expand All @@ -92,6 +95,21 @@ public function updateImage(FormRequest $request, CreateImageVariants $createIma
return Redirect::route('profile.edit')->with('status', 'profile-updated');
}

/**
* Display the user's profile form.
*/
public function edit(Request $request): View {
return view('profile.edit', [
'user' => $request->user(),
]);
}

public function show(Request $request): View {
return view('profile.show', [
'user' => $request->user(),
]);
}

/**
* Delete the user's account.
*/
Expand Down
25 changes: 13 additions & 12 deletions src/app/Http/Requests/Auth/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class LoginRequest extends FormRequest
{
class LoginRequest extends FormRequest {
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
public function authorize(): bool {
return true;
}

Expand All @@ -24,8 +22,7 @@ public function authorize(): bool
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
public function rules(): array {
return [
'email' => ['required', 'string', 'email'],
'password' => ['required', 'string'],
Expand All @@ -37,8 +34,7 @@ public function rules(): array
*
* @throws \Illuminate\Validation\ValidationException
*/
public function authenticate(): void
{
public function authenticate(): void {
$this->ensureIsNotRateLimited();

if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
Expand All @@ -49,8 +45,15 @@ public function authenticate(): void
]);
}

// Automatically mark the email as verified in development environments
$user = Auth::user();

if (app()->environment('local', 'development') && !$user->hasVerifiedEmail()) {
$user->markEmailAsVerified();
// Flash a session message to notify the user
session()->flash('notif.success', 'Your email has been automatically verified for development purposes.');
}

RateLimiter::clear($this->throttleKey());
}

Expand All @@ -59,8 +62,7 @@ public function authenticate(): void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function ensureIsNotRateLimited(): void
{
public function ensureIsNotRateLimited(): void {
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
return;
}
Expand All @@ -80,8 +82,7 @@ public function ensureIsNotRateLimited(): void
/**
* Get the rate limiting throttle key for the request.
*/
public function throttleKey(): string
{
public function throttleKey(): string {
return Str::transliterate(Str::lower($this->string('email')) . '|' . $this->ip());
}
}
26 changes: 17 additions & 9 deletions src/app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfileUpdateRequest extends FormRequest
{
class ProfileUpdateRequest extends FormRequest {
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
'profile_image' => ['nullable|image|mimes:jpeg,png,jpg,gif,svg|max:4096'],
];
public function rules(): array {
$rules = [];

if ($this->routeIs('profile.updateName')) {
$rules['name'] = ['required', 'string', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)];
}

if ($this->routeIs('profile.updateEmail')) {
$rules['email'] = ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)];
}

if ($this->routeIs('profile.updateImage')) {
$rules['profile_image'] = ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:4096'];
}

return $rules;
}
}
Loading

0 comments on commit d17482b

Please sign in to comment.