|
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
|
2 |
|
public function manager() { |
|
|
|
|
|
|
82
|
2 |
|
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
|
6 |
|
public function user_role() { |
|
|
|
|
|
|
90
|
6 |
|
return $this->belongsTo(\App\Models\UserRole::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
//Role related functions |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Abort with an HTTP error if user doesn't have correct roles |
|
|
|
|
|
|
97
|
|
|
* @param string|array $roles |
|
|
|
|
|
|
98
|
|
|
*/ |
|
|
|
|
|
|
99
|
|
|
public function authorizeRoles($roles) |
|
100
|
|
|
{ |
|
101
|
|
|
if (is_array($roles)) { |
|
102
|
|
|
return $this->hasAnyRole($roles) || |
|
103
|
|
|
abort(401, 'This action is unauthorized.'); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
return $this->hasRole($roles) || |
|
106
|
|
|
abort(401, 'This action is unauthorized.'); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Check multiple roles |
|
|
|
|
|
|
111
|
|
|
* @param array $roles |
|
|
|
|
|
|
112
|
|
|
*/ |
|
|
|
|
|
|
113
|
|
|
public function hasAnyRole($roles) |
|
114
|
|
|
{ |
|
115
|
|
|
return in_array($this->user_role->name, $roles); |
|
116
|
|
|
//return null !== $this->roles()->whereIn(‘name’, $roles)->first(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Check one role |
|
|
|
|
|
|
121
|
|
|
* @param string $role |
|
|
|
|
|
|
122
|
|
|
*/ |
|
|
|
|
|
|
123
|
2 |
|
public function hasRole($role) |
|
124
|
|
|
{ |
|
125
|
2 |
|
return $this->user_role->name == $role; |
|
126
|
|
|
//return null !== $this->roles()->where(‘name’, $role)->first(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* OVERRIDE |
|
131
|
|
|
* Send the password reset notification. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $token |
|
|
|
|
|
|
134
|
|
|
* @return void |
|
|
|
|
|
|
135
|
|
|
*/ |
|
136
|
|
|
public function sendPasswordResetNotification($token) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|