RegisterController.php 3.0 KB

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