diff --git a/app/Abstracts/Model.php b/app/Abstracts/Model.php index 1f1779c5c..1f7a73aea 100644 --- a/app/Abstracts/Model.php +++ b/app/Abstracts/Model.php @@ -3,6 +3,7 @@ namespace App\Abstracts; use App\Scopes\Company; +use App\Traits\Tenants; use GeneaLabs\LaravelModelCaching\Traits\Cachable; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Eloquent\SoftDeletes; @@ -11,7 +12,7 @@ use Lorisleiva\LaravelSearchString\Concerns\SearchString; abstract class Model extends Eloquent { - use Cachable, SearchString, SoftDeletes, Sortable; + use Cachable, SearchString, SoftDeletes, Sortable, Tenants; protected $dates = ['deleted_at']; diff --git a/app/Models/Auth/Permission.php b/app/Models/Auth/Permission.php index b9e74e407..fa416efa4 100644 --- a/app/Models/Auth/Permission.php +++ b/app/Models/Auth/Permission.php @@ -13,6 +13,8 @@ class Permission extends LaratrustPermission protected $table = 'permissions'; + protected $tenantable = false; + /** * The accessors to append to the model's array form. * diff --git a/app/Models/Auth/Role.php b/app/Models/Auth/Role.php index 8623e41cc..4570d6131 100644 --- a/app/Models/Auth/Role.php +++ b/app/Models/Auth/Role.php @@ -13,6 +13,8 @@ class Role extends LaratrustRole protected $table = 'roles'; + protected $tenantable = false; + /** * The attributes that are mass assignable. * diff --git a/app/Models/Auth/User.php b/app/Models/Auth/User.php index 265a677b4..96888d08f 100644 --- a/app/Models/Auth/User.php +++ b/app/Models/Auth/User.php @@ -19,6 +19,8 @@ class User extends Authenticatable protected $table = 'users'; + protected $tenantable = false; + /** * The attributes that are mass assignable. * diff --git a/app/Models/Common/Company.php b/app/Models/Common/Company.php index 86de79dbe..313413a30 100644 --- a/app/Models/Common/Company.php +++ b/app/Models/Common/Company.php @@ -14,6 +14,8 @@ class Company extends Eloquent protected $table = 'companies'; + protected $tenantable = false; + protected $dates = ['deleted_at']; protected $fillable = ['domain', 'enabled']; diff --git a/app/Models/Common/Media.php b/app/Models/Common/Media.php index abc998ecf..334430d37 100644 --- a/app/Models/Common/Media.php +++ b/app/Models/Common/Media.php @@ -9,5 +9,7 @@ class Media extends BaseMedia { use SoftDeletes; + protected $tenantable = false; + protected $dates = ['deleted_at']; } diff --git a/app/Models/Setting/Setting.php b/app/Models/Setting/Setting.php index 92d6fed1b..d62ebb931 100644 --- a/app/Models/Setting/Setting.php +++ b/app/Models/Setting/Setting.php @@ -3,10 +3,13 @@ namespace App\Models\Setting; use App\Scopes\Company; +use App\Traits\Tenants; use Illuminate\Database\Eloquent\Model as Eloquent; class Setting extends Eloquent { + use Tenants; + protected $table = 'settings'; public $timestamps = false; @@ -55,4 +58,14 @@ class Setting extends Eloquent { 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(); + } } diff --git a/app/Scopes/Company.php b/app/Scopes/Company.php index 4017b3143..025dd9df7 100644 --- a/app/Scopes/Company.php +++ b/app/Scopes/Company.php @@ -17,13 +17,16 @@ class Company implements Scope */ public function apply(Builder $builder, Model $model) { + if (method_exists($model, 'isNotTenantable') && $model->isNotTenantable()) { + return; + } + $table = $model->getTable(); // Skip for specific tables $skip_tables = [ - 'companies', 'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', - 'permissions', 'roles', 'role_companies', 'role_permissions', 'sessions', 'users', 'user_companies', - 'user_dashboards', 'user_permissions', 'user_roles', + 'jobs', 'firewall_ips', 'firewall_logs', 'media', 'mediables', 'migrations', 'notifications', 'role_companies', + 'role_permissions', 'sessions', 'user_companies', 'user_dashboards', 'user_permissions', 'user_roles', ]; if (in_array($table, $skip_tables)) { diff --git a/app/Traits/Tenants.php b/app/Traits/Tenants.php new file mode 100644 index 000000000..5a07c3f13 --- /dev/null +++ b/app/Traits/Tenants.php @@ -0,0 +1,18 @@ +tenantable) && ($this->tenantable === true)); + } + + public function isNotTenantable() + { + return !$this->isTenantable(); + } +}