1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oscer\Cms\Core\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable; |
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
7
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable as AuthorizableTrait; |
8
|
|
|
use Illuminate\Support\Carbon; |
9
|
|
|
use Illuminate\Support\Facades\Mail; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Laravel\Sanctum\HasApiTokens; |
13
|
|
|
use Oscer\Cms\Core\Mails\NewUserCreatedMail; |
14
|
|
|
use Spatie\Permission\Traits\HasRoles; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property int id |
18
|
|
|
* @property string name |
19
|
|
|
* @property string email |
20
|
|
|
* @property string bio |
21
|
|
|
* @property string avatar |
22
|
|
|
* @property Carbon updated_at |
23
|
|
|
* @property Carbon created_at |
24
|
|
|
*/ |
25
|
|
|
class User extends BaseModel implements Authenticatable, Authorizable |
26
|
|
|
{ |
27
|
|
|
use HasApiTokens; |
28
|
|
|
use HasRoles; |
29
|
|
|
use AuthorizableTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The attributes excluded from the model's JSON form. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $hidden = ['password', 'remember_token']; |
37
|
|
|
|
38
|
|
|
protected $attributes = [ |
39
|
|
|
'language' => 'en', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
protected $appends = ['assigned_permissions']; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The column name of the "remember me" token. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $rememberTokenName = 'remember_token'; |
50
|
|
|
|
51
|
|
|
protected static function boot() |
52
|
|
|
{ |
53
|
|
|
parent::boot(); |
54
|
|
|
|
55
|
|
|
static::creating(function (self $user) { |
56
|
|
|
if (! $user->password) { |
57
|
|
|
$password = Str::random(); |
58
|
|
|
$user->password = $password; |
59
|
|
|
|
60
|
|
|
Mail::to($user->email)->send(new NewUserCreatedMail($password)); |
61
|
|
|
} |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the name of the unique identifier for the user. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getAuthIdentifierName() |
71
|
|
|
{ |
72
|
|
|
return $this->getKeyName(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the unique identifier for the user. |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function getAuthIdentifier() |
81
|
|
|
{ |
82
|
|
|
return $this->{$this->getAuthIdentifierName()}; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the password for the user. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getAuthPassword() |
91
|
|
|
{ |
92
|
|
|
return $this->password; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the token value for the "remember me" session. |
97
|
|
|
* |
98
|
|
|
* @return string|null |
99
|
|
|
*/ |
100
|
|
|
public function getRememberToken() |
101
|
|
|
{ |
102
|
|
|
if (! empty($this->getRememberTokenName())) { |
103
|
|
|
return (string) $this->{$this->getRememberTokenName()}; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the token value for the "remember me" session. |
109
|
|
|
* |
110
|
|
|
* @param string $value |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function setRememberToken($value) |
114
|
|
|
{ |
115
|
|
|
if (! empty($this->getRememberTokenName())) { |
116
|
|
|
$this->{$this->getRememberTokenName()} = $value; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the column name for the "remember me" token. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getRememberTokenName() |
126
|
|
|
{ |
127
|
|
|
return $this->rememberTokenName; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the author's avatar. |
132
|
|
|
* |
133
|
|
|
* @param string $value |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getAvatarAttribute($value) |
137
|
|
|
{ |
138
|
|
|
return $value |
139
|
|
|
? Storage::url($value) |
140
|
|
|
: 'https://secure.gravatar.com/avatar/'.md5(strtolower(trim($this->email))).'?s=80'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setPasswordAttribute($value) |
144
|
|
|
{ |
145
|
|
|
$this->attributes['password'] = bcrypt($value); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get all permission names that are assigned to the current user. |
150
|
|
|
*/ |
151
|
|
|
public function getAssignedPermissionsAttribute() |
152
|
|
|
{ |
153
|
|
|
return $this->getAllPermissions() |
154
|
|
|
->reduce(function ($result, $permission) { |
155
|
|
|
$result[] = $permission->name; |
156
|
|
|
|
157
|
|
|
return $result; |
158
|
|
|
}, []); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|