RegisterController.php 6.8 KB

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