1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 21:14:03 +00:00

Allow mapping nested tenant properties to mail config (#20)

* Use data_get() to allow mapping nested tenant attributes to mail config

* Fix code style (php-cs-fixer)

* Test the data_get() change

* Improve code, add info to docblock

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
lukinovec 2023-12-18 12:42:03 +01:00 committed by GitHub
parent ca400b51d2
commit ea5a7463b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 3 deletions

View file

@ -16,7 +16,8 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
* *
* For example: * For example:
* [ * [
* 'config.key.name' => 'tenant_property', * 'config.key.username' => 'tenant_property',
* 'config.key.password' => 'nested.tenant_property',
* ] * ]
*/ */
public static array $credentialsMap = []; public static array $credentialsMap = [];
@ -60,9 +61,9 @@ class MailTenancyBootstrapper implements TenancyBootstrapper
protected function setConfig(Tenant $tenant): void protected function setConfig(Tenant $tenant): void
{ {
foreach (static::$credentialsMap as $configKey => $storageKey) { 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->originalConfig[$configKey] ??= $this->config->get($configKey);
$this->config->set($configKey, $override); $this->config->set($configKey, $override);

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddCustomColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tenants', function (Blueprint $table) {
$table->json('mail')->nullable();
});
}
public function down()
{
}
}

View file

@ -54,6 +54,18 @@ test('mailer transport uses the correct credentials', function() {
assertMailerTransportUsesPassword($newTenantPassword); assertMailerTransportUsesPassword($newTenantPassword);
tenancy()->end(); 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 // Assert mailer uses the default password after tenancy ends
assertMailerTransportUsesPassword($defaultPassword); assertMailerTransportUsesPassword($defaultPassword);
}); });