main

filamentphp/demo

Last updated at: 29/12/2023 09:43

User.php

TLDR

This file contains the User class, which extends Authenticatable and implements several interfaces. It defines methods related to user authorization and accessing tenants.

Methods

canAccessPanel

This method takes a Panel object as a parameter and returns true. It checks if the user can access the given panel.

canAccessTenant

This method takes a Model object representing a tenant as a parameter and returns true. It checks if the user can access the given tenant.

getTenants

This method takes a Panel object as a parameter and returns an array or Collection containing all the tenants. It fetches the tenants from the Team model.

Classes

<?php

namespace App\Models;

use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements FilamentUser, HasTenants, MustVerifyEmail
{
    use HasApiTokens;
    use HasFactory;
    use Notifiable;

    /**
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function canAccessPanel(Panel $panel): bool
    {
        return true;
    }

    public function canAccessTenant(Model $tenant): bool
    {
        return true;
    }

    public function getTenants(Panel $panel): array | Collection
    {
        return Team::all();
    }
}