-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
287 lines (260 loc) · 7.8 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Nette\Schema\ValidationException;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
use HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'company_id',
'institution'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
/**
* The accessors to append to the model's array form.
*
* @var array<int, string>
*/
protected $appends = [
'profile_photo_url',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
/**
* Establishes a relationship between the user and the company they're part of
* (if they have a company)
* @return BelongsTo
*/
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
/**
* Hides a many-to-many relationship with presentations
* and implements relationship with linking table UserPresentation
* Please don't use this, instead refer to the methods below
*
* @return HasMany
*/
public function userPresentations(): HasMany
{
return $this->hasMany(UserPresentation::class);
}
/**
* Assign the user to a presentation, based on the role that
* was passed - participant or speaker
* Returns true if the user successfully was added to the presentation with their role;
* Returns false if the user wasn't attached to the presentation
*
* @param $presentation
* @param string $role
* @return bool
*/
public function joinPresentation($presentation, string $role = 'participant'): bool
{
if ($this->presenter_of) {
// The user is already a speaker of another presentation
if ($role == 'speaker') {
return false;
}
// The user is a speaker of this presentation, and cannot be a participant
if ($this->presenter_of->id == $presentation->id && $role == 'participant') {
return false;
}
}
// The user is already enrolled as a participant in this presentation
if ($this->participating_in->contains($presentation)) {
return false;
}
UserPresentation::create([
'user_id' => $this->id,
'presentation_id' => $presentation->id,
'role' => $role
]);
return true;
}
/**
* Disneroll participant from a presentation
*
* @param $presentation
* @return void
*/
public function leavePresentation($presentation)
{
$userPresentation = $this->userPresentations
->where('role', 'participant')
->where('presentation_id', $presentation->id)
->first();
if (!is_null($userPresentation)) {
$userPresentation->delete();
}
}
/**
* Returns the presentation of which the user is a speaker
*
* @return Attribute
*/
public function presenterOf(): Attribute
{
return Attribute::make(
get: fn() => Presentation::whereHas('userPresentations', function ($query) {
$query->where('user_id', $this->id)
->where('role', 'speaker');
})->first(),
);
}
/**
* Returns the presentations in which the user enrolled to
* be a participant
*
* @return Attribute
*/
public function participatingIn(): Attribute
{
return Attribute::make(
get: fn() => Presentation::whereHas('userPresentations', function ($query) {
$query->where('user_id', $this->id)
->where('role', 'participant');
})->get(),
);
}
/**
* Determines whether the user is a member of the specified company.
*
* @param Company $company
* @return bool
*/
public function isMemberOf(Company $company): bool
{
return $company && $this->company && $this->company->id == $company->id;
}
/**
* Determines whether the user is a presenter of the specified presentation.
*
* @param Presentation $presentation
* @return bool
*/
public function isPresenterOf(Presentation $presentation)
{
return $this->presenter_of
&& $this->presenter_of->id == $presentation->id;
}
/**
* Definition of the `is_crew` read-only attribute that is `true`
* if the user has one or more roles that resembles a Crew member,
* like organizers and supervisors.
*
* @return Attribute
*/
public function isCrew(): Attribute
{
return Attribute::make(
get: fn() => $this->hasRole(['event organizer', 'assistant organizer',
'company market supervisor', 'speakers supervisor', 'pr lead',
'entertainment organizer'])
);
}
/**
* Checks if the user can be enrolled to this presentation, based on the
* event status and the max_participants
* TODO: add user schedule clashing
*
* @param Presentation $presentation
* @return bool
*/
public function canEnroll(Presentation $presentation): bool
{
if (!EventInstance::current()->is_final_programme_released) {
return false;
}
return $presentation->remaining_capacity > 0;
}
/**
* Determines the color scheme of the hub area based on the user's role
*
* @return Attribute
*/
public function roleColour(): Attribute
{
return Attribute::make(
get: function () {
if ($this->company) {
return 'partner';
} elseif ($this->hasRole('crew')) {
return 'crew';
} else {
return 'participant';
}
}
);
}
/**
* Scope a query to only include users who can be company representatives.
*/
public function scopeForCompanyRep(Builder $query): void
{
// Only user who:
// Do not have an @hz.nl
$query->role(['participant'])
->where('email', 'not like', '%@hz.nl') // Exclude emails ending with '@hz.nl'
->orderBy('name');
}
/**
* Determines whether the user is simply a company member.
* This means they are not speaker, representative or booth owner.
*
* @return Attribute
*/
public function isDefaultCompanyMember() : Attribute
{
return Attribute::make(
get: fn() => $this->hasExactRoles(['participant', 'company member'])
&& $this->company
&& is_null($this->presenter_of)
);
}
}