User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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\Casts\Attribute;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. use Illuminate\Database\Eloquent\Relations\HasOne;
  13. use Illuminate\Foundation\Auth\User as Authenticatable;
  14. use Illuminate\Notifications\Notifiable;
  15. use Illuminate\Support\Facades\DB;
  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. 'email_verified_reward',
  64. 'use_totp',
  65. 'totp_secret',
  66. 'totp_authenticated_at',
  67. ];
  68. /**
  69. * The attributes that should be hidden for arrays.
  70. *
  71. * @var array
  72. */
  73. protected $hidden = [
  74. 'password',
  75. 'remember_token',
  76. 'totp_secret',
  77. 'totp_authenticated_at'
  78. ];
  79. /**
  80. * The attributes that should be cast to native types.
  81. *
  82. * @var array
  83. */
  84. protected $casts = [
  85. 'email_verified_at' => 'datetime',
  86. 'last_seen' => 'datetime',
  87. 'credits' => 'float',
  88. 'server_limit' => 'float',
  89. 'email_verified_reward' => 'boolean',
  90. 'use_totp' => 'boolean',
  91. 'totp_secret' => 'nullable|string'
  92. ];
  93. public function __construct()
  94. {
  95. parent::__construct();
  96. $ptero_settings = new PterodactylSettings();
  97. $this->pterodactyl = new PterodactylClient($ptero_settings);
  98. }
  99. public static function boot()
  100. {
  101. parent::boot();
  102. static::created(function (User $user) {
  103. $user->notify(new WelcomeMessage($user));
  104. });
  105. static::deleting(function (User $user) {
  106. // delete every server the user owns without using chunks
  107. $user->servers()->each(function ($server) {
  108. $server->delete();
  109. });
  110. $user->payments()->delete();
  111. $user->tickets()->delete();
  112. $user->ticketBlackList()->delete();
  113. $user->vouchers()->detach();
  114. $user->discordUser()->delete();
  115. $user->pterodactyl->application->delete("/application/users/{$user->pterodactyl_id}");
  116. });
  117. }
  118. /**
  119. * @return HasMany
  120. */
  121. public function servers()
  122. {
  123. return $this->hasMany(Server::class);
  124. }
  125. /**
  126. * @return HasMany
  127. */
  128. public function payments()
  129. {
  130. return $this->hasMany(Payment::class);
  131. }
  132. /**
  133. * @return HasMany
  134. */
  135. public function tickets()
  136. {
  137. return $this->hasMany(Ticket::class);
  138. }
  139. /**
  140. * @return HasMany
  141. */
  142. public function ticketBlackList()
  143. {
  144. return $this->hasMany(TicketBlacklist::class);
  145. }
  146. /**
  147. * @return BelongsToMany
  148. */
  149. public function vouchers()
  150. {
  151. return $this->belongsToMany(Voucher::class);
  152. }
  153. /**
  154. * @return BelongsToMany
  155. */
  156. public function coupons()
  157. {
  158. return $this->belongsToMany(Coupon::class, 'user_coupons');
  159. }
  160. /**
  161. * @return HasOne
  162. */
  163. public function discordUser()
  164. {
  165. return $this->hasOne(DiscordUser::class);
  166. }
  167. public function sendEmailVerificationNotification()
  168. {
  169. $this->notify(new QueuedVerifyEmail);
  170. }
  171. /**
  172. * @return string
  173. */
  174. public function credits()
  175. {
  176. return number_format($this->credits, 2, '.', '');
  177. }
  178. /**
  179. * @return bool
  180. */
  181. public function isSuspended()
  182. {
  183. return $this->suspended;
  184. }
  185. public function suspend()
  186. {
  187. foreach ($this->servers as $server) {
  188. $server->suspend();
  189. }
  190. $this->update([
  191. 'suspended' => true,
  192. ]);
  193. return $this;
  194. }
  195. public function unSuspend()
  196. {
  197. foreach ($this->getServersWithProduct() as $server) {
  198. if ($this->credits >= $server->product->getHourlyPrice()) {
  199. $server->unSuspend();
  200. }
  201. }
  202. $this->update([
  203. 'suspended' => false,
  204. ]);
  205. return $this;
  206. }
  207. /**
  208. * @return string
  209. */
  210. public function getAvatar()
  211. {
  212. return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email)));
  213. }
  214. public function creditUsage()
  215. {
  216. $usage = 0;
  217. foreach ($this->getServersWithProduct() as $server) {
  218. $usage += $server->product->getHourlyPrice() * 24 * 30;
  219. }
  220. return number_format($usage, 2, '.', '');
  221. }
  222. private function getServersWithProduct()
  223. {
  224. return $this->servers()
  225. ->whereNull('suspended')
  226. ->whereNull('canceled')
  227. ->with('product')
  228. ->get();
  229. }
  230. /**
  231. * @return array|string|string[]
  232. */
  233. public function getVerifiedStatus()
  234. {
  235. $status = '';
  236. if ($this->hasVerifiedEmail()) {
  237. $status .= 'email ';
  238. }
  239. if ($this->discordUser()->exists()) {
  240. $status .= 'discord';
  241. }
  242. $status = str_replace(' ', '/', $status);
  243. return $status;
  244. }
  245. public function verifyEmail()
  246. {
  247. $this->forceFill([
  248. 'email_verified_at' => now()
  249. ])->save();
  250. }
  251. public function reVerifyEmail()
  252. {
  253. $this->forceFill([
  254. 'email_verified_at' => null
  255. ])->save();
  256. }
  257. public function referredBy()
  258. {
  259. $referee = DB::table('user_referrals')->where("registered_user_id", $this->id)->first();
  260. if ($referee) {
  261. $referee = User::where("id", $referee->referral_id)->firstOrFail();
  262. return $referee;
  263. }
  264. return Null;
  265. }
  266. public function getActivitylogOptions(): LogOptions
  267. {
  268. return LogOptions::defaults()
  269. ->logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
  270. ->logOnlyDirty()
  271. ->dontSubmitEmptyLogs();
  272. }
  273. public function recoveryTokens(): HasMany
  274. {
  275. return $this->hasMany(RecoveryToken::class);
  276. }
  277. /**
  278. * Interact with the 2fa secret attribute.
  279. *
  280. * @param string $value
  281. * @return \Illuminate\Database\Eloquent\Casts\Attribute
  282. */
  283. protected function google2faSecret(): Attribute
  284. {
  285. return new Attribute(
  286. get: fn ($value) => decrypt($value),
  287. set: fn ($value) => encrypt($value),
  288. );
  289. }
  290. }