User.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\Database\Eloquent\Relations\BelongsToMany;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\Relations\HasOne;
  11. use Illuminate\Foundation\Auth\User as Authenticatable;
  12. use Illuminate\Notifications\Notifiable;
  13. use Spatie\Activitylog\LogOptions;
  14. use Spatie\Activitylog\Traits\CausesActivity;
  15. use Spatie\Activitylog\Traits\LogsActivity;
  16. /**
  17. * Class User
  18. */
  19. class User extends Authenticatable implements MustVerifyEmail
  20. {
  21. use HasFactory, Notifiable, LogsActivity, CausesActivity;
  22. /**
  23. * @var string[]
  24. */
  25. protected static $logAttributes = ['name', 'email'];
  26. /**
  27. * @var string[]
  28. */
  29. protected static $ignoreChangedAttributes = [
  30. 'remember_token',
  31. 'credits',
  32. 'updated_at',
  33. 'server_limit',
  34. 'last_seen',
  35. 'ip',
  36. 'pterodactyl_id',
  37. ];
  38. /**
  39. * The attributes that are mass assignable.
  40. *
  41. * @var array
  42. */
  43. protected $fillable = [
  44. 'name',
  45. 'ip',
  46. 'mac',
  47. 'last_seen',
  48. 'role',
  49. 'credits',
  50. 'email',
  51. 'server_limit',
  52. 'password',
  53. 'pterodactyl_id',
  54. 'discord_verified_at',
  55. 'avatar',
  56. 'suspended',
  57. 'referral_code',
  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. public static function boot()
  80. {
  81. parent::boot();
  82. static::created(function (User $user) {
  83. $user->notify(new WelcomeMessage($user));
  84. });
  85. static::deleting(function (User $user) {
  86. $user->servers()->chunk(10, function ($servers) {
  87. foreach ($servers as $server) {
  88. $server->delete();
  89. }
  90. });
  91. $user->payments()->chunk(10, function ($payments) {
  92. foreach ($payments as $payment) {
  93. $payment->delete();
  94. }
  95. });
  96. $user->tickets()->chunk(10, function ($tickets) {
  97. foreach ($tickets as $ticket) {
  98. $ticket->delete();
  99. }
  100. });
  101. $user->ticketBlackList()->delete();
  102. $user->vouchers()->detach();
  103. $user->discordUser()->delete();
  104. Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
  105. });
  106. }
  107. /**
  108. * @return HasMany
  109. */
  110. public function servers()
  111. {
  112. return $this->hasMany(Server::class);
  113. }
  114. /**
  115. * @return HasMany
  116. */
  117. public function payments()
  118. {
  119. return $this->hasMany(Payment::class);
  120. }
  121. /**
  122. * @return HasMany
  123. */
  124. public function tickets()
  125. {
  126. return $this->hasMany(Ticket::class);
  127. }
  128. /**
  129. * @return HasMany
  130. */
  131. public function ticketBlackList()
  132. {
  133. return $this->hasMany(TicketBlacklist::class);
  134. }
  135. /**
  136. * @return BelongsToMany
  137. */
  138. public function vouchers()
  139. {
  140. return $this->belongsToMany(Voucher::class);
  141. }
  142. /**
  143. * @return HasOne
  144. */
  145. public function discordUser()
  146. {
  147. return $this->hasOne(DiscordUser::class);
  148. }
  149. public function sendEmailVerificationNotification()
  150. {
  151. $this->notify(new QueuedVerifyEmail);
  152. }
  153. /**
  154. * @return string
  155. */
  156. public function credits()
  157. {
  158. return number_format($this->credits, 2, '.', '');
  159. }
  160. /**
  161. * @return bool
  162. */
  163. public function isSuspended()
  164. {
  165. return $this->suspended;
  166. }
  167. /**
  168. * @throws Exception
  169. */
  170. public function suspend()
  171. {
  172. foreach ($this->servers as $server) {
  173. $server->suspend();
  174. }
  175. $this->update([
  176. 'suspended' => true,
  177. ]);
  178. return $this;
  179. }
  180. /**
  181. * @throws Exception
  182. */
  183. public function unSuspend()
  184. {
  185. foreach ($this->getServersWithProduct() as $server) {
  186. if ($this->credits >= $server->product->getHourlyPrice()) {
  187. $server->unSuspend();
  188. }
  189. }
  190. $this->update([
  191. 'suspended' => false,
  192. ]);
  193. return $this;
  194. }
  195. private function getServersWithProduct()
  196. {
  197. return $this->servers()
  198. ->with('product')
  199. ->get();
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getAvatar()
  205. {
  206. //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>
  207. // if ($this->discordUser()->exists()) {
  208. // if(@getimagesize($this->discordUser->getAvatar())) {
  209. // $avatar = $this->discordUser->getAvatar();
  210. // } else {
  211. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  212. // }
  213. // } else {
  214. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  215. // }
  216. return 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($this->email)));
  217. }
  218. /**
  219. * @return string
  220. */
  221. public function creditUsage()
  222. {
  223. $usage = 0;
  224. foreach ($this->getServersWithProduct() as $server) {
  225. $usage += $server->product->price;
  226. }
  227. return number_format($usage, 2, '.', '');
  228. }
  229. /**
  230. * @return array|string|string[]
  231. */
  232. public function getVerifiedStatus()
  233. {
  234. $status = '';
  235. if ($this->hasVerifiedEmail()) {
  236. $status .= 'email ';
  237. }
  238. if ($this->discordUser()->exists()) {
  239. $status .= 'discord';
  240. }
  241. $status = str_replace(' ', '/', $status);
  242. return $status;
  243. }
  244. public function verifyEmail()
  245. {
  246. $this->forceFill([
  247. 'email_verified_at' => now(),
  248. ])->save();
  249. }
  250. public function reVerifyEmail()
  251. {
  252. $this->forceFill([
  253. 'email_verified_at' => null,
  254. ])->save();
  255. }
  256. public function getActivitylogOptions(): LogOptions
  257. {
  258. return LogOptions::defaults()
  259. -> logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
  260. -> logOnlyDirty()
  261. -> dontSubmitEmptyLogs();
  262. }
  263. }