1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Models; |
6
|
|
|
|
7
|
|
|
use Rinvex\Tenants\Traits\Tenantable; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
10
|
|
|
use Cortex\Auth\Notifications\PhoneVerificationNotification; |
11
|
|
|
use Cortex\Auth\Notifications\ManagerPasswordResetNotification; |
12
|
|
|
use Cortex\Auth\Notifications\ManagerEmailVerificationNotification; |
13
|
|
|
|
14
|
|
|
class Manager extends User |
15
|
|
|
{ |
16
|
|
|
use Tenantable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected $fillable = [ |
22
|
|
|
'username', |
23
|
|
|
'password', |
24
|
|
|
'two_factor', |
25
|
|
|
'email', |
26
|
|
|
'email_verified', |
27
|
|
|
'email_verified_at', |
28
|
|
|
'phone', |
29
|
|
|
'phone_verified', |
30
|
|
|
'phone_verified_at', |
31
|
|
|
'name_prefix', |
32
|
|
|
'first_name', |
33
|
|
|
'middle_name', |
34
|
|
|
'last_name', |
35
|
|
|
'name_suffix', |
36
|
|
|
'title', |
37
|
|
|
'country_code', |
38
|
|
|
'language_code', |
39
|
|
|
'birthday', |
40
|
|
|
'gender', |
41
|
|
|
'is_active', |
42
|
|
|
'last_activity', |
43
|
|
|
'abilities', |
44
|
|
|
'roles', |
45
|
|
|
'tenants', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected $passwordResetNotificationClass = ManagerPasswordResetNotification::class; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected $emailVerificationNotificationClass = ManagerEmailVerificationNotification::class; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
protected $phoneVerificationNotificationClass = PhoneVerificationNotification::class; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create a new Eloquent model instance. |
65
|
|
|
* |
66
|
|
|
* @param array $attributes |
67
|
|
|
*/ |
68
|
|
|
public function __construct(array $attributes = []) |
69
|
|
|
{ |
70
|
|
|
parent::__construct($attributes); |
71
|
|
|
|
72
|
|
|
$this->setTable(config('cortex.auth.tables.managers')); |
73
|
|
|
$this->setRules([ |
74
|
|
|
'username' => 'required|alpha_dash|min:3|max:150|unique:'.config('cortex.auth.tables.managers').',username', |
75
|
|
|
'password' => 'sometimes|required|min:'.config('cortex.auth.password_min_chars'), |
76
|
|
|
'two_factor' => 'nullable|array', |
77
|
|
|
'email' => 'required|email|min:3|max:150|unique:'.config('cortex.auth.tables.managers').',email', |
78
|
|
|
'email_verified' => 'sometimes|boolean', |
79
|
|
|
'email_verified_at' => 'nullable|date', |
80
|
|
|
'phone' => 'nullable|numeric|min:4', |
81
|
|
|
'phone_verified' => 'sometimes|boolean', |
82
|
|
|
'phone_verified_at' => 'nullable|date', |
83
|
|
|
'name_prefix' => 'nullable|string|max:150', |
84
|
|
|
'first_name' => 'nullable|string|max:150', |
85
|
|
|
'middle_name' => 'nullable|string|max:150', |
86
|
|
|
'last_name' => 'nullable|string|max:150', |
87
|
|
|
'name_suffix' => 'nullable|string|max:150', |
88
|
|
|
'title' => 'nullable|string|max:150', |
89
|
|
|
'country_code' => 'nullable|alpha|size:2|country', |
90
|
|
|
'language_code' => 'nullable|alpha|size:2|language', |
91
|
|
|
'birthday' => 'nullable|date_format:Y-m-d', |
92
|
|
|
'gender' => 'nullable|string|in:male,female', |
93
|
|
|
'is_active' => 'sometimes|boolean', |
94
|
|
|
'last_activity' => 'nullable|date', |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Attach the given tenants to the model. |
100
|
|
|
* |
101
|
|
|
* @param mixed $tenants |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function setTenantsAttribute($tenants): void |
106
|
|
|
{ |
107
|
|
|
static::saved(function (self $model) use ($tenants) { |
108
|
|
|
$tenants === $model->tenants->pluck('id')->toArray() |
109
|
|
|
|| activity() |
110
|
|
|
->performedOn($model) |
111
|
|
|
->withProperties(['attributes' => ['tenants' => $tenants], 'old' => ['tenants' => $model->tenants->pluck('id')->toArray()]]) |
|
|
|
|
112
|
|
|
->log('updated'); |
113
|
|
|
|
114
|
|
|
$model->syncTenants($tenants); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get all attached tenants to the manager. |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
122
|
|
|
*/ |
123
|
|
|
public function tenants(): MorphToMany |
124
|
|
|
{ |
125
|
|
|
return $this->morphToMany(config('rinvex.tenants.models.tenant'), 'tenantable', config('rinvex.tenants.tables.tenantables'), 'tenantable_id', 'tenant_id') |
|
|
|
|
126
|
|
|
->withTimestamps(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Determine if manager is owner of the given tenant. |
131
|
|
|
* |
132
|
|
|
* @param \Illuminate\Database\Eloquent\Model $tenant |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
|
|
public function isOwner(Model $tenant): bool |
137
|
|
|
{ |
138
|
|
|
return $this->getKey() === $tenant->owner->getKey(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Determine if manager is staff of the given tenant. |
143
|
|
|
* |
144
|
|
|
* @param \Illuminate\Database\Eloquent\Model $tenant |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function isStaff(Model $tenant): bool |
149
|
|
|
{ |
150
|
|
|
return $this->tenants->contains($tenant); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.