Changeset View
Changeset View
Standalone View
Standalone View
src/app/Tenant.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\Traits\SettingsTrait; | |||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
/** | /** | ||||
* The eloquent definition of a Tenant. | * The eloquent definition of a Tenant. | ||||
* | * | ||||
* @property int $id | * @property int $id | ||||
* @property string $title | * @property string $title | ||||
*/ | */ | ||||
class Tenant extends Model | class Tenant extends Model | ||||
{ | { | ||||
use SettingsTrait; | |||||
protected $fillable = [ | protected $fillable = [ | ||||
'id', | 'id', | ||||
'title', | 'title', | ||||
]; | ]; | ||||
protected $keyType = 'bigint'; | protected $keyType = 'bigint'; | ||||
/** | /** | ||||
* Utility method to get tenant-specific system setting. | |||||
* If the setting is not specified for the tenant a system-wide value will be returned. | |||||
* | |||||
* @param int $tenantId Tenant identifier | |||||
* @param string $key Setting name | |||||
* | |||||
* @return mixed Setting value | |||||
*/ | |||||
public static function getConfig($tenantId, string $key) | |||||
{ | |||||
// Cache the tenant instance in memory | |||||
static $tenant; | |||||
if (empty($tenant) || $tenant->id != $tenantId) { | |||||
$tenant = null; | |||||
if ($tenantId) { | |||||
$tenant = self::findOrFail($tenantId); | |||||
} | |||||
} | |||||
// Supported options (TODO: document this somewhere): | |||||
// - app.name (tenants.title will be returned) | |||||
// - app.public_url and app.url | |||||
// - app.support_url | |||||
// - mail.from.address and mail.from.name | |||||
// - mail.reply_to.address and mail.reply_to.name | |||||
// - app.kb.account_delete and app.kb.account_suspended | |||||
if ($key == 'app.name') { | |||||
return $tenant ? $tenant->title : \config($key); | |||||
} | |||||
$value = $tenant ? $tenant->getSetting($key) : null; | |||||
return $value !== null ? $value : \config($key); | |||||
} | |||||
/** | |||||
* Discounts assigned to this tenant. | * Discounts assigned to this tenant. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function discounts() | public function discounts() | ||||
{ | { | ||||
return $this->hasMany('App\Discount'); | return $this->hasMany('App\Discount'); | ||||
} | } | ||||
/** | /** | ||||
* Any (additional) settings of this tenant. | |||||
* | |||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | |||||
*/ | |||||
public function settings() | |||||
{ | |||||
return $this->hasMany('App\TenantSetting'); | |||||
} | |||||
/** | |||||
* SignupInvitations assigned to this tenant. | * SignupInvitations assigned to this tenant. | ||||
* | * | ||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function signupInvitations() | public function signupInvitations() | ||||
{ | { | ||||
return $this->hasMany('App\SignupInvitation'); | return $this->hasMany('App\SignupInvitation'); | ||||
} | } | ||||
Show All 13 Lines |