RegisterController.php 3.8 KB

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