User.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace App\Models;
  3. use App\Classes\Pterodactyl;
  4. use App\Events\UserUpdateCreditsEvent;
  5. use App\Notifications\Auth\QueuedVerifyEmail;
  6. use App\Notifications\WelcomeMessage;
  7. use Illuminate\Contracts\Auth\MustVerifyEmail;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Database\Eloquent\Relations\HasOne;
  12. use Illuminate\Foundation\Auth\User as Authenticatable;
  13. use Illuminate\Notifications\Notifiable;
  14. use Spatie\Activitylog\Traits\CausesActivity;
  15. use Spatie\Activitylog\Traits\LogsActivity;
  16. /**
  17. * Class User
  18. * @package App\Models
  19. */
  20. class User extends Authenticatable implements MustVerifyEmail
  21. {
  22. use HasFactory, Notifiable, LogsActivity, CausesActivity;
  23. /**
  24. * @var string[]
  25. */
  26. protected static $logAttributes = ['name', 'email'];
  27. /**
  28. * @var string[]
  29. */
  30. protected static $ignoreChangedAttributes = [
  31. 'remember_token',
  32. 'credits',
  33. 'updated_at',
  34. 'server_limit',
  35. 'last_seen',
  36. 'ip',
  37. 'pterodactyl_id'
  38. ];
  39. /**
  40. * The attributes that are mass assignable.
  41. *
  42. * @var array
  43. */
  44. protected $fillable = [
  45. 'name',
  46. 'ip',
  47. 'mac',
  48. 'last_seen',
  49. 'role',
  50. 'credits',
  51. 'email',
  52. 'server_limit',
  53. 'password',
  54. 'pterodactyl_id',
  55. 'discord_verified_at',
  56. 'avatar',
  57. 'suspended'
  58. ];
  59. /**
  60. * The attributes that should be hidden for arrays.
  61. *
  62. * @var array
  63. */
  64. protected $hidden = [
  65. 'password',
  66. 'remember_token',
  67. ];
  68. /**
  69. * The attributes that should be cast to native types.
  70. *
  71. * @var array
  72. */
  73. protected $casts = [
  74. 'email_verified_at' => 'datetime',
  75. 'last_seen' => 'datetime',
  76. 'credits' => 'float',
  77. 'server_limit' => 'float',
  78. ];
  79. /**
  80. *
  81. */
  82. public static function boot()
  83. {
  84. parent::boot();
  85. static::created(function (User $user) {
  86. $user->notify(new WelcomeMessage($user));
  87. });
  88. static::deleting(function (User $user) {
  89. $user->servers()->chunk(10, function ($servers) {
  90. foreach ($servers as $server) {
  91. $server->delete();
  92. }
  93. });
  94. $user->payments()->chunk(10, function ($payments) {
  95. foreach ($payments as $payment) {
  96. $payment->delete();
  97. }
  98. });
  99. $user->vouchers()->detach();
  100. $user->discordUser()->delete();
  101. Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
  102. });
  103. }
  104. /**
  105. * @return HasMany
  106. */
  107. public function servers()
  108. {
  109. return $this->hasMany(Server::class);
  110. }
  111. /**
  112. * @return HasMany
  113. */
  114. public function payments()
  115. {
  116. return $this->hasMany(Payment::class);
  117. }
  118. /**
  119. * @return BelongsToMany
  120. */
  121. public function vouchers()
  122. {
  123. return $this->belongsToMany(Voucher::class);
  124. }
  125. /**
  126. * @return HasOne
  127. */
  128. public function discordUser()
  129. {
  130. return $this->hasOne(DiscordUser::class);
  131. }
  132. /**
  133. *
  134. */
  135. public function sendEmailVerificationNotification()
  136. {
  137. $this->notify(new QueuedVerifyEmail);
  138. }
  139. /**
  140. * @return string
  141. */
  142. public function credits()
  143. {
  144. return number_format($this->credits, 2, '.', '');
  145. }
  146. /**
  147. * @return bool
  148. */
  149. public function isSuspended()
  150. {
  151. return $this->suspended;
  152. }
  153. /**
  154. *
  155. * @throws Exception
  156. */
  157. public function suspend()
  158. {
  159. foreach ($this->servers as $server) {
  160. $server->suspend();
  161. }
  162. $this->update([
  163. 'suspended' => true
  164. ]);
  165. return $this;
  166. }
  167. /**
  168. * @throws Exception
  169. */
  170. public function unSuspend()
  171. {
  172. foreach ($this->servers as $server) {
  173. if ($this->credits >= $server->product->getHourlyPrice()) {
  174. $server->unSuspend();
  175. }
  176. }
  177. $this->update([
  178. 'suspended' => false
  179. ]);
  180. return $this;
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getAvatar()
  186. {
  187. //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>
  188. // if ($this->discordUser()->exists()) {
  189. // if(@getimagesize($this->discordUser->getAvatar())) {
  190. // $avatar = $this->discordUser->getAvatar();
  191. // } else {
  192. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  193. // }
  194. // } else {
  195. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  196. // }
  197. return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  198. }
  199. /**
  200. * @return string
  201. */
  202. public function creditUsage()
  203. {
  204. $usage = 0;
  205. foreach ($this->Servers as $server) {
  206. $usage += $server->product->price;
  207. }
  208. return number_format($usage, 2, '.', '');
  209. }
  210. /**
  211. * @return array|string|string[]
  212. */
  213. public function getVerifiedStatus()
  214. {
  215. $status = '';
  216. if ($this->hasVerifiedEmail()) $status .= 'email ';
  217. if ($this->discordUser()->exists()) $status .= 'discord';
  218. $status = str_replace(' ', '/', $status);
  219. return $status;
  220. }
  221. }