RegisterController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Classes\Pterodactyl;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Configuration;
  6. use App\Models\User;
  7. use App\Providers\RouteServiceProvider;
  8. use Illuminate\Foundation\Auth\RegistersUsers;
  9. use Illuminate\Support\Facades\App;
  10. use Illuminate\Support\Facades\Hash;
  11. use Illuminate\Support\Facades\Validator;
  12. use Illuminate\Support\Str;
  13. use Illuminate\Validation\ValidationException;
  14. class RegisterController extends Controller
  15. {
  16. /*
  17. |--------------------------------------------------------------------------
  18. | Register Controller
  19. |--------------------------------------------------------------------------
  20. |
  21. | This controller handles the registration of new users as well as their
  22. | validation and creation. By default this controller uses a trait to
  23. | provide this functionality without requiring any additional code.
  24. |
  25. */
  26. use RegistersUsers;
  27. /**
  28. * Where to redirect users after registration.
  29. *
  30. * @var string
  31. */
  32. protected $redirectTo = RouteServiceProvider::HOME;
  33. /**
  34. * Create a new controller instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct()
  39. {
  40. $this->middleware('guest');
  41. }
  42. /**
  43. * Get a validator for an incoming registration request.
  44. *
  45. * @param array $data
  46. * @return \Illuminate\Contracts\Validation\Validator
  47. */
  48. protected function validator(array $data)
  49. {
  50. if (Configuration::getValueByKey('REGISTER_IP_CHECK', 'true') == 'true') {
  51. //check if ip has already made an account
  52. $data['ip'] = session()->get('ip') ?? request()->ip();
  53. if (User::where('ip', '=', request()->ip())->exists()) session()->put('ip', request()->ip());
  54. return Validator::make($data, [
  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. 'g-recaptcha-response' => ['recaptcha'],
  59. 'ip' => ['unique:users'],
  60. ], [
  61. 'ip.unique' => __("You have already made an account with us! Please contact support if you think this is incorrect.")
  62. ]);
  63. }
  64. return Validator::make($data, [
  65. 'name' => ['required', 'string', 'max:30', 'min:4', 'alpha_num', 'unique:users'],
  66. 'email' => ['required', 'string', 'email', 'max:64', 'unique:users'],
  67. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  68. 'g-recaptcha-response' => ['recaptcha'],
  69. ]);
  70. }
  71. /**
  72. * Create a new user instance after a valid registration.
  73. *
  74. * @param array $data
  75. * @return User
  76. */
  77. protected function create(array $data)
  78. {
  79. $user = User::create([
  80. 'name' => $data['name'],
  81. 'email' => $data['email'],
  82. 'credits' => Configuration::getValueByKey('INITIAL_CREDITS', 150),
  83. 'server_limit' => Configuration::getValueByKey('INITIAL_SERVER_LIMIT', 1),
  84. 'password' => Hash::make($data['password']),
  85. ]);
  86. $response = Pterodactyl::client()->post('/application/users', [
  87. "external_id" => App::environment('local') ? Str::random(16) : (string)$user->id,
  88. "username" => $user->name,
  89. "email" => $user->email,
  90. "first_name" => $user->name,
  91. "last_name" => $user->name,
  92. "password" => $data['password'],
  93. "root_admin" => false,
  94. "language" => "en"
  95. ]);
  96. if ($response->failed()) {
  97. $user->delete();
  98. throw ValidationException::withMessages([
  99. 'ptero_registration_error' => [__('Account already exists on Pterodactyl. Please contact the Support!')],
  100. ]);
  101. }
  102. $user->update([
  103. 'pterodactyl_id' => $response->json()['attributes']['id']
  104. ]);
  105. return $user;
  106. }
  107. }