1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
8
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
9
|
|
|
use Spatie\MediaLibrary\HasMedia; |
10
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
11
|
|
|
|
12
|
|
|
class User extends Authenticatable implements MustVerifyEmail, HasMedia |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
use InteractsWithMedia; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User constructor. |
18
|
|
|
* |
19
|
|
|
* @param array $attributes |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $attributes = []) |
22
|
|
|
{ |
23
|
|
|
$this->fillable = array_merge([ |
24
|
|
|
'role_id', 'name', 'email', 'password' |
25
|
|
|
], $this->fillable); |
26
|
|
|
|
27
|
|
|
parent::__construct($attributes); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return BelongsTo |
32
|
|
|
*/ |
33
|
|
|
public function role() |
34
|
|
|
{ |
35
|
|
|
return $this->belongsTo(Role::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Retrieve role's permissions |
40
|
|
|
* |
41
|
|
|
* @return Collection |
42
|
|
|
*/ |
43
|
|
|
public function permissions() |
44
|
|
|
{ |
45
|
|
|
return $this->role->permissions; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Register media library collections. |
50
|
|
|
*/ |
51
|
|
|
public function registerMediaCollections(): void |
52
|
|
|
{ |
53
|
|
|
$this->addMediaCollection('avatar') |
54
|
|
|
->singleFile() |
55
|
|
|
->useFallbackPath($avatar = asset('/storage/user-placeholder.png')) |
56
|
|
|
->useFallbackUrl($avatar); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Hash the password whenever updating. |
61
|
|
|
* |
62
|
|
|
* @param $value |
63
|
|
|
*/ |
64
|
|
|
public function setPasswordAttribute($value) |
65
|
|
|
{ |
66
|
|
|
$this->attributes['password'] = bcrypt($value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get user's avatar or get the placeholder |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getAvatarAttribute() |
75
|
|
|
{ |
76
|
|
|
return $this->getFirstMediaUrl('avatar'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|