User.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 Illuminate\Support\Facades\DB;
  17. use Spatie\Activitylog\LogOptions;
  18. use Spatie\Activitylog\Traits\CausesActivity;
  19. use Spatie\Activitylog\Traits\LogsActivity;
  20. use Spatie\Permission\Traits\HasRoles;
  21. /**
  22. * Class User
  23. */
  24. class User extends Authenticatable implements MustVerifyEmail
  25. {
  26. use HasFactory, Notifiable, LogsActivity, CausesActivity, HasRoles;
  27. private PterodactylClient $pterodactyl;
  28. /**
  29. * @var string[]
  30. */
  31. protected static $logAttributes = ['name', 'email'];
  32. /**
  33. * @var string[]
  34. */
  35. protected static $ignoreChangedAttributes = [
  36. 'remember_token',
  37. 'credits',
  38. 'updated_at',
  39. 'server_limit',
  40. 'last_seen',
  41. 'ip',
  42. 'pterodactyl_id',
  43. ];
  44. /**
  45. * The attributes that are mass assignable.
  46. *
  47. * @var array
  48. */
  49. protected $fillable = [
  50. 'name',
  51. 'ip',
  52. 'mac',
  53. 'last_seen',
  54. 'role',
  55. 'credits',
  56. 'email',
  57. 'server_limit',
  58. 'password',
  59. 'pterodactyl_id',
  60. 'discord_verified_at',
  61. 'avatar',
  62. 'suspended',
  63. 'referral_code',
  64. ];
  65. /**
  66. * The attributes that should be hidden for arrays.
  67. *
  68. * @var array
  69. */
  70. protected $hidden = [
  71. 'password',
  72. 'remember_token',
  73. ];
  74. /**
  75. * The attributes that should be cast to native types.
  76. *
  77. * @var array
  78. */
  79. protected $casts = [
  80. 'email_verified_at' => 'datetime',
  81. 'last_seen' => 'datetime',
  82. 'credits' => 'float',
  83. 'server_limit' => 'float',
  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 HasOne
  147. */
  148. public function discordUser()
  149. {
  150. return $this->hasOne(DiscordUser::class);
  151. }
  152. public function sendEmailVerificationNotification()
  153. {
  154. $this->notify(new QueuedVerifyEmail);
  155. }
  156. /**
  157. * @return string
  158. */
  159. public function credits()
  160. {
  161. return number_format($this->credits, 2, '.', '');
  162. }
  163. /**
  164. * @return bool
  165. */
  166. public function isSuspended()
  167. {
  168. return $this->suspended;
  169. }
  170. public function suspend()
  171. {
  172. foreach ($this->servers as $server) {
  173. $server->suspend();
  174. }
  175. $this->update([
  176. 'suspended' => true,
  177. ]);
  178. return $this;
  179. }
  180. public function unSuspend()
  181. {
  182. foreach ($this->getServersWithProduct() as $server) {
  183. if ($this->credits >= $server->product->getHourlyPrice()) {
  184. $server->unSuspend();
  185. }
  186. }
  187. $this->update([
  188. 'suspended' => false,
  189. ]);
  190. return $this;
  191. }
  192. /**
  193. * @return string
  194. */
  195. public function getAvatar()
  196. {
  197. return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email)));
  198. }
  199. public function creditUsage()
  200. {
  201. $usage = 0;
  202. foreach ($this->getServersWithProduct() as $server) {
  203. $usage += $server->product->getHourlyPrice() * 24 * 30;
  204. }
  205. return number_format($usage, 2, '.', '');
  206. }
  207. private function getServersWithProduct()
  208. {
  209. return $this->servers()
  210. ->whereNull('suspended')
  211. ->whereNull('cancelled')
  212. ->with('product')
  213. ->get();
  214. }
  215. /**
  216. * @return array|string|string[]
  217. */
  218. public function getVerifiedStatus()
  219. {
  220. $status = '';
  221. if ($this->hasVerifiedEmail()) {
  222. $status .= 'email ';
  223. }
  224. if ($this->discordUser()->exists()) {
  225. $status .= 'discord';
  226. }
  227. $status = str_replace(' ', '/', $status);
  228. return $status;
  229. }
  230. public function verifyEmail()
  231. {
  232. $this->forceFill([
  233. 'email_verified_at' => now(),
  234. ])->save();
  235. }
  236. public function reVerifyEmail()
  237. {
  238. $this->forceFill([
  239. 'email_verified_at' => null,
  240. ])->save();
  241. }
  242. public function referredBy(){
  243. $referee = DB::table('user_referrals')->where("registered_user_id",$this->id)->first();
  244. if($referee){
  245. $referee = User::where("id",$referee->referral_id)->firstOrFail();
  246. return $referee;
  247. }
  248. return Null;
  249. }
  250. public function getActivitylogOptions(): LogOptions
  251. {
  252. return LogOptions::defaults()
  253. ->logOnly(['role', 'name', 'server_limit', 'pterodactyl_id', 'email'])
  254. ->logOnlyDirty()
  255. ->dontSubmitEmptyLogs();
  256. }
  257. }