Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Command.php
Show First 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | public function getObject($objectClass, $objectIdOrTitle, $objectTitle = null, $withDeleted = false) | ||||
if (!$withDeleted) { | if (!$withDeleted) { | ||||
$withDeleted = $this->hasOption('with-deleted') && $this->option('with-deleted'); | $withDeleted = $this->hasOption('with-deleted') && $this->option('with-deleted'); | ||||
} | } | ||||
$object = $this->getObjectModel($objectClass, $withDeleted)->find($objectIdOrTitle); | $object = $this->getObjectModel($objectClass, $withDeleted)->find($objectIdOrTitle); | ||||
if (!$object && !empty($objectTitle)) { | if (!$object && !empty($objectTitle)) { | ||||
$object = $this->getObjectModel($objectClass, $withDeleted) | $object = $this->getObjectModel($objectClass, $withDeleted) | ||||
->where($objectTitle, $objectIdOrTitle)->first(); | ->where($objectTitle, $objectIdOrTitle)->first(); | ||||
machniak: We do this in getObjectModel() already. | |||||
} | } | ||||
return $object; | return $object; | ||||
} | } | ||||
/** | /** | ||||
* Returns a preconfigured Model object for a specified class. | * Returns a preconfigured Model object for a specified class. | ||||
* | * | ||||
* @param string $objectClass The name of the class | * @param string $objectClass The name of the class | ||||
* @param bool $withDeleted Include withTrashed() query | * @param bool $withDeleted Include withTrashed() query | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
protected function getObjectModel($objectClass, $withDeleted = false) | protected function getObjectModel($objectClass, $withDeleted = false) | ||||
{ | { | ||||
if ($withDeleted) { | if ($withDeleted) { | ||||
$model = $objectClass::withTrashed(); | $model = $objectClass::withTrashed(); | ||||
} else { | } else { | ||||
$model = new $objectClass(); | $model = new $objectClass(); | ||||
} | } | ||||
if ($this->commandPrefix == 'scalpel') { | |||||
return $model; | |||||
} | |||||
$modelsWithTenant = [ | $modelsWithTenant = [ | ||||
\App\Discount::class, | \App\Discount::class, | ||||
\App\Domain::class, | \App\Domain::class, | ||||
\App\Group::class, | \App\Group::class, | ||||
\App\Package::class, | \App\Package::class, | ||||
\App\Plan::class, | \App\Plan::class, | ||||
\App\Sku::class, | \App\Sku::class, | ||||
\App\User::class, | \App\User::class, | ||||
]; | ]; | ||||
$modelsWithOwner = [ | $modelsWithOwner = [ | ||||
\App\Wallet::class, | \App\Wallet::class, | ||||
]; | ]; | ||||
$tenant_id = \config('app.tenant_id'); | $tenantId = \config('app.tenant_id'); | ||||
// Add tenant filter | // Add tenant filter | ||||
if (in_array($objectClass, $modelsWithTenant)) { | if (in_array($objectClass, $modelsWithTenant)) { | ||||
$model = $model->withEnvTenant(); | $model = $model->withEnvTenantContext(); | ||||
} elseif (in_array($objectClass, $modelsWithOwner)) { | } elseif (in_array($objectClass, $modelsWithOwner)) { | ||||
$model = $model->whereExists(function ($query) use ($tenant_id) { | $model = $model->whereExists(function ($query) use ($tenantId) { | ||||
$query->select(DB::raw(1)) | $query->select(DB::raw(1)) | ||||
->from('users') | ->from('users') | ||||
->whereRaw('wallets.user_id = users.id') | ->whereRaw('wallets.user_id = users.id') | ||||
->whereRaw('users.tenant_id ' . ($tenant_id ? "= $tenant_id" : 'is null')); | ->whereRaw('users.tenant_id ' . ($tenantId ? "= $tenantId" : 'is null')); | ||||
}); | }); | ||||
} | } | ||||
// TODO: tenant check for Entitlement, Transaction, etc. | |||||
return $model; | return $model; | ||||
} | } | ||||
/** | /** | ||||
* Find the user. | * Find the user. | ||||
* | * | ||||
* @param string $user User ID or email | * @param string $user User ID or email | ||||
* @param bool $withDeleted Include deleted | * @param bool $withDeleted Include deleted | ||||
▲ Show 20 Lines • Show All 82 Lines • Show Last 20 Lines |
We do this in getObjectModel() already.