Initial commit

This commit is contained in:
root
2025-12-21 09:53:16 -05:00
committed by Sebastian Krupinski
commit a7ccac98a2
43 changed files with 6391 additions and 0 deletions

View File

@@ -0,0 +1,370 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace KTXM\PeopleManager\Controllers;
use InvalidArgumentException;
use KTXC\Http\Response\JsonResponse;
use KTXC\SessionIdentity;
use KTXC\SessionTenant;
use KTXF\Controller\ControllerAbstract;
use KTXF\People\Collection\ICollectionBase;
use KTXF\Resource\Selector\SourceSelector;
use KTXF\Routing\Attributes\AuthenticatedRoute;
use KTXM\PeopleManager\Manager;
class DefaultController extends ControllerAbstract {
public function __construct(
private readonly SessionTenant $tenantIdentity,
private readonly SessionIdentity $userIdentity,
private Manager $peopleManager,
) {}
/**
* Retrieve list of available providers
*
* @return JsonResponse
*/
#[AuthenticatedRoute('/v1', name: 'peoplemanager.v1', methods: ['POST'])]
public function index(int $version, string $transaction, string $operation, array $data = [], string|null $user = null): JsonResponse {
// authorize request
$tenantId = $this->tenantIdentity->identifier();
$userId = $this->userIdentity->identifier();
try {
$data = $this->process($tenantId, $userId, $operation, $data);
return new JsonResponse([
'version' => $version,
'transaction' => $transaction,
'operation' => $operation,
'status' => 'success',
'data' => $data,
], JsonResponse::HTTP_OK);
} catch (\Throwable $t) {
return new JsonResponse([
'version' => $version,
'transaction' => $transaction,
'operation' => $operation,
'status' => 'error',
'data' => [
'code' => $t->getCode(),
'message' => $t->getMessage(),
]
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
}
}
private function process(string $tenantId, string $userId, string $operation, array $data): mixed {
return match ($operation) {
'provider.list' => $this->providerList($tenantId, $userId, $data),
'provider.extant' => $this->providerExtant($tenantId, $userId, $data),
'service.list' => $this->serviceList($tenantId, $userId, $data),
'service.extant' => $this->serviceExtant($tenantId, $userId, $data),
'service.fetch' => $this->serviceFetch($tenantId, $userId, $data),
'collection.list' => $this->collectionList($tenantId, $userId, $data),
'collection.extant' => $this->collectionExtant($tenantId, $userId, $data),
'collection.fetch' => $this->collectionFetch($tenantId, $userId, $data),
'collection.create' => $this->collectionCreate($tenantId, $userId, $data),
'collection.modify' => $this->collectionModify($tenantId, $userId, $data),
'collection.destroy' => $this->collectionDestroy($tenantId, $userId, $data),
'entity.list' => $this->entityList($tenantId, $userId, $data),
'entity.delta' => $this->entityDelta($tenantId, $userId, $data),
'entity.extant' => $this->entityExtant($tenantId, $userId, $data),
'entity.fetch' => $this->entityFetch($tenantId, $userId, $data),
'entity.create' => $this->entityCreate($tenantId, $userId, $data),
'entity.modify' => $this->entityModify($tenantId, $userId, $data),
'entity.destroy' => $this->entityDestroy($tenantId, $userId, $data),
default => throw new InvalidArgumentException("Invalid operation: $operation"),
};
}
// ==================== Provider Operations ====================
private function providerList(string $tenantId, string $userId, array $data): mixed {
$sources = null;
if (isset($data['sources']) && is_array($data['sources'])) {
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
}
return $this->peopleManager->providerList($tenantId, $userId, $sources);
}
private function providerExtant(string $tenantId, string $userId, array $data): mixed {
if (!isset($data['sources']) || !is_array($data['sources'])) {
throw new InvalidArgumentException('Invalid sources selector provided');
}
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
return $this->peopleManager->providerExtant($tenantId, $userId, $sources);
}
// ==================== Service Operations =====================
private function serviceList(string $tenantId, string $userId, array $data): mixed {
$sources = null;
if (isset($data['sources']) && is_array($data['sources'])) {
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
}
return $this->peopleManager->serviceList($tenantId, $userId, $sources);
}
private function serviceExtant(string $tenantId, string $userId, array $data): mixed {
if (!isset($data['sources']) || !is_array($data['sources'])) {
throw new InvalidArgumentException('Invalid sources selector provided');
}
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
return $this->peopleManager->serviceExtant($tenantId, $userId, $sources);
}
private function serviceFetch(string $tenantId, string $userId, array $data): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['identifier']) || !is_string($data['identifier'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
return $this->peopleManager->serviceFetch($tenantId, $userId, $data['provider'], $data['identifier']);
}
private function collectionList(string $tenantId, string $userId, array $data): mixed {
$sources = null;
if (isset($data['sources']) && is_array($data['sources'])) {
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
}
$filter = $data['filter'] ?? null;
$sort = $data['sort'] ?? null;
// retrieve collections
return $this->peopleManager->collectionList($tenantId, $userId, $sources, $filter, $sort);
}
private function collectionExtant(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['sources']) || !is_array($data['sources'])) {
throw new InvalidArgumentException('Invalid sources selector provided');
}
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
// retrieve collection status
return $this->peopleManager->collectionExtant($tenantId, $userId, $sources);
}
private function collectionFetch(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['identifier'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
// retrieve collection
return $this->peopleManager->collectionFetch($tenantId, $userId, $data['provider'], $data['service'], $data['identifier']);
}
private function collectionCreate(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['data'])) {
throw new InvalidArgumentException('Invalid collection data provided');
}
$options = $data['options'] ?? [];
// create collection
return $this->peopleManager->collectionCreate($tenantId, $userId, $data['provider'], $data['service'], $data['data'], $options);
}
private function collectionModify(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['identifier'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
if (!isset($data['data'])) {
throw new InvalidArgumentException('Invalid collection data provided');
}
// modify collection
return $this->peopleManager->collectionModify($tenantId, $userId, $data['provider'], $data['service'], $data['identifier'], $data['data']);
}
private function collectionDestroy(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['identifier'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
// destroy collection
return ['success' => $this->peopleManager->collectionDestroy($tenantId, $userId, $data['provider'], $data['service'], $data['identifier'])];
}
private function entityList(string $tenantId, string $userId, array $data = []): mixed {
$sources = null;
if (isset($data['sources']) && is_array($data['sources'])) {
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
}
$filter = $data['filter'] ?? null;
$sort = $data['sort'] ?? null;
$range = $data['range'] ?? null;
// retrieve entities
return $this->peopleManager->entityList($tenantId, $userId, $sources, $filter, $sort, $range);
}
private function entityDelta(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['sources']) || !is_array($data['sources'])) {
throw new InvalidArgumentException('Invalid sources selector provided');
}
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
// retrieve entity delta
return $this->peopleManager->entityDelta($tenantId, $userId, $sources);
}
private function entityExtant(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['sources']) || !is_array($data['sources'])) {
throw new InvalidArgumentException('Invalid sources selector provided');
}
$sources = new SourceSelector();
$sources->jsonDeserialize($data['sources']);
// retrieve entity status
return $this->peopleManager->entityExtant($tenantId, $userId, $sources);
}
private function entityFetch(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['collection'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
if (!isset($data['identifiers']) || !is_array($data['identifiers'])) {
throw new InvalidArgumentException('Invalid entity identifiers provided');
}
// retrieve entities
return $this->peopleManager->entityFetch($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $data['identifiers']);
}
private function entityCreate(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['collection'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
if (!isset($data['data']) || !is_array($data['data'])) {
throw new InvalidArgumentException('Invalid entity data provided');
}
$options = $data['options'] ?? [];
// create entity
return $this->peopleManager->entityCreate($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $data['data'], $options);
}
private function entityModify(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['collection'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
if (!isset($data['identifier'])) {
throw new InvalidArgumentException('Invalid entity identifier provided');
}
if (!isset($data['data']) || !is_array($data['data'])) {
throw new InvalidArgumentException('Invalid entity data provided');
}
// modify entity
return $this->peopleManager->entityModify($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $data['identifier'], $data['data']);
}
private function entityDestroy(string $tenantId, string $userId, array $data = []): mixed {
if (!isset($data['provider']) || !is_string($data['provider'])) {
throw new InvalidArgumentException('Invalid provider identifier provided');
}
if (!isset($data['service']) || !is_string($data['service'])) {
throw new InvalidArgumentException('Invalid service identifier provided');
}
if (!isset($data['collection'])) {
throw new InvalidArgumentException('Invalid collection identifier provided');
}
if (!isset($data['identifier'])) {
throw new InvalidArgumentException('Invalid entity identifier provided');
}
// destroy entity
return ['success' => $this->peopleManager->entityDestroy($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $data['identifier'])];
}
}

685
lib/Manager.php Normal file
View File

@@ -0,0 +1,685 @@
<?php
declare(strict_types=1);
namespace KTXM\PeopleManager;
use InvalidArgumentException;
use KTXC\Resource\ProviderManager;
use KTXF\People\Collection\ICollectionBase;
use KTXF\People\Entity\IEntityBase;
use KTXF\People\Provider\IProviderBase;
use KTXF\People\Service\IServiceBase;
use KTXF\People\Service\IServiceCollectionMutable;
use KTXF\People\Service\IServiceEntityMutable;
use KTXF\Resource\Provider\ProviderInterface;
use KTXF\Resource\Range\RangeAnchorType;
use KTXF\Resource\Range\RangeType;
use KTXF\Resource\Selector\CollectionSelector;
use KTXF\Resource\Selector\EntitySelector;
use KTXF\Resource\Selector\ServiceSelector;
use KTXF\Resource\Selector\SourceSelector;
use Psr\Log\LoggerInterface;
class Manager {
public function __construct(
private LoggerInterface $logger,
private ProviderManager $providerManager,
) { }
/**
* Retrieve available providers
*
* @param SourceSelector|null $sources collection of provider identifiers
*
* @return array<string,IProviderBase> collection of available providers e.g. ['provider1' => IProvider, 'provider2' => IProvider]
*/
public function providerList(string $tenantId, string $userId, ?SourceSelector $sources = null): array {
// determine filter from sources
$filter = ($sources !== null && $sources->identifiers() !== []) ? $sources->identifiers() : null;
// retrieve providers from provider manager
return $this->providerManager->providers(ProviderInterface::TYPE_PEOPLE, $filter);
}
/**
* Confirm which providers are available
*
* @param SourceSelector|null $sources collection of provider identifiers to confirm
*
* @return array<string,bool> collection of providers and their availability status e.g. ['provider1' => true, 'provider2' => false]
*/
public function providerExtant(string $tenantId, string $userId, SourceSelector $sources): array {
// determine which providers are available
$providersResolved = $this->providerList($tenantId, $userId, $sources);
$providersAvailable = array_keys($providersResolved);
$providersUnavailable = array_diff($sources->identifiers(), $providersAvailable);
// construct response data
$responseData = array_merge(
array_fill_keys($providersAvailable, true),
array_fill_keys($providersUnavailable, false)
);
return $responseData;
}
/**
* Retrieve specific provider for specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $provider provider identifier
*
* @return IProviderBase
* @throws InvalidArgumentException
*/
public function providerFetch(string $tenantId, string $userId, string $provider): IProviderBase {
// retrieve provider
$providers = $this->providerList($tenantId, $userId, new SourceSelector([$provider => true]));
if (!isset($providers[$provider])) {
throw new InvalidArgumentException("Provider '$provider' not found");
}
return $providers[$provider];
}
/**
* Retrieve available services for specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param SourceSelector|null $sources list of provider and service identifiers
*
* @return array<string,<string,IServiceBase>> collections of available services e.g. ['provider1' => ['service1' => IServiceBase], 'provider2' => ['service2' => IServiceBase]]
*/
public function serviceList(string $tenantId, string $userId, ?SourceSelector $sources = null): array {
// retrieve providers
$providers = $this->providerList($tenantId, $userId, $sources);
// retrieve services for each provider
$responseData = [];
foreach ($providers as $provider) {
$serviceFilter = $sources[$provider->id()] instanceof ServiceSelector ? $sources[$provider->id()]->identifiers() : [];
$services = $provider->serviceList($tenantId, $userId, $serviceFilter);
$responseData[$provider->id()] = $services;
}
return $responseData;
}
/**
* Confirm which services are available
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param SourceSelector|null $sources collection of provider and service identifiers to confirm
*
* @return array<string,bool> collection of providers and their availability status e.g. ['provider1' => ['service1' => false], 'provider2' => ['service2' => true, 'service3' => true]]
*/
public function serviceExtant(string $tenantId, string $userId, SourceSelector $sources): array {
// retrieve providers
$providers = $this->providerList($tenantId, $userId, $sources);
$providersRequested = $sources->identifiers();
$providersUnavailable = array_diff($providersRequested, array_keys($providers));
// initialize response with unavailable providers
$responseData = array_fill_keys($providersUnavailable, false);
// retrieve services for each available provider
foreach ($providers as $provider) {
$serviceSelector = $sources[$provider->id()];
$serviceAvailability = $provider->serviceExtant($tenantId, $userId, ...$serviceSelector->identifiers());
$responseData[$provider->id()] = $serviceAvailability;
}
return $responseData;
}
/**
* Retrieve service for specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
*
* @return IServiceBase
* @throws InvalidArgumentException
*/
public function serviceFetch(string $tenantId, string $userId, string $providerId, string|int $serviceId): IServiceBase {
// retrieve provider and service
$service = $this->providerFetch($tenantId, $userId, $providerId)->serviceFetch($tenantId, $userId, $serviceId);
if ($service === null) {
throw new InvalidArgumentException("Service '$serviceId' not found for provider '$providerId'");
}
// retrieve services
return $service;
}
/**
* Retrieve available collections for specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param SourceSelector $sources list of provider and service identifiers
*
* @return array<string,<string,IServiceBase>> collections of available services e.g. ['provider1' => ['service1' => [ICollectionBase], 'service2' => [ICollectionBase]]]
*/
public function collectionList(string $tenantId, string $userId, ?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null): array {
// confirm that sources are provided
if ($sources === null) {
$sources = new SourceSelector([]);
}
// retrieve providers
$providers = $this->providerList($tenantId, $userId, $sources);
// retrieve services for each provider
$responseData = [];
foreach ($providers as $provider) {
$serviceFilter = $sources[$provider->id()] instanceof ServiceSelector ? $sources[$provider->id()]->identifiers() : [];
$services = $provider->serviceList($tenantId, $userId, $serviceFilter);
// retrieve collections for each service
foreach ($services as $service) {
// construct filter for collections
$collectionFilter = null;
if ($filter !== null && $filter !== []) {
$collectionFilter = $service->collectionListFilter();
foreach ($filter as $attribute => $value) {
$collectionFilter->condition($attribute, $value);
}
}
// construct sort for collections
$collectionSort = null;
if ($sort !== null && $sort !== []) {
$collectionSort = $service->collectionListSort();
foreach ($sort as $attribute => $direction) {
$collectionSort->condition($attribute, $direction);
}
}
$collections = $service->collectionList($collectionFilter, $collectionSort);
if ($collections !== []) {
$responseData[$provider->id()][$service->id()] = $collections;
}
}
}
return $responseData;
}
/**
* Confirm which collections are available
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param SourceSelector $sources collection of provider and service identifiers to confirm
*
* @return array<string,bool> collection of providers and their availability status e.g. ['provider1' => ['service1' => ['collection1' => true, 'collection2' => false]]]]
*/
public function collectionExtant(string $tenantId, string $userId, SourceSelector $sources): array {
// retrieve available providers
$providers = $this->providerList($tenantId, $userId, $sources);
$providersRequested = $sources->identifiers();
$providersUnavailable = array_diff($providersRequested, array_keys($providers));
// initialize response with unavailable providers
$responseData = array_fill_keys($providersUnavailable, false);
// check services and collections for each available provider
foreach ($providers as $provider) {
$serviceSelector = $sources[$provider->id()];
$servicesRequested = $serviceSelector->identifiers();
$servicesAvailable = $provider->serviceList($tenantId, $userId, $servicesRequested);
$servicesUnavailable = array_diff($servicesRequested, array_keys($servicesAvailable));
// mark unavailable services as false
if ($servicesUnavailable !== []) {
$responseData[$provider->id()] = array_fill_keys($servicesUnavailable, false);
}
// confirm collections for each available service
foreach ($servicesAvailable as $service) {
$collectionSelector = $serviceSelector[$service->id()];
$collectionsRequested = $collectionSelector->identifiers();
if ($collectionsRequested === []) {
continue;
}
// check each requested collection
foreach ($collectionsRequested as $collectionId) {
$responseData[$provider->id()][$service->id()][$collectionId] = $service->collectionExtant((string)$collectionId);
}
}
}
return $responseData;
}
/**
* Retrieve collection for specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
*
* @return ICollectionBase
* @throws InvalidArgumentException
*/
public function collectionFetch(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId): ICollectionBase {
// retrieve services
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// retrieve collection
return $service->collectionFetch($collectionId);
}
/**
* Create a new collection for a specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param ICollectionBase|array $collection collection to create
* @param array $options additional options for creation
*
* @return ICollectionBase
* @throws InvalidArgumentException
*/
public function collectionCreate(string $tenantId, string $userId, string $providerId, string|int $serviceId, ICollectionBase|array $collection, array $options = []): ICollectionBase {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// Check if service supports collection creation
if (!($service instanceof IServiceCollectionMutable)) {
throw new InvalidArgumentException("Service does not support collection mutations");
}
if (!$service->capable(IServiceCollectionMutable::CAPABILITY_COLLECTION_CREATE)) {
throw new InvalidArgumentException("Service is not capable of creating collections");
}
if (is_array($collection)) {
$collection = $service->collectionFresh()->jsonDeserialize($collection);
}
// Create collection (location is empty string for root)
return $service->collectionCreate('', $collection, $options);
}
/**
* Modify an existing collection for a specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
* @param ICollectionBase $collectionData collection with modifications
*
* @return ICollectionBase
* @throws InvalidArgumentException
*/
public function collectionModify(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId, ICollectionBase|array $collectionData): ICollectionBase {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// Check if service supports collection modification
if (!($service instanceof IServiceCollectionMutable)) {
throw new InvalidArgumentException("Service does not support collection mutations");
}
if (!$service->capable(IServiceCollectionMutable::CAPABILITY_COLLECTION_MODIFY)) {
throw new InvalidArgumentException("Service is not capable of modifying collections");
}
if (is_array($collectionData)) {
$collectionData = $service->collectionFresh()->jsonDeserialize($collectionData);
}
// Modify collection
return $service->collectionModify($collectionId, $collectionData);
}
/**
* Delete a collection for a specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
*
* @return bool
* @throws InvalidArgumentException
*/
public function collectionDestroy(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId): bool {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// Check if service supports collection destruction
if (!($service instanceof IServiceCollectionMutable)) {
throw new InvalidArgumentException("Service does not support collection mutations");
}
if (!$service->capable(IServiceCollectionMutable::CAPABILITY_COLLECTION_DESTROY)) {
throw new InvalidArgumentException("Service is not capable of destroying collections");
}
// Destroy collection and cast result to bool
return (bool)$service->collectionDestroy($collectionId);
}
/**
* Retrieve available entities for specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param SourceSelector $sources list of provider and service identifiers
*
* @return array<string,<string,IServiceBase>> collections of store enteties e.g. ['provider1' => ['service1' => [ICollectionBase], 'service2' => [ICollectionBase]]]
*/
public function entityList(string $tenantId, string $userId, ?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?array $range = null): array {
// confirm that sources are provided
if ($sources === null) {
$sources = new SourceSelector([]);
}
// retrieve providers
$providers = $this->providerList($tenantId, $userId, $sources);
// retrieve services for each provider
$responseData = [];
foreach ($providers as $provider) {
// retrieve services for each provider
$serviceSelector = $sources[$provider->id()];
$servicesSelected = $provider->serviceList($tenantId,$userId, $serviceSelector->identifiers());
foreach ($servicesSelected as $service) {
// retrieve collections for each service
$collectionSelector = $serviceSelector[$service->id()];
$collectionSelected = $collectionSelector instanceof CollectionSelector ? $collectionSelector->identifiers() : [];
if ($collectionSelected === []) {
$collections = $service->collectionList();
$collectionSelected = array_map(
fn($collection) => $collection->id(),
$collections
);
}
if ($collectionSelected === []) {
continue;
}
// construct filter for entities
$entityFilter = null;
if ($filter !== null && $filter !== []) {
$entityFilter = $service->entityListFilter();
foreach ($filter as $attribute => $value) {
$entityFilter->condition($attribute, $value);
}
}
// construct sort for entities
$entitySort = null;
if ($sort !== null && $sort !== []) {
$entitySort = $service->entityListSort();
foreach ($sort as $attribute => $direction) {
$entitySort->condition($attribute, $direction);
}
}
// construct range for entities
$entityRange = null;
if ($range !== null && $range !== [] && isset($range['type'])) {
$entityRange = $service->entityListRange(RangeType::from($range['type']));
// Cast to IRangeTally if the range type is TALLY
if ($entityRange->type() === RangeType::TALLY) {
/** @var IRangeTally $entityRange */
if (isset($range['anchor'])) {
$entityRange->setAnchor(RangeAnchorType::from($range['anchor']));
}
if (isset($range['position'])) {
$entityRange->setPosition($range['position']);
}
if (isset($range['tally'])) {
$entityRange->setTally($range['tally']);
}
}
}
// retrieve entities for each collection
foreach ($collectionSelected as $collectionId) {
$entities = $service->entityList($collectionId, $entityFilter, $entitySort, $entityRange, null);
// skip collections with no entities
if ($entities === []) {
continue;
}
$responseData[$provider->id()][$service->id()][$collectionId] = $entities;
}
}
}
return $responseData;
}
/**
* Confirm which entities are available
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param array $sources collection of provider and service identifiers to confirm
*
* @return array collection of providers and their availability status
*/
public function entityDelta(string $tenantId, string $userId, SourceSelector $sources): array {
// confirm that sources are provided
if ($sources === null) {
$sources = new SourceSelector([]);
}
// retrieve providers
$providers = $this->providerList($tenantId, $userId, $sources);
$providersRequested = $sources->identifiers();
$providersUnavailable = array_diff($providersRequested, array_keys($providers));
// initialize response with unavailable providers
$responseData = array_fill_keys($providersUnavailable, false);
// iterate through available providers
foreach ($providers as $provider) {
$serviceSelector = $sources[$provider->id()];
$servicesRequested = $serviceSelector instanceof ServiceSelector ? $serviceSelector->identifiers() : [];
$services = $provider->serviceList($tenantId, $userId, $servicesRequested);
$servicesUnavailable = array_diff($servicesRequested, array_keys($services));
if ($servicesUnavailable !== []) {
$responseData[$provider->id()] = array_fill_keys($servicesUnavailable, false);
}
// iterate through available services
foreach ($services as $service) {
$collectionSelector = $serviceSelector[$service->id()];
$collectionsRequested = $collectionSelector instanceof CollectionSelector ? $collectionSelector->identifiers() : [];
if ($collectionsRequested === []) {
$responseData[$provider->id()][$service->id()] = false;
continue;
}
foreach ($collectionsRequested as $collection) {
$entitySelector = $collectionSelector[$collection] ?? null;
$responseData[$provider->id()][$service->id()][$collection] = $service->entityDelta($collection, $entitySelector);
}
}
}
return $responseData;
}
/**
* Confirm which entities are available
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param SourceSelector $sources collection of provider and service identifiers to confirm
*
* @return array<string,bool> collection of providers and their availability status e.g. ['provider1' => ['service1' => ['collection1' => ['entity1' => true, 'entity2' => false]]]]
*/
public function entityExtant(string $tenantId, string $userId, SourceSelector $sources): array {
// confirm that sources are provided
if ($sources === null) {
$sources = new SourceSelector([]);
}
// retrieve available providers
$providers = $this->providerList($tenantId, $userId, $sources);
$providersRequested = $sources->identifiers();
$providersUnavailable = array_diff($providersRequested, array_keys($providers));
// initialize response with unavailable providers
$responseData = array_fill_keys($providersUnavailable, false);
// check services, collections, and entities for each available provider
foreach ($providers as $provider) {
$serviceSelector = $sources[$provider->id()];
$servicesRequested = $serviceSelector->identifiers();
$servicesAvailable = $provider->serviceList($tenantId, $userId, $servicesRequested);
$servicesUnavailable = array_diff($servicesRequested, array_keys($servicesAvailable));
// mark unavailable services as false
if ($servicesUnavailable !== []) {
$responseData[$provider->id()] = array_fill_keys($servicesUnavailable, false);
}
// check collections and entities for each available service
foreach ($servicesAvailable as $service) {
$collectionSelector = $serviceSelector[$service->id()];
$collectionsRequested = $collectionSelector instanceof CollectionSelector ? $collectionSelector->identifiers() : [];
if ($collectionsRequested === []) {
continue;
}
// check entities for each requested collection
foreach ($collectionsRequested as $collectionId) {
// first check if collection exists
$collectionExists = $service->collectionExtant((string)$collectionId);
if (!$collectionExists) {
// collection doesn't exist, mark as false
$responseData[$provider->id()][$service->id()][$collectionId] = false;
continue;
}
// extract entity identifiers from collection selector
$entitySelector = $collectionSelector[$collectionId];
// handle both array of entity IDs and boolean true (meaning check if collection exists)
if ($entitySelector instanceof EntitySelector) {
// check specific entities within the collection
$responseData[$provider->id()][$service->id()][$collectionId] = $service->entityExtant($collectionId, ...$entitySelector->identifiers());
} elseif ($entitySelector === true) {
// just checking if collection exists (already confirmed above)
$responseData[$provider->id()][$service->id()][$collectionId] = true;
}
}
}
}
return $responseData;
}
/**
* Retrieve entity for specific user and collection
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
* @param array<string|int> $identifiers entity identifiers
*
* @return array<string|int,IEntityBase>
*/
public function entityFetch(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId, array $identifiers): array {
// retrieve services
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// retrieve collection
return $service->entityFetch($collectionId, ...$identifiers);
}
/**
* Create a new entity in a specific collection for a specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
* @param IEntityBase|array $entity entity to create
* @param array $options additional options for creation
*
* @return IEntityBase
* @throws InvalidArgumentException
*/
public function entityCreate(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId, IEntityBase|array $entity, array $options = []): IEntityBase {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// Check if service supports entity creation
if (!($service instanceof IServiceEntityMutable)) {
throw new InvalidArgumentException("Service does not support entity mutations");
}
if (!$service->capable(IServiceEntityMutable::CAPABILITY_ENTITY_CREATE)) {
throw new InvalidArgumentException("Service is not capable of creating entities");
}
if (is_array($entity)) {
$entityInstance = $service->entityFresh();
$entityInstance->jsonDeserialize($entity);
} else {
$entityInstance = $entity;
}
// Create entity
return $service->entityCreate($collectionId, $entityInstance, $options);
}
/**
* Modify an existing entity in a collection for a specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
* @param string|int $identifier entity identifier
* @param IEntityBase|array $entity entity with modifications
*
* @return IEntityBase
* @throws InvalidArgumentException
*/
public function entityModify(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId, string|int $identifier, IEntityBase|array $entity): IEntityBase {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// Check if service supports entity modification
if (!($service instanceof IServiceEntityMutable)) {
throw new InvalidArgumentException("Service does not support entity mutations");
}
if (!$service->capable(IServiceEntityMutable::CAPABILITY_ENTITY_MODIFY)) {
throw new InvalidArgumentException("Service is not capable of modifying entities");
}
if (is_array($entity)) {
$entityInstance = $service->entityFresh();
$entityInstance->jsonDeserialize($entity);
} else {
$entityInstance = $entity;
}
// Modify entity
return $service->entityModify($collectionId, $identifier, $entityInstance);
}
/**
* Delete an entity from a collection for a specific user
*
* @param string $tenantId tenant identifier
* @param string $userId user identifier
* @param string $providerId provider identifier
* @param string|int $serviceId service identifier
* @param string|int $collectionId collection identifier
* @param string|int $identifier entity identifier
*
* @return bool
* @throws InvalidArgumentException
*/
public function entityDestroy(string $tenantId, string $userId, string $providerId, string|int $serviceId, string|int $collectionId, string|int $identifier): bool {
// retrieve service
$service = $this->serviceFetch($tenantId, $userId, $providerId, $serviceId);
// Check if service supports entity destruction
if (!($service instanceof IServiceEntityMutable)) {
throw new InvalidArgumentException("Service does not support entity mutations");
}
if (!$service->capable(IServiceEntityMutable::CAPABILITY_ENTITY_DESTROY)) {
throw new InvalidArgumentException("Service is not capable of destroying entities");
}
// Destroy entity and cast result to bool
return (bool)$service->entityDestroy($collectionId, $identifier);
}
}

64
lib/Module.php Normal file
View File

@@ -0,0 +1,64 @@
<?php
namespace KTXM\PeopleManager;
use KTXF\Module\ModuleBrowserInterface;
use KTXF\Module\ModuleInstanceAbstract;
/**
* People Manager Module
*/
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
{
public function __construct()
{ }
public function handle(): string
{
return 'people_manager';
}
public function label(): string
{
return 'People Manager';
}
public function author(): string
{
return 'Ktrix';
}
public function description(): string
{
return 'People management module for Ktrix - provides user and group management functionalities';
}
public function version(): string
{
return '0.0.1';
}
public function permissions(): array
{
return [
'people_manager' => [
'label' => 'Access People Manager',
'description' => 'View and access the people manager module',
'group' => 'People Management'
],
];
}
public function registerBI(): array {
return [
'handle' => $this->handle(),
'namespace' => 'PeopleManager',
'version' => $this->version(),
'label' => $this->label(),
'author' => $this->author(),
'description' => $this->description(),
'boot' => 'static/module.mjs',
];
}
}