ctrlpanel/app/Models/User.php

301 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Models;
use App\Classes\Pterodactyl;
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;
2023-01-05 18:33:46 +00:00
use Spatie\Activitylog\LogOptions;
2021-06-05 09:26:32 +00:00
use Spatie\Activitylog\Traits\CausesActivity;
use Spatie\Activitylog\Traits\LogsActivity;
2021-07-10 06:58:11 +00:00
/**
* Class User
*/
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',
2021-06-05 09:26:32 +00:00
];
/**
* 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',
2021-09-26 15:50:13 +00:00
'avatar',
2022-06-02 14:11:24 +00:00
'suspended',
'referral_code',
'email_verified_reward'
2021-06-05 09:26:32 +00:00
];
/**
* 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',
2021-11-11 14:44:52 +00:00
'last_seen' => 'datetime',
'credits' => 'float',
'server_limit' => 'float',
'email_verified_reward' => 'boolean'
2021-06-05 09:26:32 +00:00
];
public static function boot()
{
parent::boot();
static::created(function (User $user) {
$user->notify(new WelcomeMessage($user));
2021-06-05 09:26:32 +00:00
});
static::deleting(function (User $user) {
// delete every server the user owns without using chunks
$user->servers()->each(function ($server) {
$server->delete();
2022-11-29 08:20:24 +00:00
});
$user->payments()->delete();
$user->tickets()->delete();
2022-11-29 08:20:24 +00:00
$user->ticketBlackList()->delete();
$user->vouchers()->detach();
$user->discordUser()->delete();
Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
2021-06-05 09:26:32 +00:00
});
}
2021-09-26 15:50:13 +00:00
/**
* @return HasMany
*/
public function servers()
{
return $this->hasMany(Server::class);
}
/**
* @return HasMany
*/
public function payments()
{
return $this->hasMany(Payment::class);
}
2022-11-29 08:20:24 +00:00
/**
* @return HasMany
*/
public function tickets()
{
return $this->hasMany(Ticket::class);
}
/**
* @return HasMany
*/
public function ticketBlackList()
{
return $this->hasMany(TicketBlacklist::class);
}
2021-09-26 15:50:13 +00:00
/**
* @return BelongsToMany
*/
public function vouchers()
{
return $this->belongsToMany(Voucher::class);
}
/**
* @return HasOne
*/
public function discordUser()
{
return $this->hasOne(DiscordUser::class);
}
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
/**
2021-09-26 15:50:13 +00:00
* @return bool
2021-07-10 06:58:11 +00:00
*/
2021-09-26 15:50:13 +00:00
public function isSuspended()
{
return $this->suspended;
2021-06-05 09:26:32 +00:00
}
/**
* @throws Exception
*/
2021-09-26 15:50:13 +00:00
public function suspend()
2021-06-05 09:26:32 +00:00
{
2021-09-27 18:27:06 +00:00
foreach ($this->servers as $server) {
$server->suspend();
}
2021-09-26 15:50:13 +00:00
$this->update([
'suspended' => true,
2021-09-26 15:50:13 +00:00
]);
2021-06-05 09:26:32 +00:00
2021-09-26 15:50:13 +00:00
return $this;
2021-06-05 09:26:32 +00:00
}
/**
* @throws Exception
*/
2021-09-26 15:50:13 +00:00
public function unSuspend()
{
foreach ($this->getServersWithProduct() as $server) {
2021-09-27 18:27:06 +00:00
if ($this->credits >= $server->product->getHourlyPrice()) {
$server->unSuspend();
}
}
2021-09-26 15:50:13 +00:00
$this->update([
'suspended' => false,
2021-09-26 15:50:13 +00:00
]);
2021-06-05 09:26:32 +00:00
2021-09-26 15:50:13 +00:00
return $this;
2021-07-10 06:58:11 +00:00
}
2022-11-29 08:20:24 +00:00
private function getServersWithProduct()
{
return $this->servers()
->with('product')
->get();
}
2021-07-10 06:58:11 +00:00
/**
2021-09-26 15:50:13 +00:00
* @return string
2021-07-10 06:58:11 +00:00
*/
2021-09-26 15:50:13 +00:00
public function getAvatar()
{
//TODO loading the images to confirm they exist is causing to much load time. alternative has to be found :) maybe onerror tag on the <img tags>
// if ($this->discordUser()->exists()) {
// if(@getimagesize($this->discordUser->getAvatar())) {
// $avatar = $this->discordUser->getAvatar();
// } else {
// $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
// }
// } else {
// $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
// }
return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email)));
2021-06-05 09:26:32 +00:00
}
/**
* @return string
*/
2021-09-26 15:50:13 +00:00
public function creditUsage()
2022-11-29 08:20:24 +00:00
{
2021-09-26 15:50:13 +00:00
$usage = 0;
foreach ($this->getServersWithProduct() as $server) {
$usage += $server->product->price;
2021-09-26 15:50:13 +00:00
}
return number_format($usage, 2, '.', '');
2021-06-05 09:26:32 +00:00
}
2021-07-10 06:58:11 +00:00
/**
2021-09-26 15:50:13 +00:00
* @return array|string|string[]
2021-07-10 06:58:11 +00:00
*/
2021-09-26 15:50:13 +00:00
public function getVerifiedStatus()
2021-06-05 09:26:32 +00:00
{
2021-09-26 15:50:13 +00:00
$status = '';
if ($this->hasVerifiedEmail()) {
$status .= 'email ';
}
if ($this->discordUser()->exists()) {
$status .= 'discord';
}
2021-09-26 15:50:13 +00:00
$status = str_replace(' ', '/', $status);
2021-09-26 15:50:13 +00:00
return $status;
2021-06-05 09:26:32 +00:00
}
2022-08-10 18:11:02 +00:00
2022-08-10 18:14:07 +00:00
public function verifyEmail()
{
$this->forceFill([
'email_verified_at' => now(),
])->save();
}
2022-11-29 08:20:24 +00:00
2022-11-02 16:53:52 +00:00
public function reVerifyEmail()
{
$this->forceFill([
2023-09-16 19:40:09 +00:00
'email_verified_at' => null
2022-11-02 16:53:52 +00:00
])->save();
}
2023-01-05 18:33:46 +00:00
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
->logOnlyDirty()
->dontSubmitEmptyLogs();
2023-01-05 18:33:46 +00:00
}
2021-06-05 09:26:32 +00:00
}