RegisterController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. }
  37. /**
  38. * Get a validator for an incoming registration request.
  39. *
  40. * @param array $data
  41. * @return \Illuminate\Contracts\Validation\Validator
  42. */
  43. protected function validator(array $data)
  44. {
  45. $this->validateUsername($data['username']);
  46. return Validator::make($data, [
  47. 'name' => 'required|string|max:255',
  48. 'username' => 'required|alpha_dash|min:2|max:15|unique:users',
  49. 'email' => 'required|string|email|max:255|unique:users',
  50. 'password' => 'required|string|min:6|confirmed',
  51. ]);
  52. }
  53. /**
  54. * Create a new user instance after a valid registration.
  55. *
  56. * @param array $data
  57. * @return \App\User
  58. */
  59. protected function create(array $data)
  60. {
  61. return User::create([
  62. 'name' => $data['name'],
  63. 'username' => $data['username'],
  64. 'email' => $data['email'],
  65. 'password' => Hash::make($data['password']),
  66. ]);
  67. }
  68. public function validateUsername($username)
  69. {
  70. $restricted = RestrictedNames::get();
  71. if(in_array($username, $restricted)) {
  72. return abort(403);
  73. }
  74. }
  75. }