RegisterController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. return Validator::make($data, [
  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. }
  54. /**
  55. * Create a new user instance after a valid registration.
  56. *
  57. * @param array $data
  58. * @return \App\User
  59. */
  60. protected function create(array $data)
  61. {
  62. return User::create([
  63. 'name' => $data['name'],
  64. 'username' => $data['username'],
  65. 'email' => $data['email'],
  66. 'password' => Hash::make($data['password']),
  67. ]);
  68. }
  69. public function validateUsername($username)
  70. {
  71. $restricted = RestrictedNames::get();
  72. if(in_array($username, $restricted)) {
  73. return abort(403);
  74. }
  75. }
  76. public function openRegistrationCheck()
  77. {
  78. $openRegistration = config('pixelfed.open_registration');
  79. if(false == $openRegistration) {
  80. abort(403);
  81. }
  82. }
  83. }