|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App; |
|
5
|
|
|
|
|
6
|
|
|
use App\Models\Traits\EloquentGetTableName; |
|
7
|
|
|
use App\Notifications\ResetPassword as ResetPasswordNotification; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
|
10
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
11
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
|
12
|
|
|
use Illuminate\Notifications\Notifiable; |
|
13
|
|
|
use Yadahan\AuthenticationLog\AuthenticationLogable; |
|
14
|
|
|
|
|
15
|
|
|
class User extends Authenticatable |
|
16
|
|
|
{ |
|
17
|
|
|
use Notifiable; |
|
|
|
|
|
|
18
|
|
|
use SoftDeletes; |
|
19
|
|
|
use EloquentGetTableName; |
|
20
|
|
|
use AuthenticationLogable; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
const ROLE_ADMINISTRATOR = "administrator"; |
|
23
|
|
|
const ROLE_VIEWER = "viewer"; |
|
24
|
|
|
const ROLE_STUDENT = "student"; |
|
25
|
|
|
|
|
26
|
|
|
const EXAMPLE_DOMAIN = "example.com"; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The attributes that should be mutated to dates. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $dates = ["deleted_at"]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The attributes that are mass assignable. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fillable = [ |
|
41
|
|
|
"first_name", |
|
42
|
|
|
"last_name", |
|
43
|
|
|
"email", |
|
44
|
|
|
"password", |
|
45
|
|
|
"role", |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The attributes that should be hidden for arrays. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $hidden = [ |
|
54
|
|
|
"password", |
|
55
|
|
|
"remember_token", |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the user's first name. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $value |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setFirstNameAttribute($value) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->attributes["first_name"] = ucwords(strtolower($value), "'- "); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the user's last name. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $value |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setLastNameAttribute($value) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->attributes["last_name"] = ucwords(strtolower($value), "'- "); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the student of this user |
|
82
|
|
|
* @return HasOne |
|
83
|
|
|
*/ |
|
84
|
|
|
public function student() : HasOne |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->hasOne("App\Models\Student"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Scope a query to only include student users. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
93
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
94
|
|
|
*/ |
|
95
|
|
|
public function scopeStudents(Builder $query) : Builder |
|
96
|
|
|
{ |
|
97
|
|
|
return $query->where("role", self::ROLE_STUDENT); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Scope a query to only include viewer users. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
104
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
105
|
|
|
*/ |
|
106
|
|
|
public function scopeViewers(Builder $query) : Builder |
|
107
|
|
|
{ |
|
108
|
|
|
return $query->where("role", self::ROLE_VIEWER); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Scope a query to only include administrator users. |
|
113
|
|
|
* |
|
114
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
115
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
116
|
|
|
*/ |
|
117
|
|
|
public function scopeAdministrators(Builder $query) : Builder |
|
118
|
|
|
{ |
|
119
|
|
|
return $query->where("role", self::ROLE_ADMINISTRATOR); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Send the password reset notification. |
|
124
|
|
|
* This method is overridden in order to customize/localize the message |
|
125
|
|
|
* and prevent e-mail delivery related to example users. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $token |
|
128
|
|
|
* |
|
129
|
|
|
* @see https://laravel.com/docs/5.5/passwords#password-customization |
|
130
|
|
|
*/ |
|
131
|
|
|
public function sendPasswordResetNotification($token) |
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->isExampleUser() === true) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function isExampleUser() : bool |
|
140
|
|
|
{ |
|
141
|
|
|
return substr($this->email, -strlen("@" . self::EXAMPLE_DOMAIN)) === "@" . self::EXAMPLE_DOMAIN; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|