Initial commit
This commit is contained in:
342
lib/Providers/Shared/Collection.php
Normal file
342
lib/Providers/Shared/Collection.php
Normal file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\PeopleProviderLocal\Providers\Shared;
|
||||
|
||||
use KTXF\People\Collection\CollectionContent;
|
||||
use KTXF\People\Collection\CollectionPermissions;
|
||||
use KTXF\People\Collection\CollectionRoles;
|
||||
use KTXF\People\Collection\ICollectionMutable;
|
||||
use KTXM\PeopleProviderLocal\Store\Personal\CollectionEntry;
|
||||
use Override;
|
||||
|
||||
class Collection implements ICollectionMutable {
|
||||
|
||||
private ?string $userId = null;
|
||||
private ?int $collectionShareId = null;
|
||||
private ?string $collectionShareOwner = null;
|
||||
private ?int $collectionSharePermissions = null;
|
||||
private string $providerId = 'default';
|
||||
private string $serviceId = 'shared';
|
||||
private ?int $collectionId = null;
|
||||
private ?string $collectionUuid = null;
|
||||
private ?string $collectionLabel = null;
|
||||
private ?string $collectionDescription = null;
|
||||
private ?int $collectionPriority = null;
|
||||
private ?bool $collectionVisibility = null;
|
||||
private ?string $collectionColor = null;
|
||||
private bool $collectionEnabled = true;
|
||||
private ?string $collectionSignature = null;
|
||||
private array $collectionPermissions = [];
|
||||
private array $collectionAttributes = [
|
||||
'roles' => [
|
||||
CollectionRoles::Individual->value => true,
|
||||
],
|
||||
'contents' => [
|
||||
CollectionContent::Individual->value => true,
|
||||
CollectionContent::Organization->value => true,
|
||||
CollectionContent::Group->value => true,
|
||||
],
|
||||
];
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return [
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_PROVIDER => $this->providerId,
|
||||
self::JSON_PROPERTY_SERVICE => $this->serviceId,
|
||||
self::JSON_PROPERTY_IN => null,
|
||||
self::JSON_PROPERTY_ID => $this->collectionId,
|
||||
self::JSON_PROPERTY_UUID => $this->collectionUuid,
|
||||
self::JSON_PROPERTY_LABEL => $this->collectionLabel,
|
||||
self::JSON_PROPERTY_DESCRIPTION => $this->collectionDescription,
|
||||
self::JSON_PROPERTY_PRIORITY => $this->collectionPriority,
|
||||
self::JSON_PROPERTY_VISIBILITY => $this->collectionVisibility,
|
||||
self::JSON_PROPERTY_COLOR => $this->collectionColor,
|
||||
self::JSON_PROPERTY_ENABLED => $this->collectionEnabled,
|
||||
self::JSON_PROPERTY_SIGNATURE => $this->collectionSignature,
|
||||
self::JSON_PROPERTY_PERMISSIONS => [$this->userId => $this->collectionPermissions],
|
||||
self::JSON_PROPERTY_ROLES => $this->collectionAttributes['roles'] ?? [],
|
||||
self::JSON_PROPERTY_CONTENTS => $this->collectionAttributes['contents'] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array $data): static
|
||||
{
|
||||
$this->collectionId = $data[self::JSON_PROPERTY_ID] ?? null;
|
||||
$this->collectionUuid = $data[self::JSON_PROPERTY_UUID] ?? null;
|
||||
$this->collectionLabel = $data[self::JSON_PROPERTY_LABEL] ?? null;
|
||||
$this->collectionDescription = $data[self::JSON_PROPERTY_DESCRIPTION] ?? null;
|
||||
$this->collectionPriority = $data[self::JSON_PROPERTY_PRIORITY] ?? null;
|
||||
$this->collectionVisibility = $data[self::JSON_PROPERTY_VISIBILITY] ?? null;
|
||||
$this->collectionColor = $data[self::JSON_PROPERTY_COLOR] ?? null;
|
||||
$this->collectionEnabled = $data[self::JSON_PROPERTY_ENABLED] ?? true;
|
||||
$this->collectionSignature = $data[self::JSON_PROPERTY_SIGNATURE] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromStore(CollectionEntry $data): self {
|
||||
$this->collectionShareOwner = $data->getUserId();
|
||||
$this->collectionId = $data->getId();
|
||||
$this->collectionUuid = $data->getUri();
|
||||
$this->collectionLabel = $data->getDisplayname();
|
||||
$this->collectionDescription = $data->getDescription();
|
||||
$this->collectionSignature = $data->getSynctoken();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): CollectionEntry {
|
||||
$data = new CollectionEntry();
|
||||
if ($this->collectionId !== null) {
|
||||
$data->setId($this->collectionId);
|
||||
}
|
||||
$data->setUserId($this->userId);
|
||||
$data->setUri($this->collectionUuid);
|
||||
$data->setDisplayname($this->collectionLabel);
|
||||
$data->setDescription($this->collectionDescription);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function fromShareStore(array $data): self {
|
||||
|
||||
if (empty($data['principaluri']) || !str_starts_with($data['principaluri'], 'principals/users/')) {
|
||||
throw new \InvalidArgumentException('Share data must contain a principaluri');
|
||||
}
|
||||
$this->userId = substr($data['principaluri'], 17);
|
||||
$this->collectionShareId = $data['id'] ?? null;
|
||||
$this->collectionSharePermissions = $data['access'] ?? 0;
|
||||
$this->collectionPermissions[CollectionPermissions::View->value] = true;
|
||||
if ($this->collectionSharePermissions === 2) {
|
||||
$this->collectionPermissions[CollectionPermissions::Create->value] = true;
|
||||
$this->collectionPermissions[CollectionPermissions::Modify->value] = true;
|
||||
$this->collectionPermissions[CollectionPermissions::Destroy->value] = true;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique identifier of the service this collection belongs to
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function in(): null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique arbitrary text string identifying this service (e.g. 1 or collection1 or anything else)
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function id(): int {
|
||||
return $this->collectionShareId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all supported attributes
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*
|
||||
* @return array<string,bool>
|
||||
*/
|
||||
public function attributes(): array {
|
||||
return $this->collectionAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique universal identifier
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function uuid(): string {
|
||||
return $this->collectionUuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the signature of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function signature(): ?string {
|
||||
return $this->collectionSignature;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the roles of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*
|
||||
* @return array<string,bool>
|
||||
*/
|
||||
public function roles(): array {
|
||||
return $this->collectionAttributes['roles'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this collection supports the given role
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function role(CollectionRoles $role): bool {
|
||||
return $this->collectionAttributes['roles'][$role->value] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the content types of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*
|
||||
* @return array<string,bool>
|
||||
*/
|
||||
public function contents(): array {
|
||||
return $this->collectionAttributes['content'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this collection contains the given content type
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function contains(CollectionContent $content): bool {
|
||||
return $this->collectionAttributes['content'][$content->value] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the active status of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getEnabled(): bool {
|
||||
return (bool)$this->collectionEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the active status of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function setEnabled(bool $value): self {
|
||||
$this->collectionEnabled = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the active status of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getPermissions(): array {
|
||||
return [$this->userId => $this->collectionPermissions];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this collection supports the given attribute
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function hasPermission(CollectionPermissions $permission): bool {
|
||||
return $this->collectionPermissions[$permission->value] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the human friendly name of this collection (e.g. Personal Contacts)
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getLabel(): ?string {
|
||||
return $this->collectionLabel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the human friendly name of this collection (e.g. Personal Contacts)
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function setLabel(string $value): self {
|
||||
$this->collectionLabel = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the human friendly description of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getDescription(): ?string {
|
||||
return $this->collectionDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the human friendly description of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function setDescription(?string $value): self {
|
||||
$this->collectionDescription = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the priority of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getPriority(): ?int {
|
||||
return $this->collectionPriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the priority of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function setPriority(?int $value): self {
|
||||
$this->collectionPriority = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the visibility of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getVisibility(): ?bool {
|
||||
return $this->collectionVisibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the visibility of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function setVisibility(?bool $value): self {
|
||||
$this->collectionVisibility = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the color of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function getColor(): ?string {
|
||||
return $this->collectionColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the color of this collection
|
||||
*
|
||||
* @since 2025.05.01
|
||||
*/
|
||||
public function setColor(?string $value): self {
|
||||
$this->collectionColor = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
583
lib/Providers/Shared/SharedService.php
Normal file
583
lib/Providers/Shared/SharedService.php
Normal file
@@ -0,0 +1,583 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\PeopleProviderLocal\Providers\Shared;
|
||||
|
||||
use KTXF\People\Collection\ICollectionMutable;
|
||||
use KTXF\People\Entity\IEntityMutable;
|
||||
use KTXF\People\Exceptions\InvalidParameterException;
|
||||
use KTXF\People\Exceptions\UnauthorizedException;
|
||||
use KTXF\People\Exceptions\UnsupportedException;
|
||||
use KTXF\People\Filter\Filter;
|
||||
use KTXF\People\Filter\IFilter;
|
||||
use KTXF\People\Range\IRange;
|
||||
use KTXF\People\Range\RangeTally;
|
||||
use KTXF\People\Range\RangeType;
|
||||
use KTXF\People\Service\IServiceBase;
|
||||
use KTXF\People\Service\IServiceCollectionMutable;
|
||||
use KTXF\People\Service\IServiceEntityMutable;
|
||||
use KTXF\People\Sort\ISort;
|
||||
use KTXF\People\Sort\Sort;
|
||||
use KTXM\PeopleProviderLocal\Providers\Personal\Entity;
|
||||
|
||||
class SharedService implements IServiceBase, IServiceCollectionMutable, IServiceEntityMutable {
|
||||
|
||||
protected const SERVICE_ID = 'shared';
|
||||
protected const SERVICE_LABEL = 'Shared Contacts Service';
|
||||
protected const SERVICE_PROVIDER = 'default';
|
||||
|
||||
protected array $serviceCollectionCache = [];
|
||||
protected array $serviceSharesCache = [];
|
||||
protected ?string $serviceTenantId = null;
|
||||
protected ?string $serviceUserId = null;
|
||||
protected ?bool $serviceEnabled = true;
|
||||
|
||||
protected array $serviceAbilities = [
|
||||
self::CAPABILITY_COLLECTION_LIST => true,
|
||||
self::CAPABILITY_COLLECTION_LIST_FILTER => [
|
||||
'id' => 'a:10:64:192',
|
||||
'label' => 's:100:256:771',
|
||||
'description' => 's:100:256:771',
|
||||
],
|
||||
self::CAPABILITY_COLLECTION_LIST_SORT => [
|
||||
'label',
|
||||
'description'
|
||||
],
|
||||
self::CAPABILITY_COLLECTION_EXTANT => true,
|
||||
self::CAPABILITY_COLLECTION_FETCH => true,
|
||||
self::CAPABILITY_COLLECTION_CREATE => true,
|
||||
self::CAPABILITY_COLLECTION_MODIFY => true,
|
||||
self::CAPABILITY_COLLECTION_DESTROY => true,
|
||||
self::CAPABILITY_ENTITY_LIST => true,
|
||||
self::CAPABILITY_ENTITY_LIST_FILTER => [
|
||||
'*' => 's:200:256:771',
|
||||
'uri' => 's:200:256:771',
|
||||
'label' => 's:200:256:771',
|
||||
'phone' => 's:200:256:771',
|
||||
'email' => 's:200:256:771',
|
||||
'location' => 's:200:256:771'
|
||||
],
|
||||
self::CAPABILITY_ENTITY_LIST_SORT => [
|
||||
'label',
|
||||
'phone',
|
||||
'email',
|
||||
'location'
|
||||
],
|
||||
self::CAPABILITY_ENTITY_LIST_RANGE => [
|
||||
'tally' => ['absolute', 'relative']
|
||||
],
|
||||
self::CAPABILITY_ENTITY_DELTA => true,
|
||||
self::CAPABILITY_ENTITY_EXTANT => true,
|
||||
self::CAPABILITY_ENTITY_FETCH => true,
|
||||
self::CAPABILITY_ENTITY_CREATE => true,
|
||||
self::CAPABILITY_ENTITY_MODIFY => true,
|
||||
self::CAPABILITY_ENTITY_DESTROY => true,
|
||||
self::CAPABILITY_ENTITY_COPY => true,
|
||||
self::CAPABILITY_ENTITY_MOVE => true,
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private Store $store,
|
||||
//private SharingMapper $sharingStore,
|
||||
) {}
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return [
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_PROVIDER => self::SERVICE_PROVIDER,
|
||||
self::JSON_PROPERTY_ID => self::SERVICE_ID,
|
||||
self::JSON_PROPERTY_LABEL => self::SERVICE_LABEL,
|
||||
self::JSON_PROPERTY_CAPABILITIES => $this->serviceAbilities,
|
||||
self::JSON_PROPERTY_ENABLED => $this->serviceEnabled,
|
||||
];
|
||||
}
|
||||
|
||||
public function init(string $tenantId, string $userId): self {
|
||||
$this->serviceTenantId = $tenantId;
|
||||
$this->serviceUserId = $userId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms if specific capability is supported
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function capable(string $value): bool {
|
||||
if (isset($this->serviceAbilities[$value])) {
|
||||
return (bool)$this->serviceAbilities[$value];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all supported capabilities
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function capabilities(): array {
|
||||
return $this->serviceAbilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique identifier of the provider this service belongs to
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function in(): string{
|
||||
return self::SERVICE_PROVIDER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique arbitrary text string identifying this service (e.g. 1 or service1 or anything else)
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function id(): string {
|
||||
return self::SERVICE_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the localized human friendly name of this service (e.g. ACME Company Mail Service)
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLabel(): string {
|
||||
return self::SERVICE_LABEL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the active status of this service
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getEnabled(): bool {
|
||||
return (bool)$this->serviceEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of accessible collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionList(?IFilter $filter = null, ?ISort $sort = null): array {
|
||||
/*
|
||||
$shareEntries = $this->listShareEntries();
|
||||
foreach ($shareEntries as $key => $shareEntry) {
|
||||
$collectionEntry = $this->store->collectionFetch('system', $shareEntry['resourceid']);
|
||||
$collection = new Collection();
|
||||
$collection->fromStore($collectionEntry)->fromShareStore($shareEntry);
|
||||
$list[$collection->id()] = $collection;
|
||||
$this->serviceCollectioncache[$collection->id()] = $collection;
|
||||
}
|
||||
*/
|
||||
return $list ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filter for collection list
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionListFilter(): Filter {
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_FILTER] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sort for collection list
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionListSort(): Sort {
|
||||
return new Sort($this->serviceAbilities[self::CAPABILITY_COLLECTION_LIST_SORT] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms if a collection exists
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionExtant(string|int $id): bool {
|
||||
// validate id
|
||||
if (!is_numeric($id)) {
|
||||
throw new InvalidParameterException("Invalid: Collection identifier '$id' is not valid");
|
||||
}
|
||||
$id = (int)$id;
|
||||
// determine if collection is cached
|
||||
if (isset($this->serviceCollectioncache[$id])) {
|
||||
return true;
|
||||
}
|
||||
if ($this->fetchShareEntry($id) !== null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches details about a specific collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionFetch(string|int $id): ?Collection {
|
||||
// validate access
|
||||
if ($this->collectionExtant($id) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$id'");
|
||||
}
|
||||
$id = (int)$id;
|
||||
// determine if collection is cached
|
||||
if (isset($this->serviceCollectioncache[$id])) {
|
||||
return $this->serviceCollectioncache[$id];
|
||||
}
|
||||
// retrieve share data
|
||||
$shareEntry = $this->fetchShareEntry($id);
|
||||
// retrieve collection data
|
||||
$collectionEntry = $this->store->collectionFetch('system', $shareEntry['resourceid']);
|
||||
if ($collectionEntry !== null) {
|
||||
$collection = new Collection();
|
||||
$collection->fromStore($collectionEntry)->fromShareStore($shareEntry);
|
||||
$this->serviceCollectioncache[$collection->id()] = $collection;
|
||||
return $collection;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new collection at the specified location
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionCreate(string|int $location, ICollectionMutable $collection, array $options): Collection {
|
||||
throw new UnsupportedException("Unsupported: Shared service does not support collection creation");
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies an existing collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionModify(string|int $id, ICollectionMutable $collection): Collection {
|
||||
// validate access
|
||||
if ($this->collectionExtant($id) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$id'");
|
||||
}
|
||||
$id = (int)$id;
|
||||
// convert collection to a native type if needed
|
||||
if (!($collection instanceof Collection)) {
|
||||
$nativeCollection = new Collection();
|
||||
$nativeCollection->fromJson($collection->toJson());
|
||||
} else {
|
||||
$nativeCollection = clone $collection;
|
||||
}
|
||||
// convert to store type and force user id
|
||||
$storeEntry = $nativeCollection->toStore();
|
||||
$storeEntry->setUserId($this->serviceUserId);
|
||||
// modify collection in store
|
||||
$storeEntry = $this->store->collectionModify($storeEntry);
|
||||
$nativeCollection->fromStore($storeEntry);
|
||||
return $nativeCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys a collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collectionDestroy(string|int $id): bool {
|
||||
// validate access
|
||||
if ($this->collectionExtant($id) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$id'");
|
||||
}
|
||||
$id = (int)$id;
|
||||
// destroy collection in store
|
||||
if ($this->store->collectionDestroyById($this->serviceUserId, $id)) {
|
||||
unset($this->serviceCollectioncache[$id]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all entities in a specific collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $options = null): array {
|
||||
// validate collection access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// retrieve entity entries
|
||||
$entries = $this->store->entityList($shareEntry['resourceid'], $filter, $sort, $range, $options);
|
||||
foreach ($entries as $key => $entry) {
|
||||
$entity = new Entity();
|
||||
$entity->fromStore($entry);
|
||||
$entities[$key] = $entity;
|
||||
}
|
||||
return $entities ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filter for entity list
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityListFilter(): Filter {
|
||||
return new Filter($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_FILTER] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sort for entity list
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityListSort(): Sort {
|
||||
return new Sort($this->serviceAbilities[self::CAPABILITY_ENTITY_LIST_SORT] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sort for entity list
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityListRange(RangeType $type): IRange {
|
||||
// validate type
|
||||
if ($type !== RangeType::TALLY) {
|
||||
throw new InvalidParameterException("Invalid: Entity range of type '{$type->value}' is not valid");
|
||||
}
|
||||
return new RangeTally();
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms if entity(ies) exist in a collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityExtant(string|int $collection, string|int ...$identifiers): array {
|
||||
// validate access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// retrieve entity status
|
||||
return $this->store->entityExtant($shareEntry['resourceid'], $identifiers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists of all changes from a specific token
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityDelta(string|int $collection, string $signature, string $detail = 'ids'): array {
|
||||
// validate access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// retrieve entity delta from store
|
||||
return $this->store->chronicleReminisce($shareEntry['resourceid'], $signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves details about a specific entity(ies)
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityFetch(string|int $collection, string|int ...$identifiers): array {
|
||||
// validate collection access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// retrieve entity entry
|
||||
$entries = $this->store->entityFetch($shareEntry['resourceid'], $identifiers);
|
||||
foreach ($entries as $key => $entry) {
|
||||
$entity = new Entity();
|
||||
$entity->fromStore($entry);
|
||||
$entities[$key] = $entity;
|
||||
}
|
||||
return $entities ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a fresh entity of the specified type
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityFresh(): Entity {
|
||||
return new Entity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new entity in the specified collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityCreate(string|int $collection, IEntityMutable $entity, array $options): Entity {
|
||||
// validate collection access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// convert enity to a native type if needed
|
||||
if (!($entity instanceof Entity)) {
|
||||
$nativeEntity = $this->entityFresh();
|
||||
$nativeEntity->fromJson($entity->toJson());
|
||||
} else {
|
||||
$nativeEntity = clone $entity;
|
||||
}
|
||||
// convert to store type and force address book id
|
||||
$storeEntry = $nativeEntity->toStore();
|
||||
$storeEntry->setAddressbookid($shareEntry['resourceid']);
|
||||
$storeEntry->setLastmodified(time());
|
||||
if (isset($options['source']) && $options['source'] === 'dav' && isset($options['uri']) && !empty($options['uri'])) {
|
||||
$storeEntry->setUri($options['uri']);
|
||||
}
|
||||
// create entry in store
|
||||
$storeEntry = $this->store->entityCreate($storeEntry);
|
||||
$nativeEntity->fromStore($storeEntry);
|
||||
|
||||
return $nativeEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies an existing entity in the specified collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityModify(string|int $collection, string|int $identifier, IEntityMutable $entity): Entity {
|
||||
// validate collection access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// validate entity identifier
|
||||
if (empty($identifier) || !is_numeric($identifier)) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' is not valid");
|
||||
}
|
||||
$identifier = (int)$identifier;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// validate entity extant and ownership
|
||||
$extant = $this->store->entityExtant($collection, [$identifier]);
|
||||
if (!isset($extant[$identifier]) || $extant[$identifier] === false) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' does not exist or does not belong to collection '$collection' or user '{$this->serviceUserId}'");
|
||||
}
|
||||
// convert enity to a native type if needed
|
||||
if (!($entity instanceof Entity)) {
|
||||
$nativeEntity = $this->entityFresh();
|
||||
$nativeEntity->fromJson($entity->toJson());
|
||||
} else {
|
||||
$nativeEntity = clone $entity;
|
||||
}
|
||||
|
||||
// convert to store type and force address book id
|
||||
$storeEntry = $nativeEntity->toStore();
|
||||
$storeEntry->setId($identifier);
|
||||
$storeEntry->setAddressbookid($shareEntry['resourceid']);
|
||||
$storeEntry->setLastmodified(time());
|
||||
// modify entry in store
|
||||
$storeEntry = $this->store->entityModify($shareEntry['resourceid'], $storeEntry);
|
||||
$nativeEntity->fromStore($storeEntry);
|
||||
|
||||
return $nativeEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys an existing entity in the specified collection
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function entityDestroy(string|int $collection, string|int $identifier): IEntityMutable {
|
||||
// validate collection access
|
||||
if ($this->collectionExtant($collection) === false) {
|
||||
throw new UnauthorizedException("Unauthorized: User '{$this->serviceUserId}' does not have access to collection '$collection'");
|
||||
}
|
||||
$collection = (int)$collection;
|
||||
// retrieve share entry
|
||||
$shareEntry = $this->fetchShareEntry($collection);
|
||||
// validate entity identifier
|
||||
if (empty($identifier) || !is_numeric($identifier)) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' is not valid");
|
||||
}
|
||||
$identifier = (int)$identifier;
|
||||
// validate entity extant and ownership
|
||||
$extant = $this->store->entityExtant($collection, [$identifier]);
|
||||
if (!isset($extant[$identifier]) || $extant[$identifier] === false) {
|
||||
throw new InvalidParameterException("Invalid: Entity identifier '$identifier' does not exist or does not belong to collection '$collection' or user '{$this->serviceUserId}'");
|
||||
}
|
||||
// destroy entry in store
|
||||
$storeEntry = $this->store->entityDestroyById($shareEntry['resourceid'], $identifier);
|
||||
$nativeEntity = $this->entityFresh();
|
||||
$nativeEntity->fromStore($storeEntry);
|
||||
|
||||
return $nativeEntity;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user