user auth, settings, profile

This commit is contained in:
root
2025-12-22 18:01:26 -05:00
parent 81822498c8
commit d19bd16210
19 changed files with 711 additions and 467 deletions

View File

@@ -4,6 +4,8 @@ namespace KTXC\Controllers;
use KTXC\Http\Response\JsonResponse;
use KTXC\Module\ModuleManager;
use KTXC\Service\UserService;
use KTXC\SessionIdentity;
use KTXF\Controller\ControllerAbstract;
use KTXC\SessionTenant;
use KTXF\Routing\Attributes\AuthenticatedRoute;
@@ -12,7 +14,9 @@ class InitController extends ControllerAbstract
{
public function __construct(
private readonly SessionTenant $tenant,
private readonly SessionIdentity $userIdentity,
private readonly ModuleManager $moduleManager,
private readonly UserService $userService,
) {}
#[AuthenticatedRoute('/init', name: 'init', methods: ['GET'])]
@@ -36,6 +40,18 @@ class InitController extends ControllerAbstract
'label' => $this->tenant->label(),
];
// user
$configuration['user'] = [
'auth' => [
'identifier' => $this->userIdentity->identifier(),
'identity' => $this->userIdentity->identity()->getIdentity(),
'label' => $this->userIdentity->label(),
'permissions' => [], // TODO: Implement permissions
],
'profile' => $this->userService->getEditableFields($this->userIdentity->identifier()),
'settings' => $this->userService->fetchSettings(),
];
return new JsonResponse($configuration);
}

View File

@@ -0,0 +1,63 @@
<?php
namespace KTXC\Controllers;
use KTXC\Http\Response\JsonResponse;
use KTXC\Service\UserService;
use KTXC\SessionIdentity;
use KTXC\SessionTenant;
use KTXF\Controller\ControllerAbstract;
use KTXF\Routing\Attributes\AuthenticatedRoute;
class UserProfileController extends ControllerAbstract
{
public function __construct(
private readonly SessionTenant $tenantIdentity,
private readonly SessionIdentity $userIdentity,
private readonly UserService $userService
) {}
/**
* Retrieve user profile
*
* @return JsonResponse Profile data with editability metadata
*/
#[AuthenticatedRoute('/user/profile', name: 'user.profile.read', methods: ['GET'])]
public function read(): JsonResponse
{
$userId = $this->userIdentity->identifier();
$profile = $this->userService->fetchProfile($userId);
return new JsonResponse($profile, JsonResponse::HTTP_OK);
}
/**
* Update user profile fields
* Only editable fields can be updated. Provider-managed fields are automatically filtered out.
*
* @param array $profile Key-value pairs of profile fields to update
*
* @example request body:
* {
* "name_given": "John",
* "name_family": "Doe",
* "phone": "+1234567890"
* }
*
* @return JsonResponse Updated profile data
*/
#[AuthenticatedRoute('/user/profile', name: 'user.profile.update', methods: ['PUT', 'PATCH'])]
public function update(array $profile = []): JsonResponse
{
$userId = $this->userIdentity->identifier();
// storeProfile automatically filters out provider-managed fields
$this->userService->storeProfile($userId, $profile);
// Return updated profile
$updatedProfile = $this->userService->fetchProfile($userId);
return new JsonResponse($updatedProfile, JsonResponse::HTTP_OK);
}
}

View File

@@ -13,46 +13,47 @@ class UserSettingsController extends ControllerAbstract
{
public function __construct(
private readonly SessionTenant $tenantIdentity,
private readonly SessionIdentity $userIdentity,
private readonly SessionIdentity $userIdentity,
private readonly UserService $userService
) {}
/**
* retrieve user settings
* Retrieve user settings
* If no specific settings are requested, all settings are returned
*
* @param array $settings list of settings to retrieve
*
* @example request body:
* {
* "settings": ["key1", "key2"]
* }
* @return JsonResponse Settings data as key-value pairs
*/
#[AuthenticatedRoute('/user/settings/read', name: 'user.settings.read', methods: ['PUT', 'PATCH'])]
public function read(array $settings = []): JsonResponse
#[AuthenticatedRoute('/user/settings', name: 'user.settings.read', methods: ['GET'])]
public function read(): JsonResponse
{
// authorize request
$tenantId = $this->tenantIdentity->identifier();
$userId = $this->userIdentity->identifier();
// Fetch all settings
$settings = $this->userService->fetchSettings([]);
return $this->userService->fetchSettings($tenantId, $userId, $settings);
return new JsonResponse($settings, JsonResponse::HTTP_OK);
}
/**
* store user settings
* Update user settings
*
* @param array $settings key-value pairs of settings to store
* @param array $settings Key-value pairs of settings to update
*
* @example request body:
* {
* "key1": "value1",
* "key2": "value2"
* "theme": "dark",
* "language": "en",
* "notifications": true
* }
*
* @return JsonResponse Updated settings data
*/
#[AuthenticatedRoute('/user/settings/write', name: 'user.settings.write', methods: ['PUT', 'PATCH'])]
public function write(array $settings): JsonResponse
#[AuthenticatedRoute('/user/settings', name: 'user.settings.update', methods: ['PUT', 'PATCH'])]
public function update(array $settings = []): JsonResponse
{
return new JsonResponse(['status' => 'not_implemented'], JsonResponse::HTTP_NOT_IMPLEMENTED);
$this->userService->storeSettings($settings);
// Return updated settings
$updatedSettings = $this->userService->fetchSettings(array_keys($settings));
return new JsonResponse($updatedSettings, JsonResponse::HTTP_OK);
}
}