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\Contracts\Auth\Authenticatable as AuthenticatableContract; |
18
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
19
|
|
|
use Illuminate\Database\Eloquent; |
20
|
|
|
use Illuminate\Database\Eloquent\Model; |
21
|
|
|
use Illuminate\Routing\Route; |
22
|
|
|
use Tinyissue\Model\Project\Issue; |
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
|
|
|
*/ |
40
|
|
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract |
41
|
|
|
{ |
42
|
|
|
use Authenticatable, |
43
|
|
|
CanResetPassword, |
44
|
|
|
Traits\User\CountTrait, |
45
|
|
|
Traits\User\RelationTrait, |
46
|
|
|
Traits\User\CrudTrait, |
47
|
|
|
Traits\User\QueryTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* User name is private. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
const PRIVATE_YES = 1; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* User name is public. |
58
|
|
|
* |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
const PRIVATE_NO = 0; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* User status Deleted. |
65
|
|
|
* |
66
|
|
|
* @var int |
67
|
|
|
*/ |
68
|
|
|
const DELETED_USERS = 1; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* User status not deleted. |
72
|
|
|
* |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
const NOT_DELETED_USERS = 0; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* User status active. (Standard). |
79
|
|
|
* |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
const ACTIVE_USER = 1; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* User status blocked. (Too many login attempts). |
86
|
|
|
* |
87
|
|
|
* @var int |
88
|
|
|
*/ |
89
|
|
|
const BLOCKED_USER = 2; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* User status inactive. (Cannot login at the moment). |
93
|
|
|
* |
94
|
|
|
* @var int |
95
|
|
|
*/ |
96
|
|
|
const INACTIVE_USER = 0; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The database table used by the model. |
100
|
|
|
* |
101
|
|
|
* @var string |
102
|
|
|
*/ |
103
|
|
|
protected $table = 'users'; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* The attributes that are mass assignable. |
107
|
|
|
* |
108
|
|
|
* @var array |
109
|
|
|
*/ |
110
|
|
|
protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status']; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* The attributes excluded from the model's JSON form. |
114
|
|
|
* |
115
|
|
|
* @var array |
116
|
|
|
*/ |
117
|
|
|
protected $hidden = ['password', 'remember_token']; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Collection of user permissions. |
121
|
|
|
* |
122
|
|
|
* @var Eloquent\Collection |
123
|
|
|
*/ |
124
|
|
|
protected $permission; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get available languages from translations folder. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
4 |
|
public static function getLanguages() |
132
|
|
|
{ |
133
|
4 |
|
$languages = []; |
134
|
|
|
|
135
|
4 |
|
$cdir = scandir(__DIR__ . '/../../resources/lang'); |
136
|
4 |
|
foreach ($cdir as $value) { |
137
|
4 |
|
if (!in_array($value, ['.', '..'])) { |
138
|
4 |
|
$languages[$value] = $value; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
return $languages; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Checks to see if $this user is current user. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
3 |
|
public function me() |
151
|
|
|
{ |
152
|
3 |
|
return $this->id == \Auth::user()->id; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Whether or not the user has a valid permission in current context |
157
|
|
|
* e.g. can access the issue or the project. |
158
|
|
|
* |
159
|
|
|
* @param Route $route |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
35 |
|
public function permissionInContext(Route $route) |
164
|
|
|
{ |
165
|
|
|
// Can access all projects |
166
|
35 |
|
if ($this->permission(Permission::PERM_PROJECT_ALL)) { |
167
|
30 |
|
return true; |
168
|
|
|
} |
169
|
|
|
|
170
|
8 |
|
$project = $route->getParameter('project'); |
171
|
8 |
|
$issue = $route->getParameter('issue'); |
172
|
8 |
|
$comment = $route->getParameter('comment'); |
173
|
8 |
|
$attachment = $route->getParameter('attachment'); |
174
|
8 |
|
$action = $route->getAction(); |
175
|
8 |
|
$permission = array_key_exists('permission', $action) ? $action['permission'] : ''; |
176
|
|
|
|
177
|
|
|
if ( |
178
|
8 |
|
($permission == Permission::PERM_ISSUE_MODIFY && $comment instanceof Issue\Comment && $comment->canEdit($this)) || |
179
|
8 |
|
($permission == Permission::PERM_ISSUE_MODIFY && $attachment instanceof Issue\Attachment && $attachment->canEdit($this)) || |
180
|
8 |
|
($issue instanceof Issue && $issue->canView($this)) || |
181
|
8 |
|
($project instanceof Project && $project->canView($this)) |
182
|
|
|
) { |
183
|
7 |
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
3 |
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Whether or not the user has a permission. |
191
|
|
|
* |
192
|
|
|
* @param string $key |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
57 |
|
public function permission($key) |
197
|
|
|
{ |
198
|
57 |
|
$this->loadPermissions(); |
199
|
57 |
|
foreach ($this->permission as $permission) { |
200
|
57 |
|
if ($permission->permission->isEqual($key)) { |
201
|
57 |
|
return true; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
23 |
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Return user full name with property "fullname". |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
39 |
|
public function getFullNameAttribute() |
214
|
|
|
{ |
215
|
39 |
|
if ($this->private && (Auth::guest() || !Auth::user()->permission('administration'))) { |
|
|
|
|
216
|
1 |
|
return trans('tinyissue.anonymous'); |
217
|
|
|
} |
218
|
|
|
|
219
|
39 |
|
return $this->attributes['firstname'] . ' ' . $this->attributes['lastname']; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Return user image. |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
4 |
|
public function getImageAttribute() |
228
|
|
|
{ |
229
|
4 |
|
return app('gravatar')->src($this->email); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns list of user statuses. |
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
2 |
|
public static function getStatuses() |
238
|
|
|
{ |
239
|
|
|
return [ |
240
|
2 |
|
static::ACTIVE_USER => trans('tinyissue.active'), |
241
|
2 |
|
static::BLOCKED_USER => trans('tinyissue.blocked'), |
242
|
2 |
|
static::INACTIVE_USER => trans('tinyissue.inactive'), |
243
|
|
|
]; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Whether or not the user is active. |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
public function isActive() |
252
|
|
|
{ |
253
|
|
|
return (int) $this->status === static::ACTIVE_USER; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Whether or not the user is inactive. |
258
|
|
|
* |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
|
|
public function isInactive() |
262
|
|
|
{ |
263
|
|
|
return (int) $this->status === static::INACTIVE_USER; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Whether or not the user is blocked. |
268
|
|
|
* |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function isBlocked() |
272
|
|
|
{ |
273
|
|
|
return (int) $this->status === static::BLOCKED_USER; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Whether or not the user is normal user role. |
278
|
|
|
* |
279
|
|
|
* @return bool |
280
|
|
|
*/ |
281
|
21 |
|
public function isUser() |
282
|
|
|
{ |
283
|
21 |
|
return $this->exists && $this->role->role === Role::ROLE_USER; |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Whether or not the user is administrator. |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function isAdmin() |
292
|
|
|
{ |
293
|
|
|
return $this->exists && $this->role->role === Role::ROLE_ADMIN; |
|
|
|
|
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.