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