Loheide-eu-mitarbeiterplatt.../app/Models/User.php
Friederich Loheide 51c94cd0cf
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m37s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m10s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m2s
CI / Build-Artefakte aktuell (pull_request) Successful in 52s
Anmeldung mit Microsoft Entra ID
- OpenID Connect mit PKCE, State und Nonce; ID-Token-Prüfung gegen die
  Microsoft-Schlüssel (Signatur, Mandant, App, Gültigkeit, Nonce) inkl.
  Neuladen bei Schlüsselwechsel
- Konto bei erster Anmeldung anlegen oder per E-Mail verknüpfen, optionale
  Domain-Beschränkung, deaktivierte Konten bleiben gesperrt
- Gruppen (inkl. Overage über Graph, transitiv) und App-Rollen → Rollen,
  additiv; nur Zuweisungen aus Entra werden abgeglichen, manuelle bleiben
- Profilfelder, Vorgesetzte*r und Profilfoto aus Graph, je Feld wählbar und
  im Portal schreibgeschützt; Fallback auf Token-Angaben, wenn Graph ausfällt
- Hintergrund-Abgleich mit Delta-Abfragen (Zeitbudget, Fortsetzung über
  nextLink/deltaLink), Offboarding deaktivierter/gelöschter Konten, optionale
  Vorab-Anlage, Fotos/Vorgesetzte rollierend, Gruppen und App-Rollen stündlich
- Admin „Anmeldung“: Einrichtung mit Umleitungs-URI und Berechtigungen,
  verschlüsseltes Geheimnis (nie im Browser), Gruppensuche, Status, „Mapping testen“
- Fix: @odata.nextLink/@odata.deltaLink wurden als Pfad gelesen – Folgeseiten fehlten
- Profilseite mit Vorgesetzter/m und Mitarbeitenden; Version 0.4.0

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 07:52:22 +00:00

187 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use App\Support\Avatars;
use App\Support\Permissions\Permissions;
use App\Support\Permissions\PermissionSet;
use Database\Factories\UserFactory;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasAvatar;
use Filament\Panel;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
#[Fillable([
'name', 'first_name', 'last_name', 'email', 'password',
'job_title', 'department', 'location', 'phone', 'mobile', 'about',
'is_active', 'theme',
])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable implements FilamentUser, HasAvatar
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
public const THEMES = ['system', 'light', 'dark'];
protected static function booted(): void
{
// Der Anzeigename folgt Vor- und Nachname, solange beide gepflegt sind.
static::saving(function (User $user): void {
$fullName = trim($user->first_name.' '.$user->last_name);
if ($fullName !== '') {
$user->name = $fullName;
}
});
}
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'last_login_at' => 'datetime',
'password' => 'hashed',
'is_active' => 'boolean',
];
}
/**
* Admin-Backend: für alle mit irgendeinem Recht (z. B. Redaktion einer
* Kategorie). Was sie dort sehen, regeln die Policies der einzelnen Seiten.
*/
public function canAccessPanel(Panel $panel): bool
{
return $this->canAccessAdmin();
}
public function canAccessAdmin(): bool
{
$permissions = $this->permissions();
return $this->is_active && ($permissions->global !== [] || $permissions->hasAnyObjectGrant());
}
public function permissions(): PermissionSet
{
return app(Permissions::class)->for($this);
}
/**
* @return HasMany<UserIdentity, $this>
*/
public function identities(): HasMany
{
return $this->hasMany(UserIdentity::class);
}
/**
* @return BelongsTo<User, $this>
*/
public function manager(): BelongsTo
{
return $this->belongsTo(User::class, 'manager_id');
}
/**
* @return HasMany<User, $this>
*/
public function reports(): HasMany
{
return $this->hasMany(User::class, 'manager_id');
}
/**
* Profilfelder, die ein Identity Provider pflegt – im Portal nur lesbar.
*
* @return list<string>
*/
public function managedFields(): array
{
return $this->identities()->with('provider')->get()
->filter(fn (UserIdentity $identity) => $identity->provider?->enabled)
->flatMap(fn (UserIdentity $identity) => $identity->provider->syncedFields())
->unique()->values()->all();
}
/**
* @return BelongsToMany<Role, $this>
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class)->withPivot('source', 'source_ref', 'created_at');
}
public function hasRole(string $slug): bool
{
return $this->roles()->where('slug', $slug)->exists();
}
public function isAdministrator(): bool
{
return $this->permissions()->isSuperuser();
}
/**
* Manuell vergebene Rollen setzen. Rollen aus Identity-Provider-Gruppen
* (Quelle „idp“) bleiben unberührt – Rollen sind additiv.
*
* @param list<int> $roleIds
*/
public function syncManualRoles(array $roleIds): void
{
$current = $this->roles()->wherePivot('source', 'manual')->pluck('roles.id')->all();
foreach (array_diff($current, $roleIds) as $roleId) {
$this->roles()->wherePivot('source', 'manual')->detach($roleId);
}
foreach (array_diff($roleIds, $current) as $roleId) {
$this->roles()->attach($roleId, ['source' => 'manual', 'created_at' => now()]);
}
Permissions::flush();
}
public function getFilamentAvatarUrl(): ?string
{
return $this->avatarUrl(96);
}
public function avatarUrl(int $size = 256): ?string
{
return app(Avatars::class)->url($this, $size);
}
/**
* @return Attribute<string, never>
*/
protected function initials(): Attribute
{
return Attribute::get(function (): string {
$parts = preg_split('/\s+/', trim($this->name)) ?: [];
$first = Str::substr($parts[0] ?? '', 0, 1);
$last = count($parts) > 1 ? Str::substr(end($parts), 0, 1) : '';
return Str::upper($first.$last) ?: '?';
});
}
/**
* @param Builder<User> $query
*/
public function scopeActive(Builder $query): void
{
$query->where('is_active', true);
}
}