diff --git a/src/Bootstrappers/MailTenancyBootstrapper.php b/src/Bootstrappers/MailTenancyBootstrapper.php index 7f15f547..9880d2c4 100644 --- a/src/Bootstrappers/MailTenancyBootstrapper.php +++ b/src/Bootstrappers/MailTenancyBootstrapper.php @@ -16,7 +16,8 @@ class MailTenancyBootstrapper implements TenancyBootstrapper * * For example: * [ - * 'config.key.name' => 'tenant_property', + * 'config.key.username' => 'tenant_property', + * 'config.key.password' => 'nested.tenant_property', * ] */ public static array $credentialsMap = []; @@ -60,9 +61,9 @@ class MailTenancyBootstrapper implements TenancyBootstrapper protected function setConfig(Tenant $tenant): void { foreach (static::$credentialsMap as $configKey => $storageKey) { - $override = $tenant->$storageKey; + $override = data_get($tenant, $storageKey); - if (array_key_exists($storageKey, $tenant->getAttributes())) { + if ($override !== null) { $this->originalConfig[$configKey] ??= $this->config->get($configKey); $this->config->set($configKey, $override); diff --git a/tests/Etc/mail_migrations/2019_08_08_000001_add_custom_column.php b/tests/Etc/mail_migrations/2019_08_08_000001_add_custom_column.php new file mode 100644 index 00000000..5ff48f07 --- /dev/null +++ b/tests/Etc/mail_migrations/2019_08_08_000001_add_custom_column.php @@ -0,0 +1,26 @@ +json('mail')->nullable(); + }); + } + + public function down() + { + } +} diff --git a/tests/MailTest.php b/tests/MailTest.php index a46b2466..03f8aa63 100644 --- a/tests/MailTest.php +++ b/tests/MailTest.php @@ -54,6 +54,18 @@ test('mailer transport uses the correct credentials', function() { assertMailerTransportUsesPassword($newTenantPassword); tenancy()->end(); + // Assert nested tenant properties can be mapped to mail config (e.g. using dot notation) + // Add 'mail' JSON column to tenants table where smtp_password will be stored + pest()->artisan('tenants:migrate', [ + '--path' => __DIR__ . '/Etc/mail_migrations', + '--realpath' => true, + ])->assertExitCode(0); + + MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'mail.smtp_password']; + tenancy()->initialize($tenant = Tenant::create(['mail' => ['smtp_password' => $nestedTenantPassword = 'nested']])); + assertMailerTransportUsesPassword($nestedTenantPassword); + tenancy()->end(); + // Assert mailer uses the default password after tenancy ends assertMailerTransportUsesPassword($defaultPassword); });