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
9 changes: 8 additions & 1 deletion src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,11 +973,18 @@ protected function performInsert(): void

$this->dispatch('creating');

// Some PHP versions prevent non-numerically indexed arrays
// from being sent to the server. To resolve this, we will
// convert the attributes to numerically indexed arrays.
$attributes = array_map('array_values', array_filter($this->getAttributes()));

// Here we perform the insert of new object in the directory,
// 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.
$this->dn = $query->insertAndGetDn($this->getDn(), array_filter($this->getAttributes()));
$this->dn = $query->insertAndGetDn($this->getDn(), $attributes);

$this->attributes = $attributes;

$this->dispatch('created');

Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/Models/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,28 @@ public function test_generated_dns_properly_substitute_base_on_creation()
$this->assertEquals('cn=foo,dc=local,dc=com', $model->getDn());
}

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

DirectoryFake::setup()->getLdapConnection()->expect(
LdapFake::operation('add')->once()->with(fn ($dn) => true, fn ($attributes) => (
collect($attributes)->every(fn ($values) => array_is_list($values))
))->andReturnTrue(),
);

$model = new Entry;

$model->objectclass = ['bar'];
$model->cn = ['invalid' => 'John Doe'];

$model->save();

$this->assertEquals($model->cn, ['John Doe']);
}

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