RegisterController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\User;
  5. use App\Util\Lexer\RestrictedNames;
  6. use Illuminate\Foundation\Auth\RegistersUsers;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Validator;
  9. use Illuminate\Auth\Events\Registered;
  10. use Illuminate\Http\Request;
  11. class RegisterController extends Controller
  12. {
  13. /*
  14. |--------------------------------------------------------------------------
  15. | Register Controller
  16. |--------------------------------------------------------------------------
  17. |
  18. | This controller handles the registration of new users as well as their
  19. | validation and creation. By default this controller uses a trait to
  20. | provide this functionality without requiring any additional code.
  21. |
  22. */
  23. use RegistersUsers;
  24. /**
  25. * Where to redirect users after registration.
  26. *
  27. * @var string
  28. */
  29. protected $redirectTo = '/';
  30. /**
  31. * Create a new controller instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. $this->middleware('guest');
  38. }
  39. /**
  40. * Get a validator for an incoming registration request.
  41. *
  42. * @param array $data
  43. *
  44. * @return \Illuminate\Contracts\Validation\Validator
  45. */
  46. protected function validator(array $data)
  47. {
  48. $this->validateUsername($data['username']);
  49. $usernameRules = [
  50. 'required',
  51. 'alpha_dash',
  52. 'min:2',
  53. 'max:15',
  54. 'unique:users',
  55. function ($attribute, $value, $fail) {
  56. if (!ctype_alpha($value[0])) {
  57. return $fail($attribute.' is invalid. Username must be alpha-numeric and start with a letter.');
  58. }
  59. },
  60. ];
  61. $rules = [
  62. 'name' => 'required|string|max:'.config('pixelfed.max_name_length'),
  63. 'username' => $usernameRules,
  64. 'email' => 'required|string|email|max:255|unique:users',
  65. 'password' => 'required|string|min:6|confirmed',
  66. ];
  67. if (config('pixelfed.recaptcha')) {
  68. $rules['g-recaptcha-response'] = 'required|recaptcha';
  69. }
  70. return Validator::make($data, $rules);
  71. }
  72. /**
  73. * Create a new user instance after a valid registration.
  74. *
  75. * @param array $data
  76. *
  77. * @return \App\User
  78. */
  79. protected function create(array $data)
  80. {
  81. return User::create([
  82. 'name' => $data['name'],
  83. 'username' => $data['username'],
  84. 'email' => $data['email'],
  85. 'password' => Hash::make($data['password']),
  86. ]);
  87. }
  88. public function validateUsername($username)
  89. {
  90. $restricted = RestrictedNames::get();
  91. if (in_array($username, $restricted)) {
  92. return abort(403);
  93. }
  94. }
  95. /**
  96. * Show the application registration form.
  97. *
  98. * @return \Illuminate\Http\Response
  99. */
  100. public function showRegistrationForm()
  101. {
  102. $view = config('pixelfed.open_registration') == true ? 'auth.register' : 'site.closed-registration';
  103. return view($view);
  104. }
  105. /**
  106. * Handle a registration request for the application.
  107. *
  108. * @param \Illuminate\Http\Request $request
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function register(Request $request)
  112. {
  113. if(false == config('pixelfed.open_registration')) {
  114. return abort(403);
  115. }
  116. $this->validator($request->all())->validate();
  117. event(new Registered($user = $this->create($request->all())));
  118. $this->guard()->login($user);
  119. return $this->registered($request, $user)
  120. ?: redirect($this->redirectPath());
  121. }
  122. }