RegisterController.php 4.0 KB

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