Skip to content

Commit

Permalink
Merge pull request #668 from DirectoryTree/FEATURE-632
Browse files Browse the repository at this point in the history
Going to merge this in to get the release out now. We can add substitution in other areas later if needed.
  • Loading branch information
stevebauman authored Nov 8, 2023
2 parents 6f5b7f2 + 5d8d856 commit 17540a7
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 175 deletions.
12 changes: 5 additions & 7 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function getDn(): ?string
}

/**
* Set the models distinguished name.
* Set the model's distinguished name.
*/
public function setDn(string $dn = null): static
{
Expand All @@ -167,15 +167,15 @@ public function setDn(string $dn = null): static
}

/**
* A mutator for setting the models distinguished name.
* A mutator for setting the model's distinguished name.
*/
public function setDnAttribute(string $dn): static
{
return $this->setRawAttribute('dn', $dn)->setDn($dn);
}

/**
* A mutator for setting the models distinguished name.
* A mutator for setting the model's distinguished name.
*/
public function setDistinguishedNameAttribute(string $dn): static
{
Expand Down Expand Up @@ -886,7 +886,7 @@ protected function performInsert(): void
// but filter out any empty attributes before sending them
// to the server. LDAP servers will throw an exception if
// attributes have been given empty or null values.
$query->insert($this->getDn(), array_filter($this->getAttributes()));
$this->dn = $query->insertAndGetDn($this->getDn(), array_filter($this->getAttributes()));

$this->dispatch('created');

Expand Down Expand Up @@ -1190,12 +1190,10 @@ public function rename(string $rdn, Model|string $newParentDn = null, bool $dele

$this->dispatch('renaming', [$rdn, $newParentDn]);

$this->newQuery()->rename($this->dn, $rdn, $newParentDn, $deleteOldRdn);

// If the model was successfully renamed, we will set
// its new DN so any further updates to the model
// can be performed without any issues.
$this->dn = implode(',', [$rdn, $newParentDn]);
$this->dn = $this->newQuery()->renameAndGetDn($this->dn, $rdn, $newParentDn, $deleteOldRdn);

$map = $this->newDn($this->dn)->assoc();

Expand Down
38 changes: 28 additions & 10 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function getType(): string
*/
public function setBaseDn(Model|string $dn = null): static
{
$this->baseDn = $this->substituteBaseInDn($dn);
$this->baseDn = $this->substituteBaseDn($dn);

return $this;
}
Expand All @@ -292,21 +292,17 @@ public function getDn(): ?string
*/
public function setDn(Model|string $dn = null): static
{
$this->dn = $this->substituteBaseInDn($dn);
$this->dn = $this->substituteBaseDn($dn);

return $this;
}

/**
* Substitute the base DN string template for the current base.
*/
protected function substituteBaseInDn(Model|string $dn = null): string
protected function substituteBaseDn(Model|string $dn = null): string
{
return str_replace(
'{base}',
$this->baseDn ?: '',
(string) ($dn instanceof Model ? $dn->getDn() : $dn)
);
return str_replace('{base}', $this->baseDn ?? '', (string) $dn);
}

/**
Expand Down Expand Up @@ -1384,6 +1380,18 @@ public function isPaginated(): bool
*/
public function insert(string $dn, array $attributes): bool
{
return (bool) $this->insertAndGetDn($dn, $attributes);
}

/**
* Insert an entry into the directory and get the inserted distinguished name.
*
* @throws LdapRecordException
*/
public function insertAndGetDn(string $dn, array $attributes): string|false
{
$dn = $this->substituteBaseDn($dn);

if (empty($dn)) {
throw new LdapRecordException('A new LDAP object must have a distinguished name (dn).');
}
Expand All @@ -1396,7 +1404,7 @@ public function insert(string $dn, array $attributes): bool

return $this->connection->run(
fn (LdapInterface $ldap) => $ldap->add($dn, $attributes)
);
) ? $dn : false;
}

/**
Expand Down Expand Up @@ -1454,9 +1462,19 @@ public function remove(string $dn, array $attributes): bool
*/
public function rename(string $dn, string $rdn, string $newParentDn, bool $deleteOldRdn = true): bool
{
return (bool) $this->renameAndGetDn($dn, $rdn, $newParentDn, $deleteOldRdn);
}

/**
* Rename an entry in the directory and get the new distinguished name.
*/
public function renameAndGetDn(string $dn, string $rdn, string $newParentDn, bool $deleteOldRdn = true): string|false
{
$newParentDn = $this->substituteBaseDn($newParentDn);

return $this->connection->run(
fn (LdapInterface $ldap) => $ldap->rename($dn, $rdn, $newParentDn, $deleteOldRdn)
);
) ? implode(',', [$rdn, $newParentDn]) : false;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Models/ModelEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ public function test_deleting_model_fires_events()

class ModelQueryBuilderSaveStub extends Builder
{
public function insert(string $dn, array $attributes): bool
public function insertAndGetDn(string $dn, array $attributes): string|false
{
return true;
return $dn;
}

public function update(string $dn, array $modifications): bool
Expand Down
Loading

0 comments on commit 17540a7

Please sign in to comment.