1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
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
|
|
|
# == Schema Information |
13
|
|
|
# |
14
|
|
|
# Table name: users |
15
|
|
|
# |
16
|
|
|
# id :integer not null, primary key |
17
|
|
|
# username :string(255) |
18
|
|
|
# password :string(255) |
19
|
|
|
# remember_token :string(100) |
20
|
|
|
# email :integer |
21
|
|
|
# api_key :string(255) |
22
|
|
|
# active :boolean default(FALSE) |
23
|
|
|
# level :integer default(2) |
24
|
|
|
# created_at :timestamp |
25
|
|
|
# updated_at :timestamp |
26
|
|
|
# |
27
|
|
|
|
28
|
|
|
namespace Gitamin\Models; |
29
|
|
|
|
30
|
|
|
use AltThree\Validator\ValidatingTrait; |
31
|
|
|
use Illuminate\Auth\Authenticatable; |
32
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
33
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
34
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
35
|
|
|
use Illuminate\Database\Eloquent\Model; |
36
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
37
|
|
|
use Illuminate\Support\Facades\Hash; |
38
|
|
|
use Zizaco\Entrust\Traits\EntrustUserTrait; |
39
|
|
|
|
40
|
|
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract |
41
|
|
|
{ |
42
|
|
|
use Authenticatable, CanResetPassword, ValidatingTrait, EntrustUserTrait; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The attributes that should be casted to native types. |
46
|
|
|
* |
47
|
|
|
* @var string[] |
48
|
|
|
*/ |
49
|
|
|
protected $casts = [ |
50
|
|
|
'id' => 'int', |
51
|
|
|
'username' => 'string', |
52
|
|
|
'email' => 'string', |
53
|
|
|
'api_key' => 'string', |
54
|
|
|
'active' => 'bool', |
55
|
|
|
'level' => 'int', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The properties that cannot be mass assigned. |
60
|
|
|
* |
61
|
|
|
* @var string[] |
62
|
|
|
*/ |
63
|
|
|
protected $guarded = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The hidden properties. |
67
|
|
|
* |
68
|
|
|
* These are excluded when we are serializing the model. |
69
|
|
|
* |
70
|
|
|
* @var string[] |
71
|
|
|
*/ |
72
|
|
|
protected $hidden = ['password', 'remember_token']; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The validation rules. |
76
|
|
|
* |
77
|
|
|
* @var string[] |
78
|
|
|
*/ |
79
|
|
|
public $rules = [ |
80
|
|
|
'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'], |
81
|
|
|
'email' => 'required|email', |
82
|
|
|
'password' => 'required', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Overrides the models boot method. |
87
|
|
|
*/ |
88
|
|
|
public static function boot() |
89
|
|
|
{ |
90
|
|
|
parent::boot(); |
91
|
|
|
|
92
|
|
|
self::creating(function ($user) { |
93
|
|
|
if (! $user->api_key) { |
94
|
|
|
$user->api_key = self::generateApiKey(); |
95
|
|
|
} |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Hash any password being inserted by default. |
101
|
|
|
* |
102
|
|
|
* @param string $password |
103
|
|
|
* |
104
|
|
|
* @return \Gitamin\Models\User |
105
|
|
|
*/ |
106
|
|
|
public function setPasswordAttribute($password) |
107
|
|
|
{ |
108
|
|
|
$this->attributes['password'] = Hash::make($password); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns a Gravatar URL for the users email address. |
115
|
|
|
* |
116
|
|
|
* @param int $size |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getGravatarAttribute($size = 200) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
return 'https://avatars2.githubusercontent.com/u/15867969?v=3&s=40'; |
123
|
|
|
//return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5($this->email), $size); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Find by api_key, or throw an exception. |
128
|
|
|
* |
129
|
|
|
* @param string $token |
130
|
|
|
* @param string[] $columns |
131
|
|
|
* |
132
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
133
|
|
|
* |
134
|
|
|
* @return \Gitamin\Models\User |
135
|
|
|
*/ |
136
|
|
|
public static function findByApiToken($token, $columns = ['*']) |
137
|
|
|
{ |
138
|
|
|
$user = static::where('api_key', $token)->first($columns); |
139
|
|
|
|
140
|
|
|
if (! $user) { |
141
|
|
|
throw new ModelNotFoundException(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $user; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns an API key. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public static function generateApiKey() |
153
|
|
|
{ |
154
|
|
|
return str_random(20); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns whether a user is approved. |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isApproved() |
163
|
|
|
{ |
164
|
|
|
return $this->active == 1; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns whether a user is at admin level. |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function getIsAdminAttribute() |
173
|
|
|
{ |
174
|
|
|
return $this->level === 1; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* A user can have many issues. |
179
|
|
|
* |
180
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
181
|
|
|
*/ |
182
|
|
|
public function issues() |
183
|
|
|
{ |
184
|
|
|
return $this->hasMany(Issue::class, 'user_id', 'id'); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.