RegisterController.php 5.6 KB

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