Loheide-eu-mitarbeiterplatt.../app/Http/Controllers/Api/AuthController.php
Friederich Loheide 9703a4c3f2
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m39s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m12s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m26s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m7s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m56s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m12s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 8m58s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m52s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m9s
App kennt aktive Plugins (0.43.0)
- /api/v1/me liefert „plugins“: aktive Plugins, die die Person nutzen darf
  (Rollen-Ausschluss beachtet) – die App blendet Nachrichten, Gruppen und
  Kleinanzeigen danach ein
- docs/API.md: Feld beschrieben, Hinweis auf die API der Kunden-Plugins

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 20:24:06 +00:00

103 lines
3.7 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\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Plugins\PluginAccess;
use App\Plugins\PluginManager;
use App\Providers\FortifyServiceProvider;
use App\Support\Audit;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller
{
/**
* Token für eine App (nur Konten mit Portal-Passwort; bei Anmeldung über
* Microsoft & Co. erzeugt man das Token unter „Profil → Apps & Geräte“).
*/
public function token(Request $request): JsonResponse
{
$data = $request->validate([
'email' => ['required', 'email'],
'password' => ['required', 'string'],
'device_name' => ['required', 'string', 'max:100'],
]);
// Je E-Mail-Adresse begrenzen (zusätzlich zur Begrenzung je IP) – gegen Durchprobieren von Passwörtern
$key = 'api-token:'.Str::lower($data['email']);
if (RateLimiter::tooManyAttempts($key, 5)) {
throw ValidationException::withMessages(['email' => 'Zu viele Versuche. Bitte in einigen Minuten erneut versuchen.']);
}
$user = User::query()->where('email', Str::lower($data['email']))->first();
$valid = Hash::check($data['password'], $user?->password ?? FortifyServiceProvider::dummyHash());
if (! $user || ! $user->is_active || $user->password === null || ! $valid) {
RateLimiter::hit($key, 300);
throw ValidationException::withMessages(['email' => 'Diese Kombination aus Zugangsdaten wurde nicht gefunden.']);
}
RateLimiter::clear($key);
return self::issue($user, $data['device_name']);
}
/**
* Neuer App-Zugang. Höchstens 20 je Person – der älteste fällt weg.
*/
public static function issue(User $user, string $deviceName): JsonResponse
{
$excess = $user->tokens()->count() - 19;
if ($excess > 0) {
$user->tokens()->oldest()->limit($excess)->get()->each->delete();
}
$token = $user->createToken(trim($deviceName));
Audit::log('api.token_created', ['user' => $user->id, 'device' => $deviceName]);
return response()->json(['token' => $token->plainTextToken, 'user' => self::profile($user)], 201);
}
public function me(Request $request): JsonResponse
{
return response()->json(['user' => self::profile($request->user())]);
}
public function revoke(Request $request): JsonResponse
{
$request->user()->currentAccessToken()?->delete();
return response()->json(['revoked' => true]);
}
/**
* @return array<string, mixed>
*/
private static function profile(User $user): array
{
return [
'id' => $user->id,
'name' => $user->name,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'job_title' => $user->job_title,
'department' => $user->department,
'location' => $user->location,
'avatar_url' => $user->avatarUrl(256),
'cover_url' => $user->coverUrl(),
'unread_notifications' => $user->unreadNotifications()->count(),
// Aktive Plugins, die die Person nutzen darf – die App blendet danach Bereiche ein (z. B. chat, gruppen)
'plugins' => array_values(array_filter(array_keys(app(PluginManager::class)->loaded()), fn (string $slug) => app(PluginAccess::class)->allows($slug, $user))),
];
}
}