1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MultiTenantLaravel\App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notifiable; |
6
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
abstract class BaseUser extends Authenticatable |
10
|
|
|
{ |
11
|
|
|
use Notifiable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The attributes that are mass assignable. |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $fillable = [ |
19
|
|
|
'name', 'email', 'password', |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The attributes that should be hidden for arrays. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $hidden = [ |
28
|
|
|
'password', 'remember_token', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return all of the tenants this user is the owner of |
33
|
|
|
*/ |
34
|
|
|
public function roles() |
35
|
|
|
{ |
36
|
|
|
return $this->belongsToMany( |
37
|
|
|
config('multi-tenant.role_class'), |
38
|
|
|
'multi_tenant_role_user', |
39
|
|
|
'user_id', |
40
|
|
|
'multi_tenant_role_id' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Assign the provided role to the user |
46
|
|
|
* |
47
|
|
|
* @param BaseRole $role |
48
|
|
|
* |
49
|
|
|
* @return BaseUser |
50
|
|
|
*/ |
51
|
|
|
public function assignRole(BaseRole $role) |
52
|
|
|
{ |
53
|
|
|
$this->roles()->syncWithoutDetaching([$role->id]); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if a user has the provided role. |
60
|
|
|
* Accepected values are string with the name of the role, |
61
|
|
|
* a collection of Role models or an array of role names with a "name" key |
62
|
|
|
* |
63
|
|
|
* @param $role |
64
|
|
|
* |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
|
|
public function hasRole($role) |
68
|
|
|
{ |
69
|
|
|
if (is_string($role)) { |
70
|
|
|
return $this->roles->contains('name', $role); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($role instanceof Collection) { |
74
|
|
|
$role = $role->toArray(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_array($role)) { |
78
|
|
|
foreach($role as $r) { |
79
|
|
|
if ($this->roles->contains('name', $r['name'])) { |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->roles()->where('multi_tenant_role_id', $role->id)->exists(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return all of the tenants this user is the owner of |
90
|
|
|
*/ |
91
|
|
|
public function owns() |
92
|
|
|
{ |
93
|
|
|
return $this->hasMany(config('multi-tenant.tenant_class'), 'owner_id'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the currently active tenant for this user based on the session |
98
|
|
|
* |
99
|
|
|
* @return BaseTenant |
100
|
|
|
*/ |
101
|
|
|
public function activeTenant() |
102
|
|
|
{ |
103
|
|
|
return config('multi-tenant.tenant_class')::findOrFail(session()->get('tenant.id')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The tenants relationship |
108
|
|
|
*/ |
109
|
|
|
public function tenants() |
110
|
|
|
{ |
111
|
|
|
return $this->belongsToMany(config('multi-tenant.tenant_class')); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|