added tenantable attribute to models

This commit is contained in:
denisdulici 2020-05-02 14:33:41 +03:00
parent a0c154ded4
commit 8c4ca9d73c
9 changed files with 49 additions and 4 deletions

View File

@ -3,6 +3,7 @@
namespace App\Abstracts; namespace App\Abstracts;
use App\Scopes\Company; use App\Scopes\Company;
use App\Traits\Tenants;
use GeneaLabs\LaravelModelCaching\Traits\Cachable; use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -11,7 +12,7 @@ use Lorisleiva\LaravelSearchString\Concerns\SearchString;
abstract class Model extends Eloquent abstract class Model extends Eloquent
{ {
use Cachable, SearchString, SoftDeletes, Sortable; use Cachable, SearchString, SoftDeletes, Sortable, Tenants;
protected $dates = ['deleted_at']; protected $dates = ['deleted_at'];

View File

@ -13,6 +13,8 @@ class Permission extends LaratrustPermission
protected $table = 'permissions'; protected $table = 'permissions';
protected $tenantable = false;
/** /**
* The accessors to append to the model's array form. * The accessors to append to the model's array form.
* *

View File

@ -13,6 +13,8 @@ class Role extends LaratrustRole
protected $table = 'roles'; protected $table = 'roles';
protected $tenantable = false;
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *

View File

@ -19,6 +19,8 @@ class User extends Authenticatable
protected $table = 'users'; protected $table = 'users';
protected $tenantable = false;
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *

View File

@ -14,6 +14,8 @@ class Company extends Eloquent
protected $table = 'companies'; protected $table = 'companies';
protected $tenantable = false;
protected $dates = ['deleted_at']; protected $dates = ['deleted_at'];
protected $fillable = ['domain', 'enabled']; protected $fillable = ['domain', 'enabled'];

View File

@ -9,5 +9,7 @@ class Media extends BaseMedia
{ {
use SoftDeletes; use SoftDeletes;
protected $tenantable = false;
protected $dates = ['deleted_at']; protected $dates = ['deleted_at'];
} }

View File

@ -3,10 +3,13 @@
namespace App\Models\Setting; namespace App\Models\Setting;
use App\Scopes\Company; use App\Scopes\Company;
use App\Traits\Tenants;
use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\Model as Eloquent;
class Setting extends Eloquent class Setting extends Eloquent
{ {
use Tenants;
protected $table = 'settings'; protected $table = 'settings';
public $timestamps = false; public $timestamps = false;
@ -55,4 +58,14 @@ class Setting extends Eloquent
{ {
return $query->where($this->table . '.company_id', '=', $company_id); return $query->where($this->table . '.company_id', '=', $company_id);
} }
public function isTenantable()
{
return (isset($this->tenantable) && ($this->tenantable === true));
}
public function isNotTenantable()
{
return !$this->isTenantable();
}
} }

View File

@ -17,13 +17,16 @@ class Company implements Scope
*/ */
public function apply(Builder $builder, Model $model) public function apply(Builder $builder, Model $model)
{ {
if (method_exists($model, 'isNotTenantable') && $model->isNotTenantable()) {
return;
}
$table = $model->getTable(); $table = $model->getTable();
// Skip for specific tables // Skip for specific tables
$skip_tables = [ $skip_tables = [
'companies', 'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', 'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', 'role_companies',
'permissions', 'roles', 'role_companies', 'role_permissions', 'sessions', 'users', 'user_companies', 'role_permissions', 'sessions', 'user_companies', 'user_dashboards', 'user_permissions', 'user_roles',
'user_dashboards', 'user_permissions', 'user_roles',
]; ];
if (in_array($table, $skip_tables)) { if (in_array($table, $skip_tables)) {

18
app/Traits/Tenants.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace App\Traits;
trait Tenants
{
protected $tenantable = true;
public function isTenantable()
{
return (isset($this->tenantable) && ($this->tenantable === true));
}
public function isNotTenantable()
{
return !$this->isTenantable();
}
}