diff --git a/src/Models/Model.php b/src/Models/Model.php index 4e37a847..b17d0793 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -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'); diff --git a/tests/Unit/Models/ModelTest.php b/tests/Unit/Models/ModelTest.php index b8daaaa8..507a924c 100644 --- a/tests/Unit/Models/ModelTest.php +++ b/tests/Unit/Models/ModelTest.php @@ -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());