This commit is contained in:
Saeed Vaziry
2024-09-27 20:36:03 +02:00
committed by GitHub
parent b62c40c97d
commit f6bc04763b
122 changed files with 6609 additions and 807 deletions

View File

@ -3,8 +3,13 @@
namespace App\Models;
use App\Enums\UserRole;
use App\Traits\HasTimezoneTimestamps;
use Carbon\Carbon;
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
@ -34,10 +39,13 @@
* @property Project $currentProject
* @property Collection<Project> $projects
* @property string $role
* @property Carbon $created_at
* @property Carbon $updated_at
*/
class User extends Authenticatable
class User extends Authenticatable implements HasTenants
{
use HasFactory;
use HasTimezoneTimestamps;
use Notifiable;
use TwoFactorAuthenticatable;
@ -111,6 +119,16 @@ public function projects(): BelongsToMany
return $this->belongsToMany(Project::class, 'user_project')->withTimestamps();
}
public function getTenants(Panel $panel): Collection
{
return $this->projects;
}
public function canAccessTenant(Model $tenant): bool
{
return $this->projects()->whereKey($tenant)->exists();
}
public function currentProject(): HasOne
{
return $this->HasOne(Project::class, 'id', 'current_project_id');
@ -121,7 +139,7 @@ public function createDefaultProject(): Project
$project = $this->projects()->first();
if (! $project) {
$project = new Project();
$project = new Project;
$project->name = 'default';
$project->save();