-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathUser.php
executable file
·242 lines (215 loc) · 5.57 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace App\Models;
use App\Models\Presenters\UserPresenter;
use App\Notifications\GreetNotification;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Notification;
use Laravel\Sanctum\HasApiTokens;
use NotificationChannels\WebPush\HasPushSubscriptions;
use Orchid\Access\UserAccess;
use Orchid\Filters\Filterable;
use Orchid\Filters\Types\Like;
use Orchid\Filters\Types\Where;
use Orchid\Filters\Types\WhereDateStartEnd;
use Orchid\Metrics\Chartable;
use Orchid\Screen\AsSource;
use Orchid\Support\Facades\Dashboard;
use Overtrue\LaravelLike\Traits\Liker;
class User extends Authenticatable
{
use AsSource, Chartable, Filterable, HasApiTokens, HasFactory, HasPushSubscriptions, Liker, Notifiable, UserAccess;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'avatar',
'nickname',
'github_id',
'about',
'github_name',
'github_bio',
'selected_achievement',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'remember_token',
'github_id',
'permissions',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'permissions' => 'array',
];
/**
* The attributes for which you can use filters in url.
*
* @var array
*/
protected $allowedFilters = [
'id' => Where::class,
'name' => Like::class,
'email' => Like::class,
'nickname' => Like::class,
'updated_at' => WhereDateStartEnd::class,
'created_at' => WhereDateStartEnd::class,
];
/**
* The attributes for which can use sort in url.
*
* @var array
*/
protected $allowedSorts = [
'id',
'name',
'email',
'updated_at',
'created_at',
];
/**
* Boot the model's events.
*
* @return void
*/
protected static function booted(): void
{
static::created(function (User $user) {
Notification::send($user, new GreetNotification());
});
}
/**
* Throw an exception if email already exists, create admin user.
*
* @throws \Throwable
*/
public static function createAdmin(string $name, string $email, string $password): void
{
throw_if(static::where('email', $email)->exists(), 'User exists');
static::create([
'name' => $name,
'email' => $email,
'permissions' => Dashboard::getAllowAllPermission(),
]);
}
/**
* @return UserPresenter
*/
public function presenter()
{
return new UserPresenter($this);
}
public function posts()
{
return $this->hasMany(Post::class);
}
/**
* @return $this
*/
public function gatNameAttribute(): static
{
return $this->name ?? $this->github_name ?? 'Unknown';
}
/**
* Returns all comments that this user has made.
*/
public function comments()
{
return $this->hasMany(Comment::class);
}
/**
* Returns all packages that this user has made.
*/
public function packages()
{
return $this->hasMany(Package::class);
}
/**
* Returns all codeSnippets that this user has made.
*/
public function codeSnippets()
{
return $this->hasMany(CodeSnippet::class);
}
/**
* Returns only approved comments that this user has made.
*/
public function approvedComments()
{
return $this->morphMany(Comment::class, 'commenter')->where('approved', true);
}
public function meets()
{
return $this->hasMany(Meet::class);
}
public function positions()
{
return $this->hasMany(Position::class);
}
/**
* Define the "achievements" relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function achievements()
{
return $this->hasMany(Achievement::class);
}
/**
* Define the "achievements" relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function challengesReapositories()
{
return $this->hasMany(ChallengeApplication::class);
}
/**
* Reward the user with an achievement.
*
* @param string $type
*
* @return \App\Models\Achievement
*/
public function reward(string $type): Achievement
{
$exist = $this->achievements()->get()->where('achievement_type', $type);
if ($exist->isNotEmpty()) {
return $exist->first();
}
return $this->achievements()->create([
'achievement_type' => $type,
]);
}
/**
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
protected function milestone(): Attribute
{
return Attribute::make(
get: function () {
if ($this->selected_achievement === null) {
return null;
}
try {
return app($this->selected_achievement);
} catch (\Throwable) {
return null;
}
},
);
}
}