Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/AppServiceProvider.php
<?php | <?php | ||||
namespace App\Providers; | namespace App\Providers; | ||||
use Illuminate\Database\Eloquent\Model; | |||||
use Illuminate\Database\Query\Builder; | use Illuminate\Database\Query\Builder; | ||||
use Illuminate\Support\Facades\Blade; | use Illuminate\Support\Facades\Blade; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
use Illuminate\Support\Facades\Schema; | use Illuminate\Support\Facades\Schema; | ||||
use Illuminate\Support\ServiceProvider; | use Illuminate\Support\ServiceProvider; | ||||
class AppServiceProvider extends ServiceProvider | class AppServiceProvider extends ServiceProvider | ||||
{ | { | ||||
Show All 38 Lines | public function boot() | ||||
// Log SQL queries in debug mode | // Log SQL queries in debug mode | ||||
if (\config('app.debug')) { | if (\config('app.debug')) { | ||||
DB::listen(function ($query) { | DB::listen(function ($query) { | ||||
\Log::debug(sprintf('[SQL] %s [%s]', $query->sql, implode(', ', $query->bindings))); | \Log::debug(sprintf('[SQL] %s [%s]', $query->sql, implode(', ', $query->bindings))); | ||||
}); | }); | ||||
} | } | ||||
// Register some template helpers | // Register some template helpers | ||||
Blade::directive('theme_asset', function ($path) { | Blade::directive( | ||||
'theme_asset', | |||||
function ($path) { | |||||
$path = trim($path, '/\'"'); | $path = trim($path, '/\'"'); | ||||
return "<?php echo secure_asset('themes/' . \$env['app.theme'] . '/' . '$path'); ?>"; | return "<?php echo secure_asset('themes/' . \$env['app.theme'] . '/' . '$path'); ?>"; | ||||
}); | } | ||||
); | |||||
// Query builder 'withEnvTenant' macro | Builder::macro( | ||||
Builder::macro('withEnvTenant', function (string $table = null) { | 'withEnvTenantContext', | ||||
$tenant_id = \config('app.tenant_id'); | function (string $table = null) { | ||||
$tenantId = \config('app.tenant_id'); | |||||
if ($tenant_id) { | if ($tenantId) { | ||||
/** @var Builder $this */ | /** @var Builder $this */ | ||||
return $this->where(($table ? "$table." : '') . 'tenant_id', $tenant_id); | return $this->where(($table ? "$table." : "") . "tenant_id", $tenantId); | ||||
} | |||||
return $this->whereNull(($table ? "$table." : "") . "tenant_id"); | |||||
} | } | ||||
); | |||||
Builder::macro( | |||||
'withObjectTenantContext', | |||||
function (Model $object, string $table = null) { | |||||
machniak: I guess we could be more specific on the $object type. We expect a Model. | |||||
// backend artisan cli | |||||
if (app()->runningInConsole()) { | |||||
/** @var Builder $this */ | /** @var Builder $this */ | ||||
return $this->whereNull(($table ? "$table." : '') . 'tenant_id'); | return $this->where(($table ? "$table." : "") . "tenant_id", $object->tenant_id); | ||||
}); | } | ||||
// Query builder 'withUserTenant' macro | $subject = auth()->user(); | ||||
Done Inline ActionsI don't like this method. 'withObjectTenantContext' means "with object's tenant" not "with object's tenant depending on who's asking". machniak: I don't like this method. 'withObjectTenantContext' means "with object's tenant" not "with… | |||||
Done Inline ActionsOnly administrators are supposed to be allowed to switch tenant contexts from their own, or the APP_TENANT_ID, in to the object's tenant context. This is a sanity check against abuse of the macro. That said, we should also add a CLI check. vanmeeuwen: Only administrators are supposed to be allowed to switch tenant contexts from their own, or the… | |||||
Builder::macro('withUserTenant', function (string $table = null) { | |||||
$tenant_id = auth()->user()->tenant_id; | |||||
if ($tenant_id) { | if ($subject->role == "admin") { | ||||
/** @var Builder $this */ | /** @var Builder $this */ | ||||
return $this->where(($table ? "$table." : '') . 'tenant_id', $tenant_id); | return $this->where(($table ? "$table." : "") . "tenant_id", $object->tenant_id); | ||||
} | } | ||||
$tenantId = $subject->tenant_id; | |||||
if ($tenantId) { | |||||
/** @var Builder $this */ | /** @var Builder $this */ | ||||
return $this->whereNull(($table ? "$table." : '') . 'tenant_id'); | return $this->where(($table ? "$table." : "") . "tenant_id", $tenantId); | ||||
}); | } | ||||
return $this->whereNull(($table ? "$table." : "") . "tenant_id"); | |||||
} | |||||
); | |||||
Builder::macro( | |||||
'withSubjectTenantContext', | |||||
function (string $table = null) { | |||||
// backend artisan cli | |||||
if (app()->runningInConsole()) { | |||||
/** @var Builder $this */ | |||||
return $this->where(($table ? "$table." : "") . "tenant_id", $object->tenant_id); | |||||
machniakUnsubmitted Done Inline ActionsThere's no $object defined here. machniak: There's no $object defined here. | |||||
} | |||||
$tenantId = auth()->user()->tenant_id; | |||||
if ($tenantId) { | |||||
/** @var Builder $this */ | |||||
return $this->where(($table ? "$table." : "") . "tenant_id", $tenantId); | |||||
} | |||||
return $this->whereNull(($table ? "$table." : "") . "tenant_id"); | |||||
} | |||||
); | |||||
// Query builder 'whereLike' mocro | // Query builder 'whereLike' mocro | ||||
Builder::macro('whereLike', function (string $column, string $search, int $mode = 0) { | Builder::macro( | ||||
'whereLike', | |||||
function (string $column, string $search, int $mode = 0) { | |||||
$search = addcslashes($search, '%_'); | $search = addcslashes($search, '%_'); | ||||
switch ($mode) { | switch ($mode) { | ||||
case 2: | case 2: | ||||
$search .= '%'; | $search .= '%'; | ||||
break; | break; | ||||
case 1: | case 1: | ||||
$search = '%' . $search; | $search = '%' . $search; | ||||
break; | break; | ||||
default: | default: | ||||
$search = '%' . $search . '%'; | $search = '%' . $search . '%'; | ||||
} | } | ||||
/** @var Builder $this */ | /** @var Builder $this */ | ||||
return $this->where($column, 'like', $search); | return $this->where($column, 'like', $search); | ||||
}); | } | ||||
); | |||||
} | } | ||||
} | } |
I guess we could be more specific on the $object type. We expect a Model.