Files
service_dav/lib/Backends/PrincipalBackend.php
T
2026-07-27 00:50:43 -04:00

98 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ServiceDav\Backends;
use KTXC\Context\IdentityContextInterface;
use KTXC\Context\TenantContextInterface;
use KTXM\ServiceDav\DavProperties;
use Sabre\DAVACL\PrincipalBackend\BackendInterface;
/**
* DAVACL principal backend.
*/
class PrincipalBackend implements BackendInterface
{
public const PRINCIPAL_PREFIX = 'principals';
public function __construct(
private readonly IdentityContextInterface $identityContext,
private readonly TenantContextInterface $tenantContext,
) {}
// -------------------------------------------------------------------------
// BackendInterface
// -------------------------------------------------------------------------
public function getPrincipalsByPrefix($prefixPath): array
{
if ($prefixPath !== self::PRINCIPAL_PREFIX || $this->identityContext->identity() === null) {
return [];
}
return [$this->buildPrincipal()];
}
public function getPrincipalByPath($path): ?array
{
if ($this->identityContext->identity() === null) {
return null;
}
$expected = self::PRINCIPAL_PREFIX . '/' . $this->identityContext->identifier();
if ($path !== $expected) {
return null;
}
return $this->buildPrincipal();
}
public function getGroupMemberSet($principal): array
{
return [];
}
public function getGroupMembership($principal): array
{
return [];
}
public function setGroupMemberSet($principal, $members): void
{
// not supported
}
public function updatePrincipal($path, $propPatch): int
{
return 403;
}
public function searchPrincipals($prefixPath, $searchProperties, $test = 'allof'): array
{
return [];
}
public function findByUri($uri, $principalPrefix): ?string
{
return null;
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
private function buildPrincipal(): array
{
$userId = $this->identityContext->identifier();
$label = $this->identityContext->label() ?? $userId;
$email = $this->identityContext->mailAddress() ?? '';
return [
DavProperties::URI => self::PRINCIPAL_PREFIX . '/' . $userId,
DavProperties::DISPLAYNAME => $label,
DavProperties::SABRE_EMAIL => $email,
];
}
}