Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117943105
D4752.1775480373.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
D4752.1775480373.diff
View Options
diff --git a/src/app/Console/Command.php b/src/app/Console/Command.php
--- a/src/app/Console/Command.php
+++ b/src/app/Console/Command.php
@@ -29,6 +29,12 @@
*/
protected $dangerous = false;
+ /**
+ * Tenant context for the command
+ *
+ * @var ?int
+ */
+ protected $tenantId;
/**
* Apply current tenant context to the query
@@ -43,21 +49,26 @@
return $object;
}
+ if (!$this->tenantId) {
+ return $object;
+ }
+
$modelsWithOwner = [
\App\Wallet::class,
];
- $tenantId = \config('app.tenant_id');
-
// Add tenant filter
if (in_array(\App\Traits\BelongsToTenantTrait::class, class_uses($object::class))) {
- $object = $object->withEnvTenantContext();
+ $context = new \App\User();
+ $context->tenant_id = $this->tenantId;
+
+ $object = $object->withObjectTenantContext($context);
} elseif (in_array($object::class, $modelsWithOwner)) {
- $object = $object->whereExists(function ($query) use ($tenantId) {
+ $object = $object->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('users')
->whereRaw('wallets.user_id = users.id')
- ->whereRaw('users.tenant_id ' . ($tenantId ? "= $tenantId" : 'is null'));
+ ->whereRaw("users.tenant_id = {$this->tenantId}");
});
}
@@ -138,6 +149,10 @@
->where($objectTitle, $objectIdOrTitle)->first();
}
+ if (!$this->tenantId && $object && !empty($object->tenant_id)) {
+ $this->tenantId = $object->tenant_id;
+ }
+
return $object;
}
@@ -227,13 +242,11 @@
if (!$confirmation) {
$this->info("Better safe than sorry.");
- return false;
+ exit(0);
}
$this->info("Vámonos!");
}
-
- return true;
}
/**
diff --git a/src/app/Console/ObjectDeleteCommand.php b/src/app/Console/ObjectDeleteCommand.php
--- a/src/app/Console/ObjectDeleteCommand.php
+++ b/src/app/Console/ObjectDeleteCommand.php
@@ -31,11 +31,7 @@
*/
public function handle()
{
- $result = parent::handle();
-
- if (!$result) {
- return 1;
- }
+ parent::handle();
$argument = $this->argument($this->objectName);
diff --git a/src/app/Console/ObjectListCommand.php b/src/app/Console/ObjectListCommand.php
--- a/src/app/Console/ObjectListCommand.php
+++ b/src/app/Console/ObjectListCommand.php
@@ -20,6 +20,10 @@
$this->signature .= "{$this->objectName}s";
}
+ if (in_array(\App\Traits\BelongsToTenantTrait::class, class_uses($this->objectClass))) {
+ $this->signature .= " {--tenant= : Limit results to the specified tenant}";
+ }
+
if ($this->isSoftDeletable($this->objectClass)) {
$this->signature .= " {--with-deleted : Include deleted {$this->objectName}s}";
}
@@ -43,7 +47,11 @@
$objects = new $this->objectClass();
}
- $objects = $this->applyTenant($objects);
+ // @phpstan-ignore-next-line
+ if ($this->hasOption('tenant') && ($tenant = intval($this->option('tenant')))) {
+ $this->tenantId = $tenant;
+ $objects = $this->applyTenant($objects);
+ }
foreach ($this->option('filter') as $filter) {
$objects = $this->applyFilter($objects, $filter);
diff --git a/src/tests/Feature/Console/DomainsTest.php b/src/tests/Feature/Console/DomainsTest.php
--- a/src/tests/Feature/Console/DomainsTest.php
+++ b/src/tests/Feature/Console/DomainsTest.php
@@ -11,18 +11,27 @@
*/
public function testHandle(): void
{
- $domain = \App\Domain::where('namespace', 'kolab.org')->first();
+ $domain1 = \App\Domain::where('namespace', 'kolab.org')->first();
+ $domain2 = \App\Domain::whereNot('tenant_id', $domain1->tenant_id)->first();
- // Existing domain
- $code = \Artisan::call("domains");
+ // List domains for a specified tenant
+ $code = \Artisan::call("domains --tenant={$domain1->tenant_id}");
$output = trim(\Artisan::output());
+
$this->assertSame(0, $code);
+ $this->assertStringContainsString((string) $domain1->id, $output);
+ $this->assertStringNotContainsString((string) $domain2->id, $output);
+
+ // List domains of all tenants
+ $code = \Artisan::call("domains");
+ $output = trim(\Artisan::output());
- $this->assertTrue(strpos($output, (string) $domain->id) !== false);
+ $this->assertSame(0, $code);
+ $this->assertStringContainsString((string) $domain1->id, $output);
+ $this->assertStringContainsString((string) $domain2->id, $output);
- // TODO: Test --deleted argument
+ // TODO: Test --with-deleted argument
// TODO: Test output format and other attributes
- // TODO: Test tenant context
$this->markTestIncomplete();
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Apr 6, 12:59 PM (11 h, 12 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18837518
Default Alt Text
D4752.1775480373.diff (5 KB)
Attached To
Mode
D4752: Make CLI commands tenant independent
Attached
Detach File
Event Timeline