php artisan make:middleware IsAdminMiddleware
public function handle(Request $request, Closure $next)
{
if(!auth()->check() || !auth()->user()->is_admin) {
abort(403);
}
return $next($request);
}
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
...
'is_admin' => \App\Http\Middleware\IsAdminMiddleware::class,
];
<?
Route::group(['middleware' => 'auth'], function() {
Route::group([
'prefix' => 'admin',
'middleware' => 'is_admin',
'as' => 'admin.',
], function() {
Route::get('tasks', [\App\Http\Controllers\Admin\TaskController::class, 'index'])->name('tasks.index');
});
Route::group([
'prefix' => 'user',
'as' => 'user.',
], function() {
Route::get('tasks', [\App\Http\Controllers\User\TaskController::class, 'index'])->name('tasks.index');
});
});
@if(auth()->user()->is_admin)
<x-nav-link :href="route('admin.tasks.index')" :active="request()->routeIs('admin.tasks.index')">
{{ __('All Tasks') }}
</x-nav-link>
@else
<x-nav-link :href="route('user.tasks.index')" :active="request()->routeIs('user.tasks.index')">
{{ __('My Tasks') }}
</x-nav-link>
@endif
<?php
namespace App\Http\Controllers\Admin;
use ...
class TaskController extends Controllers
{
public function index()
{
$tasks = Task::with('user')->orderBy('due_date')->get();
return view('admin.tasks.index', compact('tasks'));
}
}
\User\TaskController.php
<?php
namespace App\Http\Controllers\User;
use ...
class TaskController extends Controllers
{
public function index()
{
$tasks = auth()->user()->tasks;
return view('user.tasks.index', compact('tasks'));
}
}
@can('tasks_create')
<a href="{{ route('tasks.create') }}">Add new Task</a>
@endcan
public function boot()
{
$this->registerPolicies();
Gate::define('tasks_create', function(User $user) { return $user->is_admin; }
Gate::define('tasks_edit', function(User $user) { return $user->is_admin; }
Gate::define('tasks_delete', function(User $user) { return $user->is_admin; }
}
// fonksiyon true dönen kişi bunu yapabilir oluyor.
$this->authorize('gate_name')
dememiz lazım.
// TaskController
public function create()
{
$this->authorize('tasks_create');
return view('tasks.create');
}
php artisan make:policy TaskPolicy
demek. Böyle yapınca app > Policy içersine TaskPolicy dosyası oluşur.php artisan make:policy TaskPolicy --model=task
Önceden Gate ile yaptığımız şeyleri daha kullanışlı biçimde Policy içersine ekleyebiliriz.
class TaskPolicy
{
public function create(User $user)
{
return $user->is_admin;
}
public function update(User $user, Task $task)
{
return $user->is_admin || (auth()->check() && $task->user_id == auth()->id());
}
public function delete(User $user, Task $task)
{
return $user->is_admin || (auth()->check() && $task->user_id == auth()->id());
}
}
public function create()
{
$this->authorize('create', Task::class);
return view('tasks.create');
}
public function store(Request $request)
{
$this->authorize('create', Task::class);
Task::create($request->only('description','due_date'));
return redirect()->route('tasks.index');
}
public function edit(Task $task)
{
$this->authorize('create', $task);
$task->update($request->only('description','due_date'));
return redirect()->route('tasks.index');
}
@can('update', $task)
<a href="{{ route('tasks.update', $task) }}">Update Task</a>
@endcan