Use Policies only for companies Panel, not other

Friends, I have a problem, I am working on a tenancy app with integration of roles and permissions, for the companies panel everything works ok, the problem is that when entering the administration panel the app blocks and does not allow me to view the information, this happens because I am using within the administration panel, the same Model as in Companies, I am referring to the User model, I was looking for an exclusive policy that could be used for said panels, and in chatgpt it suggests this example, but it still doesn't work for me. works. This is my Policies for Companies Panel
<?php

namespace App\Policies;

use App\Models\User;

class UserPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->hasRole('Administrador');
}

}
<?php

namespace App\Policies;

use App\Models\User;

class UserPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->hasRole('Administrador');
}

}
2 Replies
TranceCode
TranceCodeOP12mo ago
this is my Policies only for AdminPanel
<?php

namespace App\Policies;

use App\Models\User;

class AdminPanelPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
// Permitir acceso a todos los usuarios para el panel de administración
return true;
}
}
<?php

namespace App\Policies;

use App\Models\User;

class AdminPanelPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
// Permitir acceso a todos los usuarios para el panel de administración
return true;
}
}
and this is my panel
<?php

namespace App\Providers\Filament;

use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

use App\Policies\AdminPanelPolicy; // Importa la nueva política
<?php

namespace App\Providers\Filament;

use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

use App\Policies\AdminPanelPolicy; // Importa la nueva política
class SuperadminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->id('superadmin')
->path('admin')
->colors([
'primary' => Color::Green,
])
->login()
->passwordReset()
->authGuard('web')
->maxContentWidth('full')
->sidebarCollapsibleOnDesktop()
->discoverResources(in: app_path('Filament/Superadmin/Resources'), for: 'App\\Filament\\Superadmin\\Resources')
->discoverPages(in: app_path('Filament/Superadmin/Pages'), for: 'App\\Filament\\Superadmin\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Superadmin/Widgets'), for: 'App\\Filament\\Superadmin\\Widgets')
->widgets([
Widgets\AccountWidget::class,
//Widgets\FilamentInfoWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
'can:viewAny,' . AdminPanelPolicy::class, // Asociar la política al middleware de autorización
]);
}
}
class SuperadminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->id('superadmin')
->path('admin')
->colors([
'primary' => Color::Green,
])
->login()
->passwordReset()
->authGuard('web')
->maxContentWidth('full')
->sidebarCollapsibleOnDesktop()
->discoverResources(in: app_path('Filament/Superadmin/Resources'), for: 'App\\Filament\\Superadmin\\Resources')
->discoverPages(in: app_path('Filament/Superadmin/Pages'), for: 'App\\Filament\\Superadmin\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Superadmin/Widgets'), for: 'App\\Filament\\Superadmin\\Widgets')
->widgets([
Widgets\AccountWidget::class,
//Widgets\FilamentInfoWidget::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
'can:viewAny,' . AdminPanelPolicy::class, // Asociar la política al middleware de autorización
]);
}
}
this return me an error TypeError PHP 8.2.4 10.33.0 App\Policies\UserPolicy::viewAny(): Argument #1 ($user) must be of type App\Models\User, App\Models\Admin given, called in C:\Users\mikey\Documents\myfit-multitenancy\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 811
DrByte
DrByte12mo ago
App\Policies\UserPolicy::viewAny(): Argument #1 ($user) must be of type App\Models\User, App\Models\Admin given public function viewAny(User $user): bool
The error message shows that it's looking for an Admin model, not a User model. And, if you look at your code, in your Policy's typehint that's because you're expecting a User model, not Admin . If your models are only Admin then you will need to change that typehint accordingly.
Want results from more Discord servers?
Add your server