User.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Models;
  3. use App\Classes\Pterodactyl;
  4. use App\Notifications\Auth\QueuedVerifyEmail;
  5. use App\Notifications\WelcomeMessage;
  6. use Illuminate\Contracts\Auth\MustVerifyEmail;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Foundation\Auth\User as Authenticatable;
  9. use Illuminate\Notifications\Notifiable;
  10. use Spatie\Activitylog\Traits\CausesActivity;
  11. use Spatie\Activitylog\Traits\LogsActivity;
  12. class User extends Authenticatable implements MustVerifyEmail
  13. {
  14. use HasFactory, Notifiable, LogsActivity, CausesActivity;
  15. protected static $logAttributes = ['name', 'email'];
  16. protected static $ignoreChangedAttributes = [
  17. 'remember_token',
  18. 'credits',
  19. 'updated_at',
  20. 'server_limit',
  21. 'last_seen',
  22. 'ip',
  23. 'pterodactyl_id'
  24. ];
  25. /**
  26. * The attributes that are mass assignable.
  27. *
  28. * @var array
  29. */
  30. protected $fillable = [
  31. 'name',
  32. 'ip',
  33. 'mac',
  34. 'last_seen',
  35. 'role',
  36. 'credits',
  37. 'email',
  38. 'server_limit',
  39. 'password',
  40. 'pterodactyl_id',
  41. 'discord_verified_at',
  42. 'avatar'
  43. ];
  44. /**
  45. * The attributes that should be hidden for arrays.
  46. *
  47. * @var array
  48. */
  49. protected $hidden = [
  50. 'password',
  51. 'remember_token',
  52. ];
  53. /**
  54. * The attributes that should be cast to native types.
  55. *
  56. * @var array
  57. */
  58. protected $casts = [
  59. 'email_verified_at' => 'datetime',
  60. 'last_seen' => 'datetime',
  61. ];
  62. public static function boot()
  63. {
  64. parent::boot();
  65. static::created(function (User $user) {
  66. $user->notify(new WelcomeMessage($user));
  67. });
  68. static::deleting(function (User $user) {
  69. $user->servers()->chunk(10 , function ($servers) {
  70. foreach ($servers as $server) {
  71. $server->delete();
  72. }
  73. });
  74. $user->payments()->chunk(10 , function ($payments) {
  75. foreach ($payments as $payment) {
  76. $payment->delete();
  77. }
  78. });
  79. Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
  80. });
  81. }
  82. public function sendEmailVerificationNotification()
  83. {
  84. $this->notify(new QueuedVerifyEmail);
  85. }
  86. public function credits()
  87. {
  88. return number_format($this->credits, 2, '.', '');
  89. }
  90. public function getAvatar(){
  91. return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  92. }
  93. public function creditUsage()
  94. {
  95. $usage = 0;
  96. foreach ($this->Servers as $server){
  97. $usage += $server->product->price;
  98. }
  99. return number_format($usage, 2, '.', '');
  100. }
  101. public function getVerifiedStatus(){
  102. $status = '';
  103. if ($this->hasVerifiedEmail()) $status .= 'email ';
  104. if ($this->discordUser()->exists()) $status .= 'discord';
  105. $status = str_replace(' ' , '/' , $status);
  106. return $status;
  107. }
  108. public function discordUser(){
  109. return $this->hasOne(DiscordUser::class);
  110. }
  111. public function servers()
  112. {
  113. return $this->hasMany(Server::class);
  114. }
  115. public function payments()
  116. {
  117. return $this->hasMany(Payment::class);
  118. }
  119. }