User.php 6.4 KB

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