User.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace App\Models;
  3. use App\Notifications\Auth\QueuedVerifyEmail;
  4. use App\Notifications\WelcomeMessage;
  5. use App\Classes\PterodactylClient;
  6. use App\Settings\PterodactylSettings;
  7. use Illuminate\Contracts\Auth\MustVerifyEmail;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Database\Eloquent\Relations\HasOne;
  12. use Illuminate\Foundation\Auth\User as Authenticatable;
  13. use Illuminate\Notifications\Notifiable;
  14. use Illuminate\Support\Facades\DB;
  15. use Spatie\Activitylog\LogOptions;
  16. use Spatie\Activitylog\Traits\CausesActivity;
  17. use Spatie\Activitylog\Traits\LogsActivity;
  18. use Spatie\Permission\Traits\HasRoles;
  19. /**
  20. * Class User
  21. */
  22. class User extends Authenticatable implements MustVerifyEmail
  23. {
  24. use HasFactory, Notifiable, LogsActivity, CausesActivity, HasRoles;
  25. private PterodactylClient $pterodactyl;
  26. /**
  27. * @var string[]
  28. */
  29. protected static $logAttributes = ['name', 'email'];
  30. /**
  31. * @var string[]
  32. */
  33. protected static $ignoreChangedAttributes = [
  34. 'remember_token',
  35. 'credits',
  36. 'updated_at',
  37. 'server_limit',
  38. 'last_seen',
  39. 'ip',
  40. 'pterodactyl_id',
  41. ];
  42. /**
  43. * The attributes that are mass assignable.
  44. *
  45. * @var array
  46. */
  47. protected $fillable = [
  48. 'name',
  49. 'ip',
  50. 'mac',
  51. 'last_seen',
  52. 'role',
  53. 'credits',
  54. 'email',
  55. 'server_limit',
  56. 'password',
  57. 'pterodactyl_id',
  58. 'discord_verified_at',
  59. 'avatar',
  60. 'suspended',
  61. 'referral_code',
  62. 'email_verified_reward',
  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. 'email_verified_reward' => 'boolean'
  84. ];
  85. public function __construct()
  86. {
  87. parent::__construct();
  88. $ptero_settings = new PterodactylSettings();
  89. $this->pterodactyl = new PterodactylClient($ptero_settings);
  90. }
  91. public static function boot()
  92. {
  93. parent::boot();
  94. static::created(function (User $user) {
  95. $user->notify(new WelcomeMessage($user));
  96. });
  97. static::deleting(function (User $user) {
  98. // delete every server the user owns without using chunks
  99. $user->servers()->each(function ($server) {
  100. $server->delete();
  101. });
  102. $user->payments()->delete();
  103. $user->tickets()->delete();
  104. $user->ticketBlackList()->delete();
  105. $user->vouchers()->detach();
  106. $user->discordUser()->delete();
  107. $user->pterodactyl->application->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 BelongsToMany
  147. */
  148. public function coupons()
  149. {
  150. return $this->belongsToMany(Coupon::class, 'user_coupons');
  151. }
  152. /**
  153. * @return HasOne
  154. */
  155. public function discordUser()
  156. {
  157. return $this->hasOne(DiscordUser::class);
  158. }
  159. public function sendEmailVerificationNotification()
  160. {
  161. $this->notify(new QueuedVerifyEmail);
  162. }
  163. /**
  164. * @return string
  165. */
  166. public function credits()
  167. {
  168. return number_format($this->credits, 2, '.', '');
  169. }
  170. /**
  171. * @return bool
  172. */
  173. public function isSuspended()
  174. {
  175. return $this->suspended;
  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. public function unSuspend()
  188. {
  189. foreach ($this->getServersWithProduct() as $server) {
  190. if ($this->credits >= $server->product->getHourlyPrice()) {
  191. $server->unSuspend();
  192. }
  193. }
  194. $this->update([
  195. 'suspended' => false,
  196. ]);
  197. return $this;
  198. }
  199. /**
  200. * @return string
  201. */
  202. public function getAvatar()
  203. {
  204. return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email)));
  205. }
  206. public function creditUsage()
  207. {
  208. $usage = 0;
  209. foreach ($this->getServersWithProduct() as $server) {
  210. $usage += $server->product->getHourlyPrice() * 24 * 30;
  211. }
  212. return number_format($usage, 2, '.', '');
  213. }
  214. private function getServersWithProduct()
  215. {
  216. return $this->servers()
  217. ->whereNull('suspended')
  218. ->whereNull('canceled')
  219. ->with('product')
  220. ->get();
  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 referredBy()
  250. {
  251. $referee = DB::table('user_referrals')->where("registered_user_id", $this->id)->first();
  252. if ($referee) {
  253. $referee = User::where("id", $referee->referral_id)->firstOrFail();
  254. return $referee;
  255. }
  256. return Null;
  257. }
  258. public function getActivitylogOptions(): LogOptions
  259. {
  260. return LogOptions::defaults()
  261. ->logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
  262. ->logOnlyDirty()
  263. ->dontSubmitEmptyLogs();
  264. }
  265. }