1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by Reliese Model. |
5
|
|
|
* Date: Thu, 12 Jul 2018 22:39:28 +0000. |
6
|
|
|
*/ |
|
|
|
|
7
|
|
|
|
8
|
|
|
namespace App\Models; |
9
|
|
|
|
10
|
|
|
use Illuminate\Auth\Authenticatable; |
11
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
12
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
13
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
14
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
15
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
16
|
|
|
use Illuminate\Notifications\Notifiable; |
17
|
|
|
use App\Events\UserCreated; |
18
|
|
|
use App\Events\UserUpdated; |
19
|
|
|
use App\Notifications\ResetPasswordNotification; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class User |
23
|
|
|
* |
24
|
|
|
* @property int $id |
25
|
|
|
* @property string $email |
26
|
|
|
* @property string $name |
27
|
|
|
* @property string $password |
28
|
|
|
* @property bool $is_confirmed |
29
|
|
|
* @property int $user_role_id |
30
|
|
|
* @property \Jenssegers\Date\Date $created_at |
31
|
|
|
* @property \Jenssegers\Date\Date $updated_at |
32
|
|
|
* |
33
|
|
|
* @property \App\Models\Applicant $applicant |
34
|
|
|
* @property \App\Models\Manager $manager |
35
|
|
|
* @property \App\Models\ProfilePic $profile_pic |
36
|
|
|
* @property \App\Models\UserRole $user_role |
37
|
|
|
*/ |
|
|
|
|
38
|
|
|
class User extends BaseModel implements |
39
|
|
|
// Laravel contracts for native login |
40
|
|
|
AuthenticatableContract, |
41
|
|
|
CanResetPasswordContract, |
42
|
|
|
// Contract for use with Gates and Policies |
43
|
|
|
AuthorizableContract |
44
|
|
|
// Custom contract for use with openid login |
45
|
|
|
// \App\Services\Auth\Contracts\OidcAuthenticatable |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
//Traits for Laravel basic authentication |
49
|
|
|
use Authenticatable, CanResetPassword; |
50
|
|
|
// Trait for working with Gates and Policies |
51
|
|
|
use Authorizable; |
52
|
|
|
// Trait for notifications |
53
|
|
|
use Notifiable; |
|
|
|
|
54
|
|
|
|
55
|
|
|
protected $casts = [ |
56
|
|
|
'is_confirmed' => 'bool', |
57
|
|
|
'user_role_id' => 'int' |
58
|
|
|
]; |
59
|
|
|
protected $fillable = [ |
60
|
|
|
'name', 'email', 'password', |
61
|
|
|
]; |
62
|
|
|
protected $with = ['user_role']; |
63
|
|
|
protected $hidden = [ |
64
|
|
|
'password', 'remember_token', |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The event map for the model. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $dispatchesEvents = [ |
73
|
|
|
'created' => UserCreated::class, |
74
|
|
|
'updated' => UserUpdated::class, |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
public function applicant() { |
|
|
|
|
78
|
|
|
return $this->hasOne(\App\Models\Applicant::class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function manager() { |
|
|
|
|
82
|
|
|
return $this->hasOne(\App\Models\Manager::class); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function profile_pic() { |
|
|
|
|
86
|
|
|
return $this->hasOne(\App\Models\ProfilePic::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function user_role() { |
|
|
|
|
90
|
|
|
return $this->belongsTo(\App\Models\UserRole::class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Mutators |
94
|
|
|
|
95
|
|
|
// public function setEmailAttribute($value) |
96
|
|
|
// { |
97
|
|
|
// $this->attributes['email'] = strtolower($value); |
98
|
|
|
// } |
99
|
|
|
|
100
|
|
|
// Accessors |
101
|
|
|
|
102
|
|
|
public function getEmailAttribute($value) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return strtolower($value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//Role related functions |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Abort with an HTTP error if user doesn't have correct roles |
|
|
|
|
111
|
|
|
* @param string|array $roles |
|
|
|
|
112
|
|
|
*/ |
|
|
|
|
113
|
|
|
public function authorizeRoles($roles) |
114
|
|
|
{ |
115
|
|
|
if (is_array($roles)) { |
116
|
|
|
return $this->hasAnyRole($roles) || |
117
|
|
|
abort(401, 'This action is unauthorized.'); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
return $this->hasRole($roles) || |
120
|
|
|
abort(401, 'This action is unauthorized.'); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check multiple roles |
|
|
|
|
125
|
|
|
* @param array $roles |
|
|
|
|
126
|
|
|
*/ |
|
|
|
|
127
|
|
|
public function hasAnyRole($roles) |
128
|
|
|
{ |
129
|
|
|
return in_array($this->user_role->name, $roles); |
130
|
|
|
//return null !== $this->roles()->whereIn(‘name’, $roles)->first(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check one role |
|
|
|
|
135
|
|
|
* @param string $role |
|
|
|
|
136
|
|
|
*/ |
|
|
|
|
137
|
|
|
public function hasRole($role) |
138
|
|
|
{ |
139
|
|
|
return $this->user_role->name == $role; |
140
|
|
|
//return null !== $this->roles()->where(‘name’, $role)->first(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* OVERRIDE |
145
|
|
|
* Send the password reset notification. |
146
|
|
|
* |
147
|
|
|
* @param string $token |
|
|
|
|
148
|
|
|
* @return void |
|
|
|
|
149
|
|
|
*/ |
150
|
|
|
public function sendPasswordResetNotification($token) |
151
|
|
|
{ |
152
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|