Skip to content

Commit

Permalink
If user has no permission to route do not create url
Browse files Browse the repository at this point in the history
  • Loading branch information
petrzpav committed Nov 13, 2024
1 parent 21f8371 commit c93d47d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace InternetGuru\LaravelCommon\Support;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -81,8 +82,20 @@ public static function parseUrlPath(string $homeRoute = 'home', int $skipFirst =
foreach ($urlParts as $index => $segment) {
$currentPath = $currentPath == '/' ? $currentPath . $segment : $currentPath . '/' . $segment;
try {
$routeName = Route::getRoutes()->match(request()->create($currentPath))->getName();
$route = Route::getRoutes()->match(request()->create($currentPath));
$routeName = $route->getName();
$uri = $currentPath;
foreach ($route->middleware() as $item) {
if (strpos($item, 'can:') === 0) {
[$permission, $model] = explode(',', substr($item, 4));
$parameters = $route->parameters();
$modelInstance = array_key_exists($model, $parameters) ? $parameters[$model] : app($model);
if (! Gate::allows($permission, $modelInstance)) {
// If user does not have permission, return the route name and empty URI
$uri = '';
}
}
}
} catch (NotFoundHttpException $e) {
$routeName = $segment;
$uri = '';
Expand Down

0 comments on commit c93d47d

Please sign in to comment.