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