diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php index 3ad7d7a..1f848eb 100644 --- a/app/Actions/Fortify/CreateNewUser.php +++ b/app/Actions/Fortify/CreateNewUser.php @@ -36,7 +36,7 @@ public function create(array $input): User Rule::unique(User::class), ], 'password' => $this->passwordRules(), - 'role' => ['required', 'string', Rule::in(['tenant', 'buyer', 'seller', 'landlord', 'contractor'])], + // 'role' => ['required', 'string', Rule::in(['tenant', 'buyer', 'seller', 'landlord', 'contractor'])], ])->validate(); @@ -49,25 +49,10 @@ public function create(array $input): User $team = $this->assignOrCreateTeam($user); $user->switchTeam($team); setPermissionsTeamId($team->id); - $user->assignRole($input['role']); + $user->assignRole('free'); }); }); - // $user = DB::transaction(function () use ($input) { - // return tap(, - // , function (User $user) use ($input) { - // $team = $this->assignOrCreateTeam($user); - // $user->switchTeam($team); - // setPermissionsTeamId($team->id); - // $user->assignRole($input['role']); - // }); - // }); - - Log::info('User created successfully', [ - 'user_id' => $user->id, - 'email' => $user->email, - 'role' => $input['role'], - ]); - + return $user; } catch (\Illuminate\Validation\ValidationException $e) { Log::error('User creation validation failed', [ diff --git a/app/Models/Content.php b/app/Models/Content.php index 8485aba..a428781 100644 --- a/app/Models/Content.php +++ b/app/Models/Content.php @@ -1,3 +1,11 @@ +id}"); + }); } public function author() @@ -41,17 +53,11 @@ public function author() return $this->belongsTo(User::class, 'author_id'); } - public static function boot() - { - parent::boot(); - static::saved(function ($content) { - Cache::forget("content_{$content->id}"); - }); - } - public static function findCached($id) { return Cache::remember("content_{$id}", now()->addHours(24), function () use ($id) { return static::find($id); }); - } \ No newline at end of file + } + +} \ No newline at end of file diff --git a/app/Models/User.php b/app/Models/User.php index 82070a0..fdb789d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,6 +3,10 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; + +use Filament\Models\Contracts\FilamentUser; +use Filament\Models\Contracts\HasDefaultTenant; +use Filament\Models\Contracts\HasTenants; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -11,15 +15,23 @@ use Laravel\Jetstream\HasTeams; use Laravel\Sanctum\HasApiTokens; use Filament\Panel; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Support\Collection; +use JoelButcher\Socialstream\SetsProfilePhotoFromUrl; use Spatie\Permission\Traits\HasRoles; -class User extends Authenticatable +class User extends Authenticatable implements HasDefaultTenant, HasTenants, FilamentUser { use HasApiTokens; use HasFactory; use HasProfilePhoto; - use HasTeams; + use HasProfilePhoto { + HasProfilePhoto::profilePhotoUrl as getPhotoUrl; + } use Notifiable; + use SetsProfilePhotoFromUrl; + use HasTeams; use TwoFactorAuthenticatable; use HasRoles; @@ -68,8 +80,39 @@ protected function casts(): array ]; } - public function getTenants(Panel $panel) + public function getTenants(Panel $panel): array | Collection { return $this->teams; } + + public function canAccessPanel(Panel $panel): bool + { + $user = auth()->user(); + if ($panel->getId() === "admin" && !$user->hasRole('admin')) { + return false; + } + + return true; // TODO: Check panel and role + } + + public function canAccessTenant(Model $tenant): bool + { + return $this->teams->contains($tenant); + } + + public function canAccessFilament(): bool + { + // return $this->hasVerifiedEmail(); + return true; + } + + public function getDefaultTenant(Panel $panel): ?Model + { + return $this->latestTeam; + } + + public function latestTeam(): BelongsTo + { + return $this->belongsTo(Team::class, 'current_team_id'); + } }