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