Skip to content

Commit

Permalink
Merge 7094c59 into 687fe74
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Feb 17, 2023
2 parents 687fe74 + 7094c59 commit 3e8dfe0
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 170 deletions.
6 changes: 3 additions & 3 deletions app/Http/Controllers/Dashboard/AllUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function view()
/**
* Delete a Short URL on user (Admin) request.
*
* @param Url $url \App\Models\Url
* @param Url $hash_id \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(Url $url)
public function delete(Url $hash_id)
{
$url->delete();
$hash_id->delete();

return redirect()->back()
->withFlashSuccess(__('Link was successfully deleted.'));
Expand Down
14 changes: 7 additions & 7 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public function edit(string $urlKey)
* Update the destination URL
*
* @param Request $request \Illuminate\Http\Request
* @param Url $url \App\Models\Url
* @param Url $hash_id \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(Request $request, Url $url)
public function update(Request $request, Url $hash_id)
{
$this->uHubLinkService->update($request, $url);
$this->uHubLinkService->update($request, $hash_id);

return to_route('dashboard')
->withFlashSuccess(__('Link changed successfully !'));
Expand All @@ -67,16 +67,16 @@ public function update(Request $request, Url $url)
/**
* Delete shortened URLs
*
* @param Url $url \App\Models\Url
* @param Url $hash_id \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete(Url $url)
public function delete(Url $hash_id)
{
$this->authorize('forceDelete', $url);
$this->authorize('forceDelete', $hash_id);

$url->delete();
$hash_id->delete();

return redirect()->back()
->withFlashSuccess(__('Link was successfully deleted.'));
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/Dashboard/User/ChangePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public function view(User $user)
* Change the password.
*
* @param UpdateUserPassword $request \App\Http\Requests\UpdateUserPassword
* @param User $user \App\Models\User
* @param User $hash_id \App\Models\User
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(UpdateUserPassword $request, User $user)
public function update(UpdateUserPassword $request, User $hash_id)
{
$this->authorize('updatePass', $user);
$this->authorize('updatePass', $hash_id);

$user->password = Hash::make($request['new-password']);
$user->save();
$hash_id->password = Hash::make($request['new-password']);
$hash_id->save();

return redirect()->back()
->withFlashSuccess(__('Password changed successfully !'));
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/Dashboard/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public function edit(User $user)
* Update the specified user in storage.
*
* @param UpdateUserEmail $request \App\Http\Requests\UpdateUserEmail
* @param User $user \App\Models\User
* @param User $hash_id \App\Models\User
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(UpdateUserEmail $request, User $user)
public function update(UpdateUserEmail $request, User $hash_id)
{
$this->authorize('update', $user);
$this->authorize('update', $hash_id);

$user->email = $request->email;
$user->save();
$hash_id->email = $request->email;
$hash_id->save();

return redirect()->back()
->withFlashSuccess(__('Profile updated.'));
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ public function showDetail(string $urlKey)
/**
* Delete a shortened URL on user request.
*
* @param Url $url \App\Models\Url
* @param Url $hash_id \App\Models\Url
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete(Url $url)
public function delete(Url $hash_id)
{
$this->authorize('forceDelete', $url);
$this->authorize('forceDelete', $hash_id);

$url->delete();
$hash_id->delete();

return to_route('home');
}
Expand Down
9 changes: 2 additions & 7 deletions app/Models/Traits/Hashidable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

namespace App\Models\Traits;

use Vinkla\Hashids\Facades\Hashids;

trait Hashidable
{
public function getRouteKey()
public function getRouteKey(): string
{
/** @var \Vinkla\Hashids\Facades\Hashids */
$hashids = Hashids::connection(get_called_class());

return $hashids->encode($this->getKey());
return encrypt($this->getKey());
}
}
21 changes: 2 additions & 19 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Vinkla\Hashids\Facades\Hashids;

class RouteServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -68,24 +67,8 @@ private function routeModelBinding()
return User::whereName($value)->firstOrFail();
});

Route::bind('user_hashId', function (string $value) {
return $this->hashidsDecoder(User::class, $value);
Route::bind('hash_id', function (string $value) {
return decrypt($value);
});

Route::bind('su_hashId', function (string $value) {
return $this->hashidsDecoder(\App\Models\Url::class, $value);
});
}

/**
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
*/
private function hashidsDecoder(string $model, string $routeKey)
{
/** @var \Vinkla\Hashids\Facades\Hashids */
$hashids = Hashids::connection($model);
$id = $hashids->decode($routeKey)[0] ?? null;

return resolve($model)->findOrFail($id);
}
}
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"paragonie/random_compat": "^2.0",
"power-components/livewire-powergrid": "^3.7",
"spatie/laravel-permission": "^5.9",
"spatie/url": "^2.2",
"vinkla/hashids": "^10.0"
"spatie/url": "^2.2"
},
"require-dev": {
"fakerphp/faker": "^1.21",
Expand Down
58 changes: 0 additions & 58 deletions config/hashids.php

This file was deleted.

10 changes: 0 additions & 10 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
<code>whereDestination</code>
</UndefinedMagicMethod>
</file>
<file src="app/Models/Traits/Hashidable.php">
<UndefinedMethod>
<code>encode</code>
</UndefinedMethod>
</file>
<file src="app/Providers/RouteServiceProvider.php">
<UndefinedMethod>
<code>decode</code>
</UndefinedMethod>
</file>
<file src="app/Services/KeyGeneratorService.php">
<UndefinedMagicMethod>
<code>exists</code>
Expand Down
12 changes: 6 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
Route::view('/', 'frontend.homepage')->name('home');
Route::post('/shorten', [UrlController::class, 'create'])->name('su_create');
Route::get('/+{keyword}', [UrlController::class, 'showDetail'])->name('su_detail');
Route::get('/delete/{su_hashId}', [UrlController::class, 'delete'])->name('su_delete');
Route::get('/delete/{hash_id}', [UrlController::class, 'delete'])->name('su_delete');
Route::get('/duplicate/{keyword}', [UrlController::class, 'duplicate'])->middleware('auth')->name('su_duplicate');

Route::namespace('Dashboard')->prefix('admin')->group(function () {
Route::middleware('auth')->group(function () {
// Dashboard (My URLs)
Route::get('/', [DashboardController::class, 'view'])->name('dashboard');
Route::get('/delete/{su_hashId}', [DashboardController::class, 'delete'])->name('dashboard.su_delete');
Route::get('/delete/{hash_id}', [DashboardController::class, 'delete'])->name('dashboard.su_delete');
Route::get('/duplicate/{keyword}', [DashboardController::class, 'duplicate'])->name('dashboard.su_duplicate');
Route::get('/edit/{keyword}', [DashboardController::class, 'edit'])->name('dashboard.su_edit');
Route::post('/edit/{su_hashId}', [DashboardController::class, 'update'])->name('dashboard.su_edit.post');
Route::post('/edit/{hash_id}', [DashboardController::class, 'update'])->name('dashboard.su_edit.post');

// All URLs
Route::get('/allurl', [AllUrlController::class, 'view'])->name('dashboard.allurl');
Route::get('/allurl/delete/{su_hashId}', [AllUrlController::class, 'delete'])->name('dashboard.allurl.su_delete');
Route::get('/allurl/delete/{hash_id}', [AllUrlController::class, 'delete'])->name('dashboard.allurl.su_delete');

// User
Route::namespace('User')->prefix('user')->group(function () {
Route::get('/', [UserController::class, 'view'])->name('user.index');
Route::get('{user}/edit', [UserController::class, 'edit'])->name('user.edit');
Route::post('{user_hashId}/edit', [UserController::class, 'update'])->name('user.update');
Route::post('{hash_id}/edit', [UserController::class, 'update'])->name('user.update');

Route::get('{user}/changepassword', [ChangePasswordController::class, 'view'])->name('user.change-password');
Route::post('{user_hashId}/changepassword', [ChangePasswordController::class, 'update'])->name('user.change-password.post');
Route::post('{hash_id}/changepassword', [ChangePasswordController::class, 'update'])->name('user.change-password.post');
});
});
});
Expand Down
12 changes: 2 additions & 10 deletions tests/Feature/AuthPage/AllUrlsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

use App\Models\Url;
use Tests\TestCase;
use Vinkla\Hashids\Facades\Hashids;

class AllUrlsPageTest extends TestCase
{
protected function hashIdRoute($routeName, $url_id)
{
$hashids = Hashids::connection(Url::class);

return route($routeName, $hashids->encode($url_id));
}

/**
* @test
* @group f-allurl
Expand Down Expand Up @@ -49,7 +41,7 @@ public function auAdminCanDelete()

$response = $this->actingAs($this->adminUser())
->from(route('dashboard.allurl'))
->get($this->hashIdRoute('dashboard.allurl.su_delete', $url->id));
->get($this->secureRoute('dashboard.allurl.su_delete', $url->id));

$response->assertRedirectToRoute('dashboard.allurl')
->assertSessionHas('flash_success');
Expand All @@ -67,7 +59,7 @@ public function auNormalUserCantDelete()

$response = $this->actingAs($this->normalUser())
->from(route('dashboard.allurl'))
->get($this->hashIdRoute('dashboard.allurl.su_delete', $url->id));
->get($this->secureRoute('dashboard.allurl.su_delete', $url->id));

$response->assertForbidden();
$this->assertCount(1, Url::all());
Expand Down
12 changes: 2 additions & 10 deletions tests/Feature/AuthPage/DashboardPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

use App\Models\Url;
use Tests\TestCase;
use Vinkla\Hashids\Facades\Hashids;

class DashboardPageTest extends TestCase
{
protected function hashIdRoute($routeName, $url_id)
{
$hashids = Hashids::connection(Url::class);

return route($routeName, $hashids->encode($url_id));
}

/**
* @test
* @group f-dashboard
Expand All @@ -37,7 +29,7 @@ public function dCanDelete()

$response = $this->actingAs($url->author)
->from(route('dashboard'))
->get($this->hashIdRoute('dashboard.su_delete', $url->id));
->get($this->secureRoute('dashboard.su_delete', $url->id));

$response
->assertRedirectToRoute('dashboard')
Expand Down Expand Up @@ -91,7 +83,7 @@ public function dCanUpdateUrl()

$response = $this->actingAs($url->author)
->from(route('dashboard.su_edit', $url->keyword))
->post($this->hashIdRoute('dashboard.su_edit.post', $url->id), [
->post($this->secureRoute('dashboard.su_edit.post', $url->id), [
'title' => $url->title,
'long_url' => $newLongUrl,
]);
Expand Down
5 changes: 1 addition & 4 deletions tests/Feature/AuthPage/User/ChangePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
use Vinkla\Hashids\Facades\Hashids;

class ChangePasswordTest extends TestCase
{
Expand All @@ -15,9 +14,7 @@ protected function getRoute($value)

protected function postRoute($value)
{
$hashids = Hashids::connection(\App\Models\User::class);

return route('user.change-password.post', $hashids->encode($value));
return $this->secureRoute('user.change-password.post', $value);
}

/**
Expand Down

0 comments on commit 3e8dfe0

Please sign in to comment.