Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/Server/General-Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ the full request pipeline. Supported changetypes: `add`, `delete`, `modify` (`ad
Operational attributes (`entryUUID`, `createTimestamp`, etc.) are preserved. So `dump()` then `seed()` restores the
entries exactly as they were, since seeding keeps the ones its source supplies rather than stamping new.

**Note**: Running dump on a directory that is not being written to produces the best results. A move during the dump can
skip or repeat part of a subtree.

```php
use FreeDSx\Ldap\LdapServer;
use FreeDSx\Ldap\Ldif\Output\FileLdifOutput;
Expand Down
9 changes: 5 additions & 4 deletions docs/Server/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Every `add`, `modify` and `modify DN` is checked before reaching storage:
- Values conform to their attribute's syntax, and so do RDN values.
- No attribute is given twice, and no value is repeated under an equivalent spelling.
- The structural object class of an existing entry does not change.
- Naming attributes have an equality matching rule, and none of them is operational.
- Naming attributes have an equality matching rule, and none of them is operational or linked.

Failures return one of these codes, with a diagnostic naming the attribute or class:

Expand All @@ -65,7 +65,7 @@ Failures return one of these codes, with a diagnostic naming the attribute or cl
| `constraintViolation` (19) | Several values on a single-valued attribute, a `NO-USER-MODIFICATION` attribute |
| `attributeOrValueExists` (20) | An attribute given twice, a value repeated under an equivalent spelling |
| `objectClassModsProhibited` (69) | Changing the structural object class |
| `namingViolation` (64) | A naming attribute with no equality matching rule, or an operational one |
| `namingViolation` (64) | A naming attribute with no equality matching rule, an operational or linked one |
| `adminLimitExceeded` (11) | More values than the attribute's limit |

## Entry Requirements
Expand Down Expand Up @@ -163,14 +163,14 @@ existing entry.
| Writing `createTimestamp`, `modifyTimestamp`, `creatorsName` or `modifiersName` | Waived | Waived |
| Writing `entryUUID` on Add | Waived | Waived |
| Changing `entryUUID`, writing a derived attribute (`entryDN`, `hasSubordinates`, `subschemaSubentry`) or a back-link such as `memberOf` | Refused | Refused |
| Naming an entry by an operational attribute | Refused | Refused |
| Naming an entry by an operational or linked attribute | Refused | Refused |
| Writing any other `NO-USER-MODIFICATION` attribute | Refused | Waived |
| Required and allowed attributes, a structural object class, undefined types, undefined object classes, single-valued types, naming attributes | Refused | Waived |
| Attribute syntax, the value limit, equivalent values, an attribute description given twice | Refused | Refused |

Each violation is logged and the first that is not waived decides the result code. An entry's identity and the values
the server derives for it are refused with `constraintViolation` in every mode, `Off` included: an entry holds exactly
one `entryUUID`, and a rename may not drop it. An entry named by an operational attribute is refused with
one `entryUUID`, and a rename may not drop it. An entry named by an operational or linked attribute is refused with
`namingViolation` in every mode as well.

A value a relaxed write supplies for one of these attributes is stored as given. A timestamp may not be in the future,
Expand Down Expand Up @@ -359,6 +359,7 @@ leave such a class without an attribute it requires, so the server refuses to st
| Reading a value back | The target's current stored DN, not the spelling that was written. |
| Renaming the target | The new DN appears, with no write to the referring entry and no change to its `modifyTimestamp`. |
| Deleting the target | The reference goes with it, again with no write to the referring entry. |
| Naming an entry by it | Refused with `namingViolation`. The value follows its target, and an RDN value may not change on its own. |
| Value order | The order the references were created in, which RFC 4511 leaves unconstrained. |
| An option-bearing form such as `member;lang-en` | Not linked, and kept on the entry as an ordinary value. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,10 @@ private function makeWriteRequestReplayer(Container $container): WriteRequestRep
private function makeDirectoryDumper(Container $container): DirectoryDumper
{
return new DirectoryDumper(
$container->get(ListEntryInterface::class),
$container->get(SubtreeEnumerator::class),
$container->get(ReadEntryInterface::class)->namingContexts(),
$container->get(FilterEvaluatorInterface::class),
$container->get(LinkedLeafWitness::class),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public function queryLinkIdsForOwner(): string;
*/
public function queryResolveDns(int $count): string;

/**
* Which of $count resolved targets one owner links under an attribute.
*
* Parameters: [owner lc_dn, attr_name_lower, target_entry_id, ...]
*/
public function queryHeldLinkTargets(int $count): string;

/**
* INSERT of $count links.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ public function queryResolveDns(int $count): string
SQL;
}

public function queryHeldLinkTargets(int $count): string
{
$markers = SqlFilterUtility::markers($count);

return <<<SQL
SELECT l.target_entry_id
FROM entries o
JOIN entry_attribute_links l ON l.owner_entry_id = o.entry_id
WHERE o.lc_dn = ?
AND l.attr_name_lower = ?
AND l.target_entry_id IN ($markers)
SQL;
}

public function queryInsertLinks(int $count): string
{
$rows = SqlFilterUtility::markers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Generator;

use function array_chunk;
use function array_filter;
use function array_intersect_key;
use function count;
use function is_array;
use function ksort;
Expand Down Expand Up @@ -151,7 +153,7 @@ public function apply(
}

/**
* Which of the named values the owner links, answered on the connection its own write runs on.
* Which of the named values the owner links, judged on the same locked targets the write then applies to.
*/
public function heldLinkValues(
Dn $owner,
Expand All @@ -163,33 +165,21 @@ public function heldLinkValues(
foreach ($values as $value) {
$asked[Dn::normalized($value)] = $value;
}
// Compared by target id rather than by DN, which a rename committing in between could change.
$resolved = $this->resolve([$attribute => array_values($asked)]);

if ($asked === []) {
if ($resolved === []) {
return [];
}
$held = [];

$chunks = array_chunk(
array_keys($asked),
self::DNS_PER_STATEMENT,
);

foreach ($chunks as $chunk) {
$rows = $this->rowsOf(
$this->dialect->queryHeldLinkValues(count($chunk)),
[
$owner->normalizedString(),
Attribute::normalizeName($attribute),
...$chunk,
],
);

foreach ($rows as $row) {
$held[] = $asked[$this->stringColumn($row['lc_dn'] ?? null)] ?? null;
}
}

return array_values(array_filter($held));
return array_values(array_intersect_key(
$asked,
$this->heldTargets(
$owner,
$attribute,
$resolved,
),
));
}

/**
Expand Down Expand Up @@ -294,6 +284,45 @@ private function resolve(array $values): array
return $ids;
}

/**
* The resolved targets the owner links under the attribute.
*
* @param array<string, int> $resolved
*
* @return array<string, int>
*/
private function heldTargets(
Dn $owner,
string $attribute,
array $resolved,
): array {
$held = [];
$chunks = array_chunk(
array_values($resolved),
self::DNS_PER_STATEMENT,
);

foreach ($chunks as $chunk) {
$rows = $this->rowsOf(
$this->dialect->queryHeldLinkTargets(count($chunk)),
[
$owner->normalizedString(),
Attribute::normalizeName($attribute),
...$chunk,
],
);

foreach ($rows as $row) {
$held[$this->intColumn($row['target_entry_id'] ?? null)] = true;
}
}

return array_filter(
$resolved,
static fn(int $id): bool => isset($held[$id]),
);
}

/**
* The links the entry now stands for, keyed so that two spellings of one target collapse to one row.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,32 @@ public function renameSubtree(
$to,
$base->getDn()->toString(),
);
// Either clash names the new base, since a descendant's name can only be taken where the base exists.
$landsOn = $to->normalize();

$this->translatingRefusal(function () use ($rename): void {
$this->connection->execute(
$this->dialect->queryRenameDescendants(),
$this->renameDescendantParams($rename),
);
});
$this->translatingRefusal(
function () use ($rename): void {
$this->connection->execute(
$this->dialect->queryRenameDescendants(),
$this->renameDescendantParams($rename),
);
},
$landsOn,
);

$this->translatingRefusal(
function () use ($rename, $to): void {
$this->connection->execute($this->dialect->queryRenameEntry(), [
$rename->toDisplay,
$rename->lcTo,
$to->normalize()->getParent()?->toString() ?? '',
$rename->lcFrom,
]);
function () use ($rename, $landsOn): void {
$this->connection->execute(
$this->dialect->queryRenameEntry(),
[
$rename->toDisplay,
$rename->lcTo,
$landsOn->getParent()?->toString() ?? '',
$rename->lcFrom,
],
);
},
$to->normalize(),
$landsOn,
);

// A moved subtree can put many DNs in place at once, so nothing narrows which parked values may settle.
Expand Down Expand Up @@ -329,7 +337,7 @@ private function updateRow(
* Runs a write, turning the driver failures the dialect recognises into the directory conditions they mean.
*
* @param Closure(): void $write
* @param ?Dn $landsOn The DN written, where the write lands on exactly one.
* @param ?Dn $landsOn The DN the write lands on, or under.
* @throws EntryAlreadyExistsException
* @throws EntryUuidTakenException
* @throws DnTooLongException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ public function leavesFirst(Dn $base): Generator
yield from $this->childrenFirst($base->normalizedString());
}

/**
* The base and everything beneath it, each entry before its children, streamed with every value it holds.
*
* @return Generator<int, Entry>
*/
public function parentsFirst(Dn $base): Generator
{
$bases = $this->lister->entriesAt(
[$base],
EntryProjection::unbounded(),
);

foreach ($bases as $entry) {
yield $entry;
yield from $this->childrenAfter($entry->getDn());
}
}

/**
* Children of the entries that are not themselves among them, which removing the entries would strand.
*
Expand Down Expand Up @@ -121,6 +139,34 @@ private function childrenFirst(string $dn): Generator
yield $dn;
}

/**
* Streamed one level at a time, which holds about one batch per level, as nothing here writes against the result.
*
* @return Generator<int, Entry>
*/
private function childrenAfter(Dn $parent): Generator
{
$children = $this->lister->list(StorageListOptions::matchAll(
$parent,
subtree: false,
projection: new EntryProjection(
linkCap: null,
withHasSubordinates: true,
),
));

foreach ($children->entries() as $child) {
$hasChildren = $child->get(AttributeTypeOid::NAME_HAS_SUBORDINATES)?->getValues() === ['TRUE'];
$child->reset(AttributeTypeOid::NAME_HAS_SUBORDINATES);

yield $child;

if ($hasChildren) {
yield from $this->childrenAfter($child->getDn());
}
}
}

/**
* @return Generator<int, array{string, bool}> each child with whether it has children of its own
*/
Expand Down
Loading
Loading