RegisterController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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. *
  43. * @return \Illuminate\Contracts\Validation\Validator
  44. */
  45. protected function validator(array $data)
  46. {
  47. $this->validateUsername($data['username']);
  48. $usernameRules = [
  49. 'required',
  50. 'alpha_dash',
  51. 'min:2',
  52. 'max:15',
  53. 'unique:users',
  54. function ($attribute, $value, $fail) {
  55. if (!ctype_alpha($value[0])) {
  56. return $fail($attribute.' is invalid. Username must be alpha-numeric and start with a letter.');
  57. }
  58. },
  59. ];
  60. $rules = [
  61. 'name' => 'required|string|max:'.config('pixelfed.max_name_length'),
  62. 'username' => $usernameRules,
  63. 'email' => 'required|string|email|max:255|unique:users',
  64. 'password' => 'required|string|min:6|confirmed',
  65. ];
  66. if (config('pixelfed.recaptcha')) {
  67. $rules['g-recaptcha-response'] = 'required|recaptcha';
  68. }
  69. return Validator::make($data, $rules);
  70. }
  71. /**
  72. * Create a new user instance after a valid registration.
  73. *
  74. * @param array $data
  75. *
  76. * @return \App\User
  77. */
  78. protected function create(array $data)
  79. {
  80. return User::create([
  81. 'name' => $data['name'],
  82. 'username' => $data['username'],
  83. 'email' => $data['email'],
  84. 'password' => Hash::make($data['password']),
  85. ]);
  86. }
  87. public function validateUsername($username)
  88. {
  89. $restricted = RestrictedNames::get();
  90. if (in_array($username, $restricted)) {
  91. return abort(403);
  92. }
  93. }
  94. public function openRegistrationCheck()
  95. {
  96. $openRegistration = config('pixelfed.open_registration');
  97. if (false == $openRegistration) {
  98. abort(403);
  99. }
  100. }
  101. }