RegisterController.php 5.7 KB

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