Skip to content

Commit

Permalink
Support base DN substitution in model DNs
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Sep 12, 2023
1 parent 5bf4b6b commit 5a06a0d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,11 @@ public function isNot(Model $model = null): bool
public function hydrate(array $records): Collection
{
return $this->newCollection($records)->transform(function ($attributes) {
return $attributes instanceof static
? $attributes
: static::newInstance()->setRawAttributes($attributes);
if ($attributes instanceof static) {
return $attributes;
}

return static::newInstance()->setRawAttributes($attributes);
});
}

Expand Down Expand Up @@ -1222,9 +1224,15 @@ public function getUpdateableRdn(string $name): string
*/
public function getCreatableDn(string $name = null, string $attribute = null): string
{
$query = $this->newQuery();

if ($this->in) {
$query->in($this->in);
}

return implode(',', [
$this->getCreatableRdn($name, $attribute),
$this->in ?? $this->newQuery()->getbaseDn(),
$query->getDn() ?? $query->getBaseDn(),
]);
}

Expand Down
10 changes: 3 additions & 7 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function getDn(): ?string
/**
* Set the distinguished name for the query.
*/
public function setDn(Model|string|null $dn = null): static
public function setDn(Model|string $dn = null): static
{
$this->dn = $this->substituteBaseInDn($dn);

Expand All @@ -302,17 +302,13 @@ public function setDn(Model|string|null $dn = null): static
*/
protected function substituteBaseInDn(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);
}

/**
* Alias for setting the distinguished name for the query.
*/
public function in(Model|string|null $dn = null): static
public function in(Model|string $dn = null): static
{
return $this->setDn($dn);
}
Expand Down
23 changes: 20 additions & 3 deletions tests/Unit/Models/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function test_creatable_rdn()

public function test_creatable_dn()
{
Container::getNewInstance()->addConnection(new Connection([
Container::addConnection(new Connection([
'base_dn' => 'dc=acme,dc=org',
]));

Expand Down Expand Up @@ -282,7 +282,7 @@ public function test_removing_attribute_values()
$model->foo = ['bar', 'baz', 'zal'];

$model->removeAttributeValue('missing', 'foo');
$this->assertEquals([], $model->missing);
$this->assertEmpty($model->missing);

$model->removeAttributeValue('foo', 'bar');
$this->assertEquals(['baz', 'zal'], $model->foo);
Expand All @@ -291,7 +291,7 @@ public function test_removing_attribute_values()
$this->assertEquals(['baz', 'zal'], $model->foo);

$model->removeAttributeValue('foo', ['baz', 'zal', 'zee']);
$this->assertEquals([], $model->foo);
$this->assertEmpty($model->foo);

$this->assertEmpty($model->getModifications());
}
Expand Down Expand Up @@ -684,6 +684,8 @@ public function test_create_fills_attributes()

public function test_generated_dns_are_properly_escaped()
{
Container::addConnection(new Connection);

$model = new Entry();

$model->inside('dc=local,dc=com');
Expand All @@ -693,6 +695,21 @@ public function test_generated_dns_are_properly_escaped()
$this->assertEquals('cn=John\5c\2c\3d\2b\3c\3e\3b\5c\23Doe,dc=local,dc=com', $model->getCreatableDn());
}

public function test_generated_dns_properly_substitute_base()
{
Container::addConnection(new Connection([
'base_dn' => 'dc=local,dc=com',
]));

$model = new Entry();

$model->inside('{base}');

$model->cn = 'foo';

$this->assertEquals('cn=foo,dc=local,dc=com', $model->getCreatableDn());
}

public function test_setting_dn_attributes_set_distinguished_name_on_model()
{
$this->assertEquals('foo', (new Entry(['distinguishedname' => 'foo']))->getDn());
Expand Down

0 comments on commit 5a06a0d

Please sign in to comment.