User.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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\Traits\CausesActivity;
  14. use Spatie\Activitylog\Traits\LogsActivity;
  15. /**
  16. * Class User
  17. * @package App\Models
  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. /**
  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->tickets()->chunk(10, function ($tickets) {
  100. foreach ($tickets as $ticket) {
  101. $ticket->delete();
  102. }
  103. });
  104. $user->ticketBlackList()->delete();
  105. $user->vouchers()->detach();
  106. $user->discordUser()->delete();
  107. Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
  108. });
  109. }
  110. /**
  111. * @return HasMany
  112. */
  113. public function servers()
  114. {
  115. return $this->hasMany(Server::class);
  116. }
  117. /**
  118. * @return HasMany
  119. */
  120. public function payments()
  121. {
  122. return $this->hasMany(Payment::class);
  123. }
  124. /**
  125. * @return HasMany
  126. */
  127. public function tickets()
  128. {
  129. return $this->hasMany(Ticket::class);
  130. }
  131. /**
  132. * @return HasMany
  133. */
  134. public function ticketBlackList()
  135. {
  136. return $this->hasMany(TicketBlacklist::class);
  137. }
  138. /**
  139. * @return BelongsToMany
  140. */
  141. public function vouchers()
  142. {
  143. return $this->belongsToMany(Voucher::class);
  144. }
  145. /**
  146. * @return HasOne
  147. */
  148. public function discordUser()
  149. {
  150. return $this->hasOne(DiscordUser::class);
  151. }
  152. /**
  153. *
  154. */
  155. public function sendEmailVerificationNotification()
  156. {
  157. $this->notify(new QueuedVerifyEmail);
  158. }
  159. /**
  160. * @return string
  161. */
  162. public function credits()
  163. {
  164. return number_format($this->credits, 2, '.', '');
  165. }
  166. /**
  167. * @return bool
  168. */
  169. public function isSuspended()
  170. {
  171. return $this->suspended;
  172. }
  173. /**
  174. *
  175. * @throws Exception
  176. */
  177. public function suspend()
  178. {
  179. foreach ($this->servers as $server) {
  180. $server->suspend();
  181. }
  182. $this->update([
  183. 'suspended' => true
  184. ]);
  185. return $this;
  186. }
  187. /**
  188. * @throws Exception
  189. */
  190. public function unSuspend()
  191. {
  192. foreach ($this->getServersWithProduct() as $server) {
  193. if ($this->credits >= $server->product->getHourlyPrice()) {
  194. $server->unSuspend();
  195. }
  196. }
  197. $this->update([
  198. 'suspended' => false
  199. ]);
  200. return $this;
  201. }
  202. private function getServersWithProduct()
  203. {
  204. return $this->servers()
  205. ->with('product')
  206. ->get();
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function getAvatar()
  212. {
  213. //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>
  214. // if ($this->discordUser()->exists()) {
  215. // if(@getimagesize($this->discordUser->getAvatar())) {
  216. // $avatar = $this->discordUser->getAvatar();
  217. // } else {
  218. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  219. // }
  220. // } else {
  221. // $avatar = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  222. // }
  223. return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
  224. }
  225. /**
  226. * @return string
  227. */
  228. public function creditUsage()
  229. {
  230. $usage = 0;
  231. foreach ($this->getServersWithProduct() as $server) {
  232. $usage += $server->product->price;
  233. }
  234. return number_format($usage, 2, '.', '');
  235. }
  236. /**
  237. * @return array|string|string[]
  238. */
  239. public function getVerifiedStatus()
  240. {
  241. $status = '';
  242. if ($this->hasVerifiedEmail()) $status .= 'email ';
  243. if ($this->discordUser()->exists()) $status .= 'discord';
  244. $status = str_replace(' ', '/', $status);
  245. return $status;
  246. }
  247. public function verifyEmail()
  248. {
  249. $this->forceFill([
  250. 'email_verified_at' => now(),
  251. ])->save();
  252. }
  253. public function reVerifyEmail()
  254. {
  255. $this->forceFill([
  256. 'email_verified_at' => null,
  257. ])->save();
  258. }
  259. }