RegisterController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. '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. $val = str_replace(['-', '_'], '', $value);
  59. if(!ctype_alnum($val)) {
  60. return $fail($attribute . ' is invalid. Username must be alpha-numeric.');
  61. }
  62. },
  63. ];
  64. $rules = [
  65. 'name' => 'required|string|max:'.config('pixelfed.max_name_length'),
  66. 'username' => $usernameRules,
  67. 'email' => 'required|string|email|max:255|unique:users',
  68. 'password' => 'required|string|min:6|confirmed',
  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. $count = User::count();
  103. $limit = config('pixelfed.max_users');
  104. if($limit && $limit <= $count) {
  105. $view = 'site.closed-registration';
  106. } else {
  107. $view = config('pixelfed.open_registration') == true ? 'auth.register' : 'site.closed-registration';
  108. }
  109. return view($view);
  110. }
  111. /**
  112. * Handle a registration request for the application.
  113. *
  114. * @param \Illuminate\Http\Request $request
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function register(Request $request)
  118. {
  119. $count = User::count();
  120. $limit = config('pixelfed.max_users');
  121. if(false == config('pixelfed.open_registration') || $limit && $limit <= $count) {
  122. return abort(403);
  123. }
  124. $this->validator($request->all())->validate();
  125. event(new Registered($user = $this->create($request->all())));
  126. $this->guard()->login($user);
  127. return $this->registered($request, $user)
  128. ?: redirect($this->redirectPath());
  129. }
  130. }