Skip to content

Commit 17540a7

Browse files
authored
Merge pull request #668 from DirectoryTree/FEATURE-632
Going to merge this in to get the release out now. We can add substitution in other areas later if needed.
2 parents 6f5b7f2 + 5d8d856 commit 17540a7

File tree

6 files changed

+245
-175
lines changed

6 files changed

+245
-175
lines changed

src/Models/Model.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function getDn(): ?string
157157
}
158158

159159
/**
160-
* Set the models distinguished name.
160+
* Set the model's distinguished name.
161161
*/
162162
public function setDn(string $dn = null): static
163163
{
@@ -167,15 +167,15 @@ public function setDn(string $dn = null): static
167167
}
168168

169169
/**
170-
* A mutator for setting the models distinguished name.
170+
* A mutator for setting the model's distinguished name.
171171
*/
172172
public function setDnAttribute(string $dn): static
173173
{
174174
return $this->setRawAttribute('dn', $dn)->setDn($dn);
175175
}
176176

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

891891
$this->dispatch('created');
892892

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

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

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

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

src/Query/Builder.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public function getType(): string
266266
*/
267267
public function setBaseDn(Model|string $dn = null): static
268268
{
269-
$this->baseDn = $this->substituteBaseInDn($dn);
269+
$this->baseDn = $this->substituteBaseDn($dn);
270270

271271
return $this;
272272
}
@@ -292,21 +292,17 @@ public function getDn(): ?string
292292
*/
293293
public function setDn(Model|string $dn = null): static
294294
{
295-
$this->dn = $this->substituteBaseInDn($dn);
295+
$this->dn = $this->substituteBaseDn($dn);
296296

297297
return $this;
298298
}
299299

300300
/**
301301
* Substitute the base DN string template for the current base.
302302
*/
303-
protected function substituteBaseInDn(Model|string $dn = null): string
303+
protected function substituteBaseDn(Model|string $dn = null): string
304304
{
305-
return str_replace(
306-
'{base}',
307-
$this->baseDn ?: '',
308-
(string) ($dn instanceof Model ? $dn->getDn() : $dn)
309-
);
305+
return str_replace('{base}', $this->baseDn ?? '', (string) $dn);
310306
}
311307

312308
/**
@@ -1384,6 +1380,18 @@ public function isPaginated(): bool
13841380
*/
13851381
public function insert(string $dn, array $attributes): bool
13861382
{
1383+
return (bool) $this->insertAndGetDn($dn, $attributes);
1384+
}
1385+
1386+
/**
1387+
* Insert an entry into the directory and get the inserted distinguished name.
1388+
*
1389+
* @throws LdapRecordException
1390+
*/
1391+
public function insertAndGetDn(string $dn, array $attributes): string|false
1392+
{
1393+
$dn = $this->substituteBaseDn($dn);
1394+
13871395
if (empty($dn)) {
13881396
throw new LdapRecordException('A new LDAP object must have a distinguished name (dn).');
13891397
}
@@ -1396,7 +1404,7 @@ public function insert(string $dn, array $attributes): bool
13961404

13971405
return $this->connection->run(
13981406
fn (LdapInterface $ldap) => $ldap->add($dn, $attributes)
1399-
);
1407+
) ? $dn : false;
14001408
}
14011409

14021410
/**
@@ -1454,9 +1462,19 @@ public function remove(string $dn, array $attributes): bool
14541462
*/
14551463
public function rename(string $dn, string $rdn, string $newParentDn, bool $deleteOldRdn = true): bool
14561464
{
1465+
return (bool) $this->renameAndGetDn($dn, $rdn, $newParentDn, $deleteOldRdn);
1466+
}
1467+
1468+
/**
1469+
* Rename an entry in the directory and get the new distinguished name.
1470+
*/
1471+
public function renameAndGetDn(string $dn, string $rdn, string $newParentDn, bool $deleteOldRdn = true): string|false
1472+
{
1473+
$newParentDn = $this->substituteBaseDn($newParentDn);
1474+
14571475
return $this->connection->run(
14581476
fn (LdapInterface $ldap) => $ldap->rename($dn, $rdn, $newParentDn, $deleteOldRdn)
1459-
);
1477+
) ? implode(',', [$rdn, $newParentDn]) : false;
14601478
}
14611479

14621480
/**

tests/Unit/Models/ModelEventTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public function test_deleting_model_fires_events()
151151

152152
class ModelQueryBuilderSaveStub extends Builder
153153
{
154-
public function insert(string $dn, array $attributes): bool
154+
public function insertAndGetDn(string $dn, array $attributes): string|false
155155
{
156-
return true;
156+
return $dn;
157157
}
158158

159159
public function update(string $dn, array $modifications): bool

0 commit comments

Comments
 (0)