RegisterController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\User;
  4. use App\Util\Lexer\RestrictedNames;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\Hash;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Foundation\Auth\RegistersUsers;
  9. class RegisterController extends Controller
  10. {
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Register Controller
  14. |--------------------------------------------------------------------------
  15. |
  16. | This controller handles the registration of new users as well as their
  17. | validation and creation. By default this controller uses a trait to
  18. | provide this functionality without requiring any additional code.
  19. |
  20. */
  21. use RegistersUsers;
  22. /**
  23. * Where to redirect users after registration.
  24. *
  25. * @var string
  26. */
  27. protected $redirectTo = '/home';
  28. /**
  29. * Create a new controller instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. $this->middleware('guest');
  36. $this->openRegistrationCheck();
  37. }
  38. /**
  39. * Get a validator for an incoming registration request.
  40. *
  41. * @param array $data
  42. * @return \Illuminate\Contracts\Validation\Validator
  43. */
  44. protected function validator(array $data)
  45. {
  46. $this->validateUsername($data['username']);
  47. $rules = [
  48. 'name' => 'required|string|max:255',
  49. 'username' => 'required|alpha_dash|min:2|max:15|unique:users',
  50. 'email' => 'required|string|email|max:255|unique:users',
  51. 'password' => 'required|string|min:6|confirmed',
  52. ];
  53. if(config('pixelfed.recaptcha')) {
  54. $rules['g-recaptcha-response'] = 'required|recaptcha';
  55. }
  56. return Validator::make($data, $rules);
  57. }
  58. /**
  59. * Create a new user instance after a valid registration.
  60. *
  61. * @param array $data
  62. * @return \App\User
  63. */
  64. protected function create(array $data)
  65. {
  66. return User::create([
  67. 'name' => $data['name'],
  68. 'username' => $data['username'],
  69. 'email' => $data['email'],
  70. 'password' => Hash::make($data['password']),
  71. ]);
  72. }
  73. public function validateUsername($username)
  74. {
  75. $restricted = RestrictedNames::get();
  76. if(in_array($username, $restricted)) {
  77. return abort(403);
  78. }
  79. }
  80. public function openRegistrationCheck()
  81. {
  82. $openRegistration = config('pixelfed.open_registration');
  83. if(false == $openRegistration) {
  84. abort(403);
  85. }
  86. }
  87. }