1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MultiTenantLaravel\App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
abstract class BaseTenant extends Model |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Construct the model with the provided config settings |
11
|
|
|
* or use our fallbacks if none are provided |
12
|
|
|
*/ |
13
|
|
|
public function __construct(array $attributes = []) |
14
|
|
|
{ |
15
|
|
|
$this->setTable(config('multi-tenant.table_name')); |
16
|
|
|
|
17
|
|
|
$this->setFillable(); |
18
|
|
|
|
19
|
|
|
// parent construct must go below property customizations |
20
|
|
|
parent::__construct($attributes); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Merge the fillable fields with any provided in the extending class |
25
|
|
|
*/ |
26
|
|
|
public function setFillable() |
27
|
|
|
{ |
28
|
|
|
$fillable = [ |
29
|
|
|
'owner_id', |
30
|
|
|
'slug', |
31
|
|
|
'name' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$this->fillable(array_merge($this->fillable, $fillable)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Merge the fillable fields with any provided in the extending class |
39
|
|
|
*/ |
40
|
|
|
public function owner() |
41
|
|
|
{ |
42
|
|
|
return $this->belongsTo(config('multi-tenant.user_class'), 'owner_id'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The features relationship |
47
|
|
|
*/ |
48
|
|
|
public function features() |
49
|
|
|
{ |
50
|
|
|
return $this->belongsToMany( |
51
|
|
|
config('multi-tenant.feature_class'), |
52
|
|
|
'feature_' . str_singular(config('multi-tenant.table_name')), |
53
|
|
|
str_singular(config('multi-tenant.table_name')) . '_id', |
54
|
|
|
'feature_id' |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The users relationship |
60
|
|
|
*/ |
61
|
|
|
public function users() |
62
|
|
|
{ |
63
|
|
|
return $this->belongsToMany( |
64
|
|
|
config('multi-tenant.user_class'), |
65
|
|
|
str_singular(config('multi-tenant.table_name')) . '_user', |
66
|
|
|
str_singular(config('multi-tenant.table_name')) . '_id', |
67
|
|
|
'user_id' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Assign the given user to the teannt |
73
|
|
|
* |
74
|
|
|
* @param BaseUser $user |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function assignUser(BaseUser $user) |
79
|
|
|
{ |
80
|
|
|
return $this->users()->syncWithoutDetaching($user); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Test that user has access to the this tenant |
85
|
|
|
* |
86
|
|
|
* @param BaseUser $user |
87
|
|
|
* |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
|
|
public function hasUser(BaseUser $user) |
91
|
|
|
{ |
92
|
|
|
return $this->users()->where('user_id', $user->id)->exists(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Assign the given feature to the teannt |
97
|
|
|
* |
98
|
|
|
* @param BaseFeature $feature |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function assignFeature(BaseFeature $feature) |
103
|
|
|
{ |
104
|
|
|
return $this->features()->syncWithoutDetaching($feature); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Test that the tenant has access to a given feature |
109
|
|
|
* |
110
|
|
|
* @param BaseFeature $feature |
111
|
|
|
* |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
public function hasFeature(BaseFeature $feature) |
115
|
|
|
{ |
116
|
|
|
return $this->features()->where('feature_id', $feature->id)->exists(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|