User.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. 'referral_code'
  59. ];
  60. /**
  61. * The attributes that should be hidden for arrays.
  62. *
  63. * @var array
  64. */
  65. protected $hidden = [
  66. 'password',
  67. 'remember_token',
  68. ];
  69. /**
  70. * The attributes that should be cast to native types.
  71. *
  72. * @var array
  73. */
  74. protected $casts = [
  75. 'email_verified_at' => 'datetime',
  76. 'last_seen' => 'datetime',
  77. 'credits' => 'float',
  78. 'server_limit' => 'float',
  79. ];
  80. /**
  81. *
  82. */
  83. public static function boot()
  84. {
  85. parent::boot();
  86. static::created(function (User $user) {
  87. $user->notify(new WelcomeMessage($user));
  88. });
  89. static::deleting(function (User $user) {
  90. $user->servers()->chunk(10, function ($servers) {
  91. foreach ($servers as $server) {
  92. $server->delete();
  93. }
  94. });
  95. $user->payments()->chunk(10, function ($payments) {
  96. foreach ($payments as $payment) {
  97. $payment->delete();
  98. }
  99. });
  100. $user->vouchers()->detach();
  101. $user->discordUser()->delete();
  102. Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
  103. });
  104. }
  105. /**
  106. * @return HasMany
  107. */
  108. public function servers()
  109. {
  110. return $this->hasMany(Server::class);
  111. }
  112. /**
  113. * @return HasMany
  114. */
  115. public function payments()
  116. {
  117. return $this->hasMany(Payment::class);
  118. }
  119. /**
  120. * @return BelongsToMany
  121. */
  122. public function vouchers()
  123. {
  124. return $this->belongsToMany(Voucher::class);
  125. }
  126. /**
  127. * @return HasOne
  128. */
  129. public function discordUser()
  130. {
  131. return $this->hasOne(DiscordUser::class);
  132. }
  133. /**
  134. *
  135. */
  136. public function sendEmailVerificationNotification()
  137. {
  138. $this->notify(new QueuedVerifyEmail);
  139. }
  140. /**
  141. * @return string
  142. */
  143. public function credits()
  144. {
  145. return number_format($this->credits, 2, '.', '');
  146. }
  147. /**
  148. * @return bool
  149. */
  150. public function isSuspended()
  151. {
  152. return $this->suspended;
  153. }
  154. /**
  155. *
  156. * @throws Exception
  157. */
  158. public function suspend()
  159. {
  160. foreach ($this->servers as $server) {
  161. $server->suspend();
  162. }
  163. $this->update([
  164. 'suspended' => true
  165. ]);
  166. return $this;
  167. }
  168. /**
  169. * @throws Exception
  170. */
  171. public function unSuspend()
  172. {
  173. foreach ($this->getServersWithProduct() as $server) {
  174. if ($this->credits >= $server->product->getHourlyPrice()) {
  175. $server->unSuspend();
  176. }
  177. }
  178. $this->update([
  179. 'suspended' => false
  180. ]);
  181. return $this;
  182. }
  183. /**
  184. * @return string
  185. */
  186. public function getAvatar()
  187. {
  188. //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>
  189. // if ($this->discordUser()->exists()) {
  190. // if(@getimagesize($this->discordUser->getAvatar())) {
  191. // $avatar = $this->discordUser->getAvatar();
  192. // } else {
  193. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  194. // }
  195. // } else {
  196. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  197. // }
  198. return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  199. }
  200. /**
  201. * @return string
  202. */
  203. public function creditUsage()
  204. {
  205. $usage = 0;
  206. foreach ($this->getServersWithProduct() as $server) {
  207. $usage += $server->product->price;
  208. }
  209. return number_format($usage, 2, '.', '');
  210. }
  211. private function getServersWithProduct() {
  212. return $this->servers()
  213. ->with('product')
  214. ->get();
  215. }
  216. /**
  217. * @return array|string|string[]
  218. */
  219. public function getVerifiedStatus()
  220. {
  221. $status = '';
  222. if ($this->hasVerifiedEmail()) $status .= 'email ';
  223. if ($this->discordUser()->exists()) $status .= 'discord';
  224. $status = str_replace(' ', '/', $status);
  225. return $status;
  226. }
  227. }