1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Model; |
13
|
|
|
|
14
|
|
|
use Auth as Auth; |
15
|
|
|
use Illuminate\Auth\Authenticatable; |
16
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
17
|
|
|
use Illuminate\Database\Eloquent; |
18
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
19
|
|
|
use Tinyissue\Model\ModelAbstract; |
20
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
21
|
|
|
use Tinyissue\Extensions\Auth\LoggedUser; |
22
|
|
|
use Illuminate\Support\Facades\Gate; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* User is model class for users. |
26
|
|
|
* |
27
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
28
|
|
|
* |
29
|
|
|
* @property int $id |
30
|
|
|
* @property int $deleted |
31
|
|
|
* @property int $role_id |
32
|
|
|
* @property string $language |
33
|
|
|
* @property string $email |
34
|
|
|
* @property string $password |
35
|
|
|
* @property string $firstname |
36
|
|
|
* @property string $lastname |
37
|
|
|
* @property string $fullname |
38
|
|
|
* @property int $status |
39
|
|
|
* @property int $private |
40
|
|
|
* @property Role $role |
41
|
|
|
* @property Eloquent\Collection $comments |
42
|
|
|
* @property Eloquent\Collection $issuesCreatedBy |
43
|
|
|
* @property Eloquent\Collection $issuesClosedBy |
44
|
|
|
* @property Eloquent\Collection $issuesUpdatedBy |
45
|
|
|
* @property Eloquent\Collection $attachments |
46
|
|
|
* @property Eloquent\Collection $projects |
47
|
|
|
* @property Eloquent\Collection $issues |
48
|
|
|
* @property Eloquent\Collection $permissions |
49
|
|
|
* |
50
|
|
|
* @method User updateOrCreate(array $where, array $input) |
51
|
|
|
*/ |
52
|
|
|
class User extends ModelAbstract implements UserInterface |
53
|
|
|
{ |
54
|
|
|
use Authenticatable, |
55
|
|
|
CanResetPassword, |
56
|
|
|
UserRelations, |
57
|
|
|
UserScopes, |
58
|
|
|
LoggedUser, |
59
|
|
|
Authorizable; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* User name is private. |
63
|
|
|
* |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
const PRIVATE_YES = 1; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* User name is public. |
70
|
|
|
* |
71
|
|
|
* @var int |
72
|
|
|
*/ |
73
|
|
|
const PRIVATE_NO = 0; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* User status Deleted. |
77
|
|
|
* |
78
|
|
|
* @var int |
79
|
|
|
*/ |
80
|
|
|
const DELETED_USERS = 1; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* User status not deleted. |
84
|
|
|
* |
85
|
|
|
* @var int |
86
|
|
|
*/ |
87
|
|
|
const NOT_DELETED_USERS = 0; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* User status active. (Standard). |
91
|
|
|
* |
92
|
|
|
* @var int |
93
|
|
|
*/ |
94
|
|
|
const ACTIVE_USER = 1; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* User status blocked. (Too many login attempts). |
98
|
|
|
* |
99
|
|
|
* @var int |
100
|
|
|
*/ |
101
|
|
|
const BLOCKED_USER = 2; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* User status inactive. (Cannot login at the moment). |
105
|
|
|
* |
106
|
|
|
* @var int |
107
|
|
|
*/ |
108
|
|
|
const INACTIVE_USER = 0; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* The database table used by the model. |
112
|
|
|
* |
113
|
|
|
* @var string |
114
|
|
|
*/ |
115
|
|
|
protected $table = 'users'; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The attributes that are mass assignable. |
119
|
|
|
* |
120
|
|
|
* @var array |
121
|
|
|
*/ |
122
|
|
|
protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status']; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* The attributes excluded from the model's JSON form. |
126
|
|
|
* |
127
|
|
|
* @var array |
128
|
|
|
*/ |
129
|
|
|
protected $hidden = ['password', 'remember_token']; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Collection of user permissions. |
133
|
|
|
* |
134
|
|
|
* @var Eloquent\Collection |
135
|
|
|
*/ |
136
|
|
|
protected $permission; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Checks to see if $this user is current user. |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function me() |
144
|
|
|
{ |
145
|
|
|
return $this->id == $this->getLoggedUser()->id; |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Return user full name with property "fullname". |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getFullNameAttribute() |
154
|
|
|
{ |
155
|
|
|
if (Gate::allows('viewName', $this)) { |
156
|
|
|
return $this->attributes['firstname'] . ' ' . $this->attributes['lastname']; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return trans('tinyissue.anonymous'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return user image. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getImageAttribute() |
168
|
|
|
{ |
169
|
|
|
return app('gravatar')->src($this->email); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns list of user statuses. |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
public static function getStatuses() |
178
|
|
|
{ |
179
|
|
|
return [ |
180
|
|
|
static::ACTIVE_USER => trans('tinyissue.active'), |
181
|
|
|
static::BLOCKED_USER => trans('tinyissue.blocked'), |
182
|
|
|
static::INACTIVE_USER => trans('tinyissue.inactive'), |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Whether or not the user is active. |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function isActive() |
192
|
|
|
{ |
193
|
|
|
return (int) $this->status === static::ACTIVE_USER; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Whether or not the user is inactive. |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function isInactive() |
202
|
|
|
{ |
203
|
|
|
return (int) $this->status === static::INACTIVE_USER; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Whether or not the user is blocked. |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function isBlocked() |
212
|
|
|
{ |
213
|
|
|
return (int) $this->status === static::BLOCKED_USER; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Whether or not the user is normal user role. |
218
|
|
|
* |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
|
|
public function isUser() |
222
|
|
|
{ |
223
|
|
|
return $this->exists && $this->role->role === Role::ROLE_USER; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Whether or not the user is administrator. |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
|
|
public function isAdmin() |
232
|
|
|
{ |
233
|
|
|
return $this->exists && $this->role->role === Role::ROLE_ADMIN; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Whether or not the user is manager. |
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function isManager() |
242
|
|
|
{ |
243
|
|
|
return $this->exists && $this->role->role === Role::ROLE_MANAGER; |
244
|
|
|
} |
245
|
|
|
/** |
246
|
|
|
* Whether or not the user is developer. |
247
|
|
|
* |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function isDeveloper() |
251
|
|
|
{ |
252
|
|
|
return $this->exists && $this->role->role === Role::ROLE_DEVELOPER; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function isManagerOrMore() |
256
|
|
|
{ |
257
|
|
|
return $this->isAdmin() || $this->isManager(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function isDeveloperOrMore() |
261
|
|
|
{ |
262
|
|
|
return $this->isAdmin() || $this->isManager() || $this->isDeveloper(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function getRoleName() |
266
|
|
|
{ |
267
|
|
|
return $this->role->role; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: