Arkadaşlar selamlar spatieyi kurdum entegre ettim şöle bir sorunum var create sayfasını yonetici ve moderator görücek fakat superadmin göremiycek ama herneyaptıysam 403 hatası alıyorum policyide tanımladım kodları aşağıya atıyorum böle bir hata alıyorum : 403
This action is unauthorized.
kodlar:
bootstrap/app.php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\Admin;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
$middleware->alias([
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
]);
$middleware->appendToGroup('Admin', [
'Admin' => Admin::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
// Gate ve Policy tanımlamalarını yapın.
$app->register(App\Providers\AuthServiceProvider::class);
YetkiPolicy.php dosyası:
<?php
namespace App\Policies;
use App\Models\User;
use App\Models\Role;
use App\Models\Hasrole;
use App\Models\Permission;
class YetkiPolicy
{
/**
* Create a new policy instance.
*/
public function __construct()
{
//
}
public function index(User $user)
{
return $user->hasAnyRole(['yonetici','moderator','superadmin']);
}
public function SeemaskYonetici(User $user){
return $user->hasAnyRole(['yonetici']);
}
public function SeemaskModerator(User $user){
return $user->hasAnyRole(['yonetici','moderator']);
}
public function store(User $user)
{
return $user->hasAnyRole(['yonetici', 'moderator']);
}
public function update(User $user){
return $user->hasAnyRole(['yonetici','moderator']);
}
public function destroy(User $user){
return $user->hasAnyRole(['yonetici']);
}
public function Haspost(User $user)
{
return $user->hasRole('yonetici');
}
public function create(User $user)
{
// Eğer kullanıcı süper adminse, yetki vermiyoruz
if ($user->hasRole('superadmin')) {
return false; // Süper admin create sayfasını göremez
}
// 'yonetici' veya 'moderator' rolüne sahip kullanıcılar için yetki ver
return $user->hasAnyRole(['yonetici', 'moderator']);
}
}
MonsterController.php dosyası:
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Monster;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
class MonsterController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
Gate::authorize('create', User::class);
return view('backend.monster.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
AuthServiceProvider dosyası:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\User;
use App\Policies\YetkiPolicy;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}
/**
* Bootstrap services.
*/
public function boot(): void
{
//
Gate::policy(User::class, YetkiPolicy::class);
}
}
middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class Admin
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
// Kullanıcı 'süperadmin' ise, admin paneline girebilir
if (Auth::user()->hasRole('superadmin')) {
return $next($request);
}
// Kullanıcı 'yonetici' veya 'moderator' ise admin paneline girebilir
if (Auth::user()->hasAnyRole(['yonetici', 'moderator'])) {
return $next($request);
}
}
// Kullanıcı yetkisi yoksa giriş sayfasına yönlendir
return redirect('admin/Adminlogin');
}
}