2021-06-05 09:26:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use App\Classes\Pterodactyl;
|
2021-09-09 18:50:16 +00:00
|
|
|
use App\Events\UserUpdatedEvent;
|
2021-06-05 09:26:32 +00:00
|
|
|
use App\Notifications\Auth\QueuedVerifyEmail;
|
|
|
|
use App\Notifications\WelcomeMessage;
|
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-07-10 06:58:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2021-06-05 09:26:32 +00:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
use Spatie\Activitylog\Traits\CausesActivity;
|
|
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* Class User
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
|
|
|
{
|
|
|
|
use HasFactory, Notifiable, LogsActivity, CausesActivity;
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
protected static $logAttributes = ['name', 'email'];
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
protected static $ignoreChangedAttributes = [
|
|
|
|
'remember_token',
|
|
|
|
'credits',
|
|
|
|
'updated_at',
|
|
|
|
'server_limit',
|
|
|
|
'last_seen',
|
|
|
|
'ip',
|
|
|
|
'pterodactyl_id'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'ip',
|
|
|
|
'mac',
|
|
|
|
'last_seen',
|
|
|
|
'role',
|
|
|
|
'credits',
|
|
|
|
'email',
|
|
|
|
'server_limit',
|
|
|
|
'password',
|
|
|
|
'pterodactyl_id',
|
|
|
|
'discord_verified_at',
|
|
|
|
'avatar'
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be hidden for arrays.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = [
|
|
|
|
'password',
|
|
|
|
'remember_token',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The attributes that should be cast to native types.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
'last_seen' => 'datetime',
|
|
|
|
];
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::created(function (User $user) {
|
|
|
|
$user->notify(new WelcomeMessage($user));
|
|
|
|
});
|
|
|
|
|
|
|
|
static::deleting(function (User $user) {
|
|
|
|
$user->servers()->chunk(10 , function ($servers) {
|
|
|
|
foreach ($servers as $server) {
|
|
|
|
$server->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$user->payments()->chunk(10 , function ($payments) {
|
|
|
|
foreach ($payments as $payment) {
|
|
|
|
$payment->delete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-10 07:24:14 +00:00
|
|
|
$user->vouchers()->detach();
|
|
|
|
|
2021-07-13 19:33:08 +00:00
|
|
|
$user->discordUser()->delete();
|
|
|
|
|
2021-06-05 09:26:32 +00:00
|
|
|
Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
|
|
|
|
});
|
2021-09-09 18:50:16 +00:00
|
|
|
|
|
|
|
static::updated(function (User $user){
|
|
|
|
event(new UserUpdatedEvent($user));
|
|
|
|
});
|
2021-06-05 09:26:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function sendEmailVerificationNotification()
|
|
|
|
{
|
|
|
|
$this->notify(new QueuedVerifyEmail);
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function credits()
|
|
|
|
{
|
|
|
|
return number_format($this->credits, 2, '.', '');
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function getAvatar(){
|
|
|
|
return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function creditUsage()
|
|
|
|
{
|
|
|
|
$usage = 0;
|
|
|
|
|
|
|
|
foreach ($this->Servers as $server){
|
|
|
|
$usage += $server->product->price;
|
|
|
|
}
|
|
|
|
|
|
|
|
return number_format($usage, 2, '.', '');
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return array|string|string[]
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function getVerifiedStatus(){
|
|
|
|
$status = '';
|
|
|
|
if ($this->hasVerifiedEmail()) $status .= 'email ';
|
|
|
|
if ($this->discordUser()->exists()) $status .= 'discord';
|
|
|
|
$status = str_replace(' ' , '/' , $status);
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
|
|
|
public function vouchers(){
|
|
|
|
return $this->belongsToMany(Voucher::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasOne
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function discordUser(){
|
|
|
|
return $this->hasOne(DiscordUser::class);
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function servers()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Server::class);
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:58:11 +00:00
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
2021-06-05 09:26:32 +00:00
|
|
|
public function payments()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Payment::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|