From b518158d318d53f5465e4a6c626e48295689e83e Mon Sep 17 00:00:00 2001 From: Kei Date: Tue, 24 Dec 2024 05:26:52 +0700 Subject: [PATCH] Streamline the controller files (#1044) * Merge to UrlController * Merge to DashboardController --- .../Dashboard/AllUrlController.php | 48 ------------- .../Dashboard/DashboardController.php | 67 ++++++------------- app/Http/Controllers/UrlController.php | 52 +++++++++++++- routes/web.php | 13 ++-- tests/Feature/AuthPage/DashboardPageTest.php | 14 ++-- .../AuthPage/UrlListPageSingleUserTest.php | 8 +-- tests/Feature/AuthPage/UrlListPageTest.php | 18 ++--- 7 files changed, 99 insertions(+), 121 deletions(-) delete mode 100644 app/Http/Controllers/Dashboard/AllUrlController.php diff --git a/app/Http/Controllers/Dashboard/AllUrlController.php b/app/Http/Controllers/Dashboard/AllUrlController.php deleted file mode 100644 index 81daedb42..000000000 --- a/app/Http/Controllers/Dashboard/AllUrlController.php +++ /dev/null @@ -1,48 +0,0 @@ - $author, - 'authorId' => User::where('name', $author)->first()->id, - ]); - } - - /** - * Show all short URLs created by guest. - * - * @return \Illuminate\Contracts\View\View - */ - public function guestLinkView() - { - return view('backend.url-list-of-guest'); - } -} diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 527df8d77..64510fb2c 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -4,14 +4,19 @@ use App\Http\Controllers\Controller; use App\Models\Url; +use App\Models\User; use App\Models\Visit; -use Illuminate\Http\Request; -use Illuminate\Support\Facades\Gate; +use Illuminate\Routing\Controllers\{HasMiddleware, Middleware}; -class DashboardController extends Controller +class DashboardController extends Controller implements HasMiddleware { + public static function middleware(): array + { + return [new Middleware('role:admin', except: ['view'])]; + } + /** - * Show all user short URLs. + * Show the dashboard and the URL list. * * @return \Illuminate\Contracts\View\View */ @@ -26,63 +31,35 @@ public function view() } /** - * Show shortened url details page. + * Show all short URLs created by all users. * - * @param Url $url \App\Models\Url * @return \Illuminate\Contracts\View\View */ - public function edit(Url $url) + public function allUrlView() { - Gate::authorize('updateUrl', $url); - - return view('backend.edit', ['url' => $url]); + return view('backend.url-list'); } /** - * Update the destination URL. + * Show all short links from specific user. * - * @param Request $request \Illuminate\Http\Request - * @param Url $url \App\Models\Url - * @return \Illuminate\Http\RedirectResponse - * - * @throws \Illuminate\Auth\Access\AuthorizationException + * @return \Illuminate\Contracts\View\View */ - public function update(Request $request, Url $url) + public function userLinkView(string $author) { - Gate::authorize('updateUrl', $url); - - $request->validate([ - 'title' => ['max:' . Url::TITLE_LENGTH], - 'long_url' => [ - 'required', 'url', 'max:65535', - new \App\Rules\NotBlacklistedDomain, - ], + return view('backend.url-list-of-user', [ + 'authorName' => $author, + 'authorId' => User::where('name', $author)->first()->id, ]); - - $url->update([ - 'destination' => $request->long_url, - 'title' => $request->title, - ]); - - return to_route('dashboard') - ->with('flash_success', __('Link changed successfully !')); } /** - * Delete shortened URLs. - * - * @param Url $url \App\Models\Url - * @return \Illuminate\Http\RedirectResponse + * Show all short URLs created by guest. * - * @throws \Illuminate\Auth\Access\AuthorizationException + * @return \Illuminate\Contracts\View\View */ - public function delete(Url $url) + public function guestLinkView() { - Gate::authorize('forceDelete', $url); - - $url->delete(); - - return redirect()->back() - ->with('flash_success', __('Link was successfully deleted.')); + return view('backend.url-list-of-guest'); } } diff --git a/app/Http/Controllers/UrlController.php b/app/Http/Controllers/UrlController.php index f4fbe5c2c..bfcabd942 100644 --- a/app/Http/Controllers/UrlController.php +++ b/app/Http/Controllers/UrlController.php @@ -7,6 +7,7 @@ use App\Models\User; use App\Models\Visit; use App\Services\QrCodeService; +use Illuminate\Http\Request; use Illuminate\Routing\Controllers\{HasMiddleware, Middleware}; use Illuminate\Support\Facades\Gate; @@ -55,6 +56,49 @@ public function showDetail(Url $url) return view('frontend.short', $data); } + /** + * Show shortened url details page. + * + * @param Url $url \App\Models\Url + * @return \Illuminate\Contracts\View\View + */ + public function edit(Url $url) + { + Gate::authorize('updateUrl', $url); + + return view('backend.edit', ['url' => $url]); + } + + /** + * Update the destination URL. + * + * @param Request $request \Illuminate\Http\Request + * @param Url $url \App\Models\Url + * @return \Illuminate\Http\RedirectResponse + * + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function update(Request $request, Url $url) + { + Gate::authorize('updateUrl', $url); + + $request->validate([ + 'title' => ['max:' . Url::TITLE_LENGTH], + 'long_url' => [ + 'required', 'url', 'max:65535', + new \App\Rules\NotBlacklistedDomain, + ], + ]); + + $url->update([ + 'destination' => $request->long_url, + 'title' => $request->title, + ]); + + return to_route('dashboard') + ->with('flash_success', __('Link changed successfully !')); + } + /** * Delete a shortened URL on user request. * @@ -69,6 +113,12 @@ public function delete(Url $url) $url->delete(); - return to_route('home'); + // if requst from shorten url details page, return to home + if (request()->routeIs('su_delete')) { + return to_route('home'); + } + + return redirect()->back() + ->with('flash_success', __('Link was successfully deleted.')); } } diff --git a/routes/web.php b/routes/web.php index 579be126d..557e0ccad 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,7 +1,6 @@ group(function () { // Dashboard (My URLs) Route::get('/', [DashboardController::class, 'view'])->name('dashboard'); - Route::get('links/{url:keyword}/delete', [DashboardController::class, 'delete'])->name('dboard.url.delete'); - Route::get('links/{url:keyword}/edit', [DashboardController::class, 'edit'])->name('dboard.url.edit.show'); - Route::post('links/{url:keyword}/edit', [DashboardController::class, 'update'])->name('dboard.url.edit.store'); + Route::get('links/{url:keyword}/delete', [UrlController::class, 'delete'])->name('dboard.url.delete'); + Route::get('links/{url:keyword}/edit', [UrlController::class, 'edit'])->name('dboard.url.edit.show'); + Route::post('links/{url:keyword}/edit', [UrlController::class, 'update'])->name('dboard.url.edit.store'); // All URLs - Route::get('/links', [AllUrlController::class, 'view'])->name('dashboard.allurl'); - Route::get('/links/u/guest', [AllUrlController::class, 'guestLinkView'])->name('dashboard.allurl.u-guest'); - Route::get('/links/u/{user:name}', [AllUrlController::class, 'userLinkView'])->name('dashboard.allurl.u-user'); + Route::get('/links', [DashboardController::class, 'allUrlView'])->name('dashboard.allurl'); + Route::get('/links/u/guest', [DashboardController::class, 'guestLinkView'])->name('dashboard.allurl.u-guest'); + Route::get('/links/u/{user:name}', [DashboardController::class, 'userLinkView'])->name('dashboard.allurl.u-user'); // User Route::namespace('User')->prefix('user')->group(function () { diff --git a/tests/Feature/AuthPage/DashboardPageTest.php b/tests/Feature/AuthPage/DashboardPageTest.php index be426f135..3ded93b2b 100644 --- a/tests/Feature/AuthPage/DashboardPageTest.php +++ b/tests/Feature/AuthPage/DashboardPageTest.php @@ -26,7 +26,7 @@ public function canAccessPage(): void /** * Test that an authenticated user can delete a link. * - * @see App\Http\Controllers\Dashboard\DashboardController::delete() + * @see App\Http\Controllers\UrlController::delete() */ #[PHPUnit\Test] public function canDelete(): void @@ -45,7 +45,7 @@ public function canDelete(): void /** * Test that an authorized user can access the edit page. * - * @see App\Http\Controllers\Dashboard\DashboardController::edit() + * @see App\Http\Controllers\UrlController::edit() */ #[PHPUnit\Test] public function canAccessEditLinkPage(): void @@ -59,7 +59,7 @@ public function canAccessEditLinkPage(): void /** * Test that an authorized user can update a link. * - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ #[PHPUnit\Test] public function canUpdateLink(): void @@ -80,7 +80,7 @@ public function canUpdateLink(): void } /** - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ public function test_update_validates_title_length(): void { @@ -98,7 +98,7 @@ public function test_update_validates_title_length(): void } /** - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ public function test_update_validates_long_url_is_url(): void { @@ -116,7 +116,7 @@ public function test_update_validates_long_url_is_url(): void } /** - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ public function test_update_validates_long_url_max_length(): void { @@ -134,7 +134,7 @@ public function test_update_validates_long_url_max_length(): void } /** - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ public function test_update_validates_long_url_not_blacklisted() { diff --git a/tests/Feature/AuthPage/UrlListPageSingleUserTest.php b/tests/Feature/AuthPage/UrlListPageSingleUserTest.php index 30eca476c..208190938 100644 --- a/tests/Feature/AuthPage/UrlListPageSingleUserTest.php +++ b/tests/Feature/AuthPage/UrlListPageSingleUserTest.php @@ -13,7 +13,7 @@ class UrlListPageSingleUserTest extends TestCase /** * Admin can access user links and guest links table page. * - * @see App\Http\Controllers\Dashboard\AllUrlController::userLinkView() + * @see App\Http\Controllers\Dashboard\DashboardController::userLinkView() */ #[PHPUnit\Test] public function adminCanAccessUserLinksTablePage(): void @@ -28,7 +28,7 @@ public function adminCanAccessUserLinksTablePage(): void /** * Non admin users can't access user links and guest links table page. * - * @see App\Http\Controllers\Dashboard\AllUrlController::userLinkView() + * @see App\Http\Controllers\Dashboard\DashboardController::userLinkView() */ #[PHPUnit\Test] public function basicUsersCantAccessUserLinksTablePage(): void @@ -43,7 +43,7 @@ public function basicUsersCantAccessUserLinksTablePage(): void /** * Test that an admin user can access the URL list page of a guest user. * - * @see App\Http\Controllers\Dashboard\AllUrlController::guestLinkView() + * @see App\Http\Controllers\Dashboard\DashboardController::guestLinkView() */ #[PHPUnit\Test] public function adminCanAccessTheUrlListPageOfAGuestUser(): void @@ -56,7 +56,7 @@ public function adminCanAccessTheUrlListPageOfAGuestUser(): void /** * Non admin users can't access guest links table page. * - * @see App\Http\Controllers\Dashboard\AllUrlController::guestLinkView() + * @see App\Http\Controllers\Dashboard\DashboardController::guestLinkView() */ #[PHPUnit\Test] public function basicUsersCantAccessTheUrlListPageOfAGuestUser(): void diff --git a/tests/Feature/AuthPage/UrlListPageTest.php b/tests/Feature/AuthPage/UrlListPageTest.php index 9b9947669..ef2d915ae 100644 --- a/tests/Feature/AuthPage/UrlListPageTest.php +++ b/tests/Feature/AuthPage/UrlListPageTest.php @@ -13,7 +13,7 @@ class UrlListPageTest extends TestCase /** * Admin users can access the link table page. * - * @see App\Http\Controllers\Dashboard\AllUrlController::view() + * @see App\Http\Controllers\Dashboard\DashboardController::allUrlView() */ #[PHPUnit\Test] public function adminCasAccessLinkTablePage(): void @@ -26,7 +26,7 @@ public function adminCasAccessLinkTablePage(): void /** * Normal users can't access the link table page. * - * @see App\Http\Controllers\Dashboard\AllUrlController::view() + * @see App\Http\Controllers\Dashboard\DashboardController::allUrlView() */ #[PHPUnit\Test] public function basicUserCantAccessLinkTablePage(): void @@ -44,7 +44,7 @@ public function basicUserCantAccessLinkTablePage(): void * database, and confirms that the operation is successful by checking for * a redirect response and a success flash message. * - * @see App\Http\Controllers\Dashboard\DashboardController::delete() + * @see App\Http\Controllers\UrlController::delete() */ #[PHPUnit\Test] public function adminCanDelete(): void @@ -62,7 +62,7 @@ public function adminCanDelete(): void /** * Normal users can't delete other users' URLs. * - * @see App\Http\Controllers\Dashboard\DashboardController::delete() + * @see App\Http\Controllers\UrlController::delete() */ #[PHPUnit\Test] public function basicUserCantDelete(): void @@ -83,7 +83,7 @@ public function basicUserCantDelete(): void * of another user, verifies that the operation is successful by checking for * a successful response. * - * @see App\Http\Controllers\Dashboard\DashboardController::edit() + * @see App\Http\Controllers\UrlController::edit() */ #[PHPUnit\Test] public function adminCanAccessOtherUsersLinkEditPage(): void @@ -101,7 +101,7 @@ public function adminCanAccessOtherUsersLinkEditPage(): void * of a guest user, verifies that the operation is successful by checking for * a successful response. * - * @see App\Http\Controllers\Dashboard\DashboardController::edit() + * @see App\Http\Controllers\UrlController::edit() */ #[PHPUnit\Test] public function adminCanAccessGuestUsersLinkEditPage(): void @@ -115,7 +115,7 @@ public function adminCanAccessGuestUsersLinkEditPage(): void /** * Basic users can't access other users' link edit page. * - * @see App\Http\Controllers\Dashboard\DashboardController::edit() + * @see App\Http\Controllers\UrlController::edit() */ #[PHPUnit\Test] public function basicUserCantAccessOtherUsersLinkEditPage(): void @@ -129,7 +129,7 @@ public function basicUserCantAccessOtherUsersLinkEditPage(): void /** * Admin can update another user's link. * - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ #[PHPUnit\Test] public function adminCanUpdateOtherUsersLink(): void @@ -156,7 +156,7 @@ public function adminCanUpdateOtherUsersLink(): void * verifies that the operation is forbidden by checking for a forbidden response, * and confirms that the link is not updated. * - * @see App\Http\Controllers\Dashboard\DashboardController::update() + * @see App\Http\Controllers\UrlController::update() */ #[PHPUnit\Test] public function basicUserCantUpdateOtherUsersLink(): void