RegisterController.php 5.8 KB

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