User.php 6.6 KB

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