1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Models; |
3
|
|
|
|
4
|
|
|
use Eloquence\Behaviours\Sluggable; |
5
|
|
|
use Illuminate\Auth\Authenticatable; |
6
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
7
|
|
|
use Illuminate\Auth\MustVerifyEmail; |
8
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
9
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
10
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
11
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract; |
12
|
|
|
use Illuminate\Database\Eloquent\Builder; |
13
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
14
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
15
|
|
|
use Illuminate\Notifications\DatabaseNotification; |
16
|
|
|
use Illuminate\Notifications\Notifiable; |
17
|
|
|
use Spatie\MediaLibrary\HasMedia; |
18
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
19
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media; |
20
|
|
|
use Ultraware\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract; |
21
|
|
|
use Ultraware\Roles\Traits\HasRoleAndPermission; |
22
|
|
|
use Xetaravel\Models\Presenters\UserPresenter; |
23
|
|
|
use Xetaravel\Notifications\Auth\VerifyEmail; |
24
|
|
|
use Xetaravel\Notifications\Auth\ResetPassword; |
25
|
|
|
|
26
|
|
|
class User extends Model implements |
27
|
|
|
AuthenticatableContract, |
28
|
|
|
AuthorizableContract, |
29
|
|
|
CanResetPasswordContract, |
30
|
|
|
HasRoleAndPermissionContract, |
31
|
|
|
HasMedia, |
32
|
|
|
MustVerifyEmailContract |
33
|
|
|
{ |
34
|
|
|
use Authenticatable, |
|
|
|
|
35
|
|
|
Authorizable, |
36
|
|
|
CanResetPassword, |
37
|
|
|
Notifiable, |
38
|
|
|
Sluggable, |
39
|
|
|
HasRoleAndPermission, |
40
|
|
|
InteractsWithMedia, |
41
|
|
|
UserPresenter, |
42
|
|
|
MustVerifyEmail, |
43
|
|
|
SoftDeletes; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The attributes that are mass assignable. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $fillable = [ |
51
|
|
|
'username', |
52
|
|
|
'email', |
53
|
|
|
'password', |
54
|
|
|
'slug', |
55
|
|
|
'github_id', |
56
|
|
|
'register_ip', |
57
|
|
|
'last_login_ip', |
58
|
|
|
'last_login', |
59
|
|
|
'email_verified_at' |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The attributes that should be hidden for arrays. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $hidden = [ |
68
|
|
|
'password', |
69
|
|
|
'remember_token' |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The accessors to append to the model's array form. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $appends = [ |
78
|
|
|
'profile_background', |
79
|
|
|
'profile_url', |
80
|
|
|
|
81
|
|
|
// Media Model |
82
|
|
|
'avatar_small', |
83
|
|
|
'avatar_medium', |
84
|
|
|
'avatar_big', |
85
|
|
|
'avatar_primary_color', |
86
|
|
|
|
87
|
|
|
// Account Model |
88
|
|
|
'first_name', |
89
|
|
|
'last_name', |
90
|
|
|
'full_name', |
91
|
|
|
'biography', |
92
|
|
|
'signature', |
93
|
|
|
'facebook', |
94
|
|
|
'twitter', |
95
|
|
|
|
96
|
|
|
// Session Model |
97
|
|
|
'online' |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The attributes that should be mutated to dates. |
102
|
|
|
* |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
protected $dates = [ |
106
|
|
|
'last_login' |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The "booting" method of the model. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected static function boot() |
115
|
|
|
{ |
116
|
|
|
parent::boot(); |
117
|
|
|
|
118
|
|
|
// Generated the slug before updating. |
119
|
|
|
static::updating(function ($model) { |
120
|
|
|
$model->generateSlug(); |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return the field to slug. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function slugStrategy(): string |
130
|
|
|
{ |
131
|
|
|
return 'username'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Register the related to the Model. |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function registerMediaConversions(Media $media = null): void |
140
|
|
|
{ |
141
|
|
|
$this->addMediaConversion('thumbnail.small') |
142
|
|
|
->width(100) |
143
|
|
|
->height(100) |
144
|
|
|
->keepOriginalImageFormat(); |
145
|
|
|
|
146
|
|
|
$this->addMediaConversion('thumbnail.medium') |
147
|
|
|
->width(200) |
148
|
|
|
->height(200) |
149
|
|
|
->keepOriginalImageFormat(); |
150
|
|
|
|
151
|
|
|
$this->addMediaConversion('thumbnail.big') |
152
|
|
|
->width(300) |
153
|
|
|
->height(300) |
154
|
|
|
->keepOriginalImageFormat(); |
155
|
|
|
|
156
|
|
|
$this->addMediaConversion('original') |
157
|
|
|
->keepOriginalImageFormat(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the comments for the user. |
162
|
|
|
* |
163
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
164
|
|
|
*/ |
165
|
|
|
public function comments() |
166
|
|
|
{ |
167
|
|
|
return $this->hasMany(Comment::class); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the articles for the user. |
172
|
|
|
* |
173
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
174
|
|
|
*/ |
175
|
|
|
public function articles() |
176
|
|
|
{ |
177
|
|
|
return $this->hasMany(Article::class); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the account for the user. |
182
|
|
|
* |
183
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
184
|
|
|
*/ |
185
|
|
|
public function account() |
186
|
|
|
{ |
187
|
|
|
return $this->hasOne(Account::class); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the roles for the user. |
192
|
|
|
* |
193
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
194
|
|
|
*/ |
195
|
|
|
/*public function roles() |
196
|
|
|
{ |
197
|
|
|
return $this->belongsToMany(Role::class)->withTimestamps(); |
198
|
|
|
}*/ |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the badges for the user. |
202
|
|
|
* |
203
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
204
|
|
|
*/ |
205
|
|
|
public function badges() |
206
|
|
|
{ |
207
|
|
|
return $this->belongsToMany(Badge::class)->withTimestamps(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the notifications for the user. |
212
|
|
|
* |
213
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
214
|
|
|
*/ |
215
|
|
|
public function notifications() |
216
|
|
|
{ |
217
|
|
|
return $this->morphMany(DatabaseNotification::class, 'notifiable') |
218
|
|
|
->orderBy('read_at', 'asc') |
219
|
|
|
->orderBy('created_at', 'desc'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the discuss posts for the user. |
224
|
|
|
* |
225
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
226
|
|
|
*/ |
227
|
|
|
public function discussPosts() |
228
|
|
|
{ |
229
|
|
|
return $this->hasMany(DiscussPost::class); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get the discuss conversations for the user. |
234
|
|
|
* |
235
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
236
|
|
|
*/ |
237
|
|
|
public function discussConversations() |
238
|
|
|
{ |
239
|
|
|
return $this->hasMany(DiscussConversation::class); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get the discuss users for the user. |
244
|
|
|
* |
245
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
246
|
|
|
*/ |
247
|
|
|
public function discussUsers() |
248
|
|
|
{ |
249
|
|
|
return $this->hasMany(DiscussUser::class); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get the discuss logs for the user. |
254
|
|
|
* |
255
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
256
|
|
|
*/ |
257
|
|
|
public function discussLogs() |
258
|
|
|
{ |
259
|
|
|
return $this->hasMany(DiscussLog::class); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get the rubies for the user. |
264
|
|
|
* |
265
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
266
|
|
|
*/ |
267
|
|
|
public function rubies() |
268
|
|
|
{ |
269
|
|
|
return $this->hasMany(Ruby::class); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get the experiences for the user. |
274
|
|
|
* |
275
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
276
|
|
|
*/ |
277
|
|
|
public function experiences() |
278
|
|
|
{ |
279
|
|
|
return $this->hasMany(Experience::class); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get the shop_items for the user. |
284
|
|
|
* |
285
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
286
|
|
|
*/ |
287
|
|
|
public function shopItems() |
288
|
|
|
{ |
289
|
|
|
return $this->belongsToMany(ShopItem::class)->withTimestamps(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Send the password reset notification. |
294
|
|
|
* |
295
|
|
|
* @param string $token |
296
|
|
|
* |
297
|
|
|
* @return void |
298
|
|
|
*/ |
299
|
|
|
public function sendPasswordResetNotification($token) |
300
|
|
|
{ |
301
|
|
|
$this->notify(new ResetPassword($token)); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Send the email verification notification. |
306
|
|
|
* |
307
|
|
|
* @return void |
308
|
|
|
*/ |
309
|
|
|
public function sendEmailVerificationNotification() |
310
|
|
|
{ |
311
|
|
|
$this->notify(new VerifyEmail); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Get all permissions from roles. |
316
|
|
|
* |
317
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
318
|
|
|
*/ |
319
|
|
|
public function rolePermissions(): Builder |
320
|
|
|
{ |
321
|
|
|
$permissionModel = app(config('roles.models.permission')); |
322
|
|
|
|
323
|
|
|
return $permissionModel |
324
|
|
|
::select([ |
325
|
|
|
'permissions.*', |
326
|
|
|
'permission_role.created_at as pivot_created_at', |
327
|
|
|
'permission_role.updated_at as pivot_updated_at' |
328
|
|
|
]) |
329
|
|
|
->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id') |
330
|
|
|
->join('roles', 'roles.id', '=', 'permission_role.role_id') |
331
|
|
|
->whereIn('roles.id', $this->getRoles()->pluck('id')->toArray()) |
332
|
|
|
->orWhere('roles.level', '<', $this->level()) |
333
|
|
|
->groupBy([ |
334
|
|
|
'permissions.id', |
335
|
|
|
'permissions.name', |
336
|
|
|
'permissions.slug', |
337
|
|
|
'permissions.description', |
338
|
|
|
'permissions.model', |
339
|
|
|
'permissions.created_at', |
340
|
|
|
'permissions.updated_at', |
341
|
|
|
'permissions.is_deletable', |
342
|
|
|
'pivot_created_at', |
343
|
|
|
'pivot_updated_at' |
344
|
|
|
]); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|