123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace App\Models;
- use App\Classes\Pterodactyl;
- use App\Notifications\Auth\QueuedVerifyEmail;
- use App\Notifications\WelcomeMessage;
- use Illuminate\Contracts\Auth\MustVerifyEmail;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Spatie\Activitylog\LogOptions;
- use Spatie\Activitylog\Traits\CausesActivity;
- use Spatie\Activitylog\Traits\LogsActivity;
- /**
- * Class User
- */
- class User extends Authenticatable implements MustVerifyEmail
- {
- use HasFactory, Notifiable, LogsActivity, CausesActivity;
- /**
- * @var string[]
- */
- protected static $logAttributes = ['name', 'email'];
- /**
- * @var string[]
- */
- 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',
- 'suspended',
- 'referral_code',
- 'email_verified_reward'
- ];
- /**
- * 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',
- 'credits' => 'float',
- 'server_limit' => 'float',
- 'email_verified_reward' => 'boolean'
- ];
- public static function boot()
- {
- parent::boot();
- static::created(function (User $user) {
- $user->notify(new WelcomeMessage($user));
- });
- static::deleting(function (User $user) {
- // delete every server the user owns without using chunks
- $user->servers()->each(function ($server) {
- $server->delete();
- });
- $user->payments()->delete();
- $user->tickets()->delete();
- $user->ticketBlackList()->delete();
- $user->vouchers()->detach();
- $user->discordUser()->delete();
- Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
- });
- }
- /**
- * @return HasMany
- */
- public function servers()
- {
- return $this->hasMany(Server::class);
- }
- /**
- * @return HasMany
- */
- public function payments()
- {
- return $this->hasMany(Payment::class);
- }
- /**
- * @return HasMany
- */
- public function tickets()
- {
- return $this->hasMany(Ticket::class);
- }
- /**
- * @return HasMany
- */
- public function ticketBlackList()
- {
- return $this->hasMany(TicketBlacklist::class);
- }
- /**
- * @return BelongsToMany
- */
- public function vouchers()
- {
- return $this->belongsToMany(Voucher::class);
- }
- /**
- * @return HasOne
- */
- public function discordUser()
- {
- return $this->hasOne(DiscordUser::class);
- }
- public function sendEmailVerificationNotification()
- {
- $this->notify(new QueuedVerifyEmail);
- }
- /**
- * @return string
- */
- public function credits()
- {
- return number_format($this->credits, 2, '.', '');
- }
- /**
- * @return bool
- */
- public function isSuspended()
- {
- return $this->suspended;
- }
- /**
- * @throws Exception
- */
- public function suspend()
- {
- foreach ($this->servers as $server) {
- $server->suspend();
- }
- $this->update([
- 'suspended' => true,
- ]);
- return $this;
- }
- /**
- * @throws Exception
- */
- public function unSuspend()
- {
- foreach ($this->getServersWithProduct() as $server) {
- if ($this->credits >= $server->product->getHourlyPrice()) {
- $server->unSuspend();
- }
- }
- $this->update([
- 'suspended' => false,
- ]);
- return $this;
- }
- private function getServersWithProduct()
- {
- return $this->servers()
- ->with('product')
- ->get();
- }
- /**
- * @return string
- */
- 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)));
- }
- /**
- * @return string
- */
- public function creditUsage()
- {
- $usage = 0;
- foreach ($this->getServersWithProduct() as $server) {
- $usage += $server->product->price;
- }
- return number_format($usage, 2, '.', '');
- }
- /**
- * @return array|string|string[]
- */
- public function getVerifiedStatus()
- {
- $status = '';
- if ($this->hasVerifiedEmail()) {
- $status .= 'email ';
- }
- if ($this->discordUser()->exists()) {
- $status .= 'discord';
- }
- $status = str_replace(' ', '/', $status);
- return $status;
- }
- public function verifyEmail()
- {
- $this->forceFill([
- 'email_verified_at' => now(),
- ])->save();
- }
- public function reVerifyEmail()
- {
- $this->forceFill([
- 'email_verified_at' => null
- ])->save();
- }
- public function getActivitylogOptions(): LogOptions
- {
- return LogOptions::defaults()
- ->logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
- ->logOnlyDirty()
- ->dontSubmitEmptyLogs();
- }
- }
|