*/ use HasFactory, Notifiable, TwoFactorAuthenticatable, SoftDeletes, HasApiTokens, HasRoles; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ 'cooperative_id', 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } /** * Get the user's initials */ public function initials(): string { return Str::of($this->name) ->explode(' ') ->take(2) ->map(fn($word) => Str::substr($word, 0, 1)) ->implode(''); } /** * Get the cooperative that owns the user. */ public function cooperative(): BelongsTo { return $this->belongsTo(Cooperative::class); } /** * Get the member associated with the user. */ public function member(): HasOne { return $this->hasOne(Member::class); } /** * Filament User interface methods */ public function canAccessPanel(Panel $panel): bool { return true; // Allow all users to access the panel } /** * Filament Tenancy interface methods */ public function getTenants(Panel $panel): Collection { // Super admin (cooperative_id = null) can access all cooperatives if ($this->isSuperAdmin()) { return Cooperative::all(); } // Regular user can only access their cooperative return collect([$this->cooperative])->filter(); } public function canAccessTenant(Model $tenant): bool { // Super admin can access any cooperative if ($this->isSuperAdmin()) { return true; } // Regular user can only access their own cooperative return $this->cooperative_id === $tenant->id; } /** * Check if user is super admin */ public function isSuperAdmin(): bool { return $this->cooperative_id === null; } /** * Override roles relationship for multi-tenancy */ public function roles() { // Get team_id dari context saat ini $teamId = getPermissionsTeamId(); // Jika tidak ada team_id dari context, gunakan 0 (global roles) if (!$teamId) { $teamId = 0; } return $this->morphToMany( config('permission.models.role'), 'model', config('permission.table_names.model_has_roles'), config('permission.column_names.model_morph_key'), config('permission.column_names.role_pivot_key') )->wherePivot('team_id', $teamId); } }