RegisterController.php 5.7 KB

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