User.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Models;
  3. use App\Notifications\Auth\QueuedVerifyEmail;
  4. use App\Notifications\WelcomeMessage;
  5. use App\Settings\GeneralSettings;
  6. use App\Settings\UserSettings;
  7. use App\Classes\PterodactylClient;
  8. use App\Settings\PterodactylSettings;
  9. use Illuminate\Contracts\Auth\MustVerifyEmail;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Database\Eloquent\Relations\HasOne;
  14. use Illuminate\Foundation\Auth\User as Authenticatable;
  15. use Illuminate\Notifications\Notifiable;
  16. use Spatie\Activitylog\LogOptions;
  17. use Spatie\Activitylog\Traits\CausesActivity;
  18. use Spatie\Activitylog\Traits\LogsActivity;
  19. use Spatie\Permission\Traits\HasRoles;
  20. /**
  21. * Class User
  22. */
  23. class User extends Authenticatable implements MustVerifyEmail
  24. {
  25. use HasFactory, Notifiable, LogsActivity, CausesActivity, HasRoles;
  26. private PterodactylClient $pterodactyl;
  27. /**
  28. * @var string[]
  29. */
  30. protected static $logAttributes = ['name', 'email'];
  31. /**
  32. * @var string[]
  33. */
  34. protected static $ignoreChangedAttributes = [
  35. 'remember_token',
  36. 'credits',
  37. 'updated_at',
  38. 'server_limit',
  39. 'last_seen',
  40. 'ip',
  41. 'pterodactyl_id',
  42. ];
  43. /**
  44. * The attributes that are mass assignable.
  45. *
  46. * @var array
  47. */
  48. protected $fillable = [
  49. 'name',
  50. 'ip',
  51. 'mac',
  52. 'last_seen',
  53. 'role',
  54. 'credits',
  55. 'email',
  56. 'server_limit',
  57. 'password',
  58. 'pterodactyl_id',
  59. 'discord_verified_at',
  60. 'avatar',
  61. 'suspended',
  62. 'referral_code',
  63. ];
  64. /**
  65. * The attributes that should be hidden for arrays.
  66. *
  67. * @var array
  68. */
  69. protected $hidden = [
  70. 'password',
  71. 'remember_token',
  72. ];
  73. /**
  74. * The attributes that should be cast to native types.
  75. *
  76. * @var array
  77. */
  78. protected $casts = [
  79. 'email_verified_at' => 'datetime',
  80. 'last_seen' => 'datetime',
  81. 'credits' => 'float',
  82. 'server_limit' => 'float',
  83. ];
  84. public function __construct()
  85. {
  86. $ptero_settings = new PterodactylSettings();
  87. $this->pterodactyl = new PterodactylClient($ptero_settings);
  88. }
  89. public static function boot()
  90. {
  91. parent::boot();
  92. static::created(function (User $user, GeneralSettings $general_settings, UserSettings $user_settings) {
  93. $user->notify(new WelcomeMessage($user, $general_settings, $user_settings));
  94. });
  95. static::deleting(function (User $user) {
  96. // delete every server the user owns without using chunks
  97. $user->servers()->each(function ($server) {
  98. $server->delete();
  99. });
  100. $user->payments()->delete();
  101. $user->tickets()->delete();
  102. $user->ticketBlackList()->delete();
  103. $user->vouchers()->detach();
  104. $user->discordUser()->delete();
  105. $user->pterodactyl->application->delete("/application/users/{$user->pterodactyl_id}");
  106. });
  107. }
  108. /**
  109. * @return HasMany
  110. */
  111. public function servers()
  112. {
  113. return $this->hasMany(Server::class);
  114. }
  115. /**
  116. * @return HasMany
  117. */
  118. public function payments()
  119. {
  120. return $this->hasMany(Payment::class);
  121. }
  122. /**
  123. * @return HasMany
  124. */
  125. public function tickets()
  126. {
  127. return $this->hasMany(Ticket::class);
  128. }
  129. /**
  130. * @return HasMany
  131. */
  132. public function ticketBlackList()
  133. {
  134. return $this->hasMany(TicketBlacklist::class);
  135. }
  136. /**
  137. * @return BelongsToMany
  138. */
  139. public function vouchers()
  140. {
  141. return $this->belongsToMany(Voucher::class);
  142. }
  143. /**
  144. * @return HasOne
  145. */
  146. public function discordUser()
  147. {
  148. return $this->hasOne(DiscordUser::class);
  149. }
  150. public function sendEmailVerificationNotification()
  151. {
  152. $this->notify(new QueuedVerifyEmail);
  153. }
  154. /**
  155. * @return string
  156. */
  157. public function credits()
  158. {
  159. return number_format($this->credits, 2, '.', '');
  160. }
  161. /**
  162. * @return bool
  163. */
  164. public function isSuspended()
  165. {
  166. return $this->suspended;
  167. }
  168. public function suspend()
  169. {
  170. foreach ($this->servers as $server) {
  171. $server->suspend();
  172. }
  173. $this->update([
  174. 'suspended' => true,
  175. ]);
  176. return $this;
  177. }
  178. public function unSuspend()
  179. {
  180. foreach ($this->getServersWithProduct() as $server) {
  181. if ($this->credits >= $server->product->getHourlyPrice()) {
  182. $server->unSuspend();
  183. }
  184. }
  185. $this->update([
  186. 'suspended' => false,
  187. ]);
  188. return $this;
  189. }
  190. private function getServersWithProduct()
  191. {
  192. return $this->servers()
  193. ->with('product')
  194. ->get();
  195. }
  196. /**
  197. * @return string
  198. */
  199. public function getAvatar()
  200. {
  201. return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email)));
  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. /**
  212. * @return array|string|string[]
  213. */
  214. public function getVerifiedStatus()
  215. {
  216. $status = '';
  217. if ($this->hasVerifiedEmail()) {
  218. $status .= 'email ';
  219. }
  220. if ($this->discordUser()->exists()) {
  221. $status .= 'discord';
  222. }
  223. $status = str_replace(' ', '/', $status);
  224. return $status;
  225. }
  226. public function verifyEmail()
  227. {
  228. $this->forceFill([
  229. 'email_verified_at' => now(),
  230. ])->save();
  231. }
  232. public function reVerifyEmail()
  233. {
  234. $this->forceFill([
  235. 'email_verified_at' => null,
  236. ])->save();
  237. }
  238. public function getActivitylogOptions(): LogOptions
  239. {
  240. return LogOptions::defaults()
  241. ->logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
  242. ->logOnlyDirty()
  243. ->dontSubmitEmptyLogs();
  244. }
  245. }