RegisterController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use App\Notifications\ReferralNotification;
  6. use App\Providers\RouteServiceProvider;
  7. use App\Traits\Referral;
  8. use Carbon\Carbon;
  9. use App\Settings\PterodactylSettings;
  10. use App\Classes\PterodactylClient;
  11. use App\Settings\GeneralSettings;
  12. use App\Settings\ReferralSettings;
  13. use App\Settings\UserSettings;
  14. use App\Settings\WebsiteSettings;
  15. use Illuminate\Foundation\Auth\RegistersUsers;
  16. use Illuminate\Support\Facades\App;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Hash;
  19. use Illuminate\Support\Facades\Log;
  20. use Illuminate\Support\Facades\Validator;
  21. use Illuminate\Support\Str;
  22. use Illuminate\Validation\ValidationException;
  23. class RegisterController extends Controller
  24. {
  25. private $pterodactyl;
  26. /*
  27. |--------------------------------------------------------------------------
  28. | Register Controller
  29. |--------------------------------------------------------------------------
  30. |
  31. | This controller handles the registration of new users as well as their
  32. | validation and creation. By default this controller uses a trait to
  33. | provide this functionality without requiring any additional code.
  34. |
  35. */
  36. use RegistersUsers, Referral;
  37. /**
  38. * Where to redirect users after registration.
  39. *
  40. * @var string
  41. */
  42. protected $redirectTo = RouteServiceProvider::HOME;
  43. /**
  44. * Create a new controller instance.
  45. *
  46. * @return void
  47. */
  48. public function __construct(PterodactylSettings $ptero_settings)
  49. {
  50. $this->middleware('guest');
  51. $this->pterodactyl = new PterodactylClient($ptero_settings);
  52. }
  53. /**
  54. * Get a validator for an incoming registration request.
  55. *
  56. * @param array $data
  57. * @return \Illuminate\Contracts\Validation\Validator
  58. */
  59. protected function validator(array $data, GeneralSettings $general_settings, WebsiteSettings $website_settings, UserSettings $user_settings)
  60. {
  61. $validationRules = [
  62. 'name' => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
  63. 'email' => ['required', 'string', 'email', 'max:64', 'unique:users'],
  64. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  65. ];
  66. if ($general_settings->recaptcha_enabled) {
  67. $validationRules['g-recaptcha-response'] = ['required', 'recaptcha'];
  68. }
  69. if ($website_settings->show_tos) {
  70. $validationRules['terms'] = ['required'];
  71. }
  72. if ($user_settings->register_ip_check) {
  73. //check if ip has already made an account
  74. $data['ip'] = session()->get('ip') ?? request()->ip();
  75. if (User::where('ip', '=', request()->ip())->exists()) {
  76. session()->put('ip', request()->ip());
  77. }
  78. $validationRules['ip'] = ['unique:users'];
  79. return Validator::make($data, $validationRules, [
  80. 'ip.unique' => 'You have already made an account! Please contact support if you think this is incorrect.',
  81. ]);
  82. }
  83. return Validator::make($data, $validationRules);
  84. }
  85. /**
  86. * Create a new user instance after a valid registration.
  87. *
  88. * @param array $data
  89. * @return User
  90. */
  91. protected function create(array $data, GeneralSettings $general_settings, UserSettings $user_settings, ReferralSettings $referral_settings)
  92. {
  93. $user = User::create([
  94. 'name' => $data['name'],
  95. 'email' => $data['email'],
  96. 'credits' => $user_settings->initial_credits,
  97. 'server_limit' => $user_settings->initial_server_limit,
  98. 'password' => Hash::make($data['password']),
  99. 'referral_code' => $this->createReferralCode(),
  100. ]);
  101. $response = $this->pterodactyl->client_admin->post('/application/users', [
  102. 'external_id' => App::environment('local') ? Str::random(16) : (string) $user->id,
  103. 'username' => $user->name,
  104. 'email' => $user->email,
  105. 'first_name' => $user->name,
  106. 'last_name' => $user->name,
  107. 'password' => $data['password'],
  108. 'root_admin' => false,
  109. 'language' => 'en',
  110. ]);
  111. if ($response->failed()) {
  112. $user->delete();
  113. Log::error('Pterodactyl Registration Error: ' . $response->json()['errors'][0]['detail']);
  114. throw ValidationException::withMessages([
  115. 'ptero_registration_error' => [__('Account already exists on Pterodactyl. Please contact the Support!')],
  116. ]);
  117. }
  118. $user->update([
  119. 'pterodactyl_id' => $response->json()['attributes']['id'],
  120. ]);
  121. //INCREMENT REFERRAL-USER CREDITS
  122. if (!empty($data['referral_code'])) {
  123. $ref_code = $data['referral_code'];
  124. $new_user = $user->id;
  125. if ($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) {
  126. if ($referral_settings->mode === 'sign-up' || $referral_settings->mode === 'both') {
  127. $ref_user->increment('credits', $referral_settings->reward);
  128. $ref_user->notify(new ReferralNotification($ref_user->id, $new_user));
  129. //LOGS REFERRALS IN THE ACTIVITY LOG
  130. activity()
  131. ->performedOn($user)
  132. ->causedBy($ref_user)
  133. ->log('gained ' . $referral_settings->reward . ' ' . $general_settings->credits_display_name . ' for sign-up-referral of ' . $user->name . ' (ID:' . $user->id . ')');
  134. }
  135. //INSERT INTO USER_REFERRALS TABLE
  136. DB::table('user_referrals')->insert([
  137. 'referral_id' => $ref_user->id,
  138. 'registered_user_id' => $user->id,
  139. 'created_at' => Carbon::now(),
  140. 'updated_at' => Carbon::now(),
  141. ]);
  142. }
  143. }
  144. return $user;
  145. }
  146. }