RegisterController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. use App\Services\EmailService;
  12. class RegisterController extends Controller
  13. {
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Register Controller
  17. |--------------------------------------------------------------------------
  18. |
  19. | This controller handles the registration of new users as well as their
  20. | validation and creation. By default this controller uses a trait to
  21. | provide this functionality without requiring any additional code.
  22. |
  23. */
  24. use RegistersUsers;
  25. /**
  26. * Where to redirect users after registration.
  27. *
  28. * @var string
  29. */
  30. protected $redirectTo = '/';
  31. /**
  32. * Create a new controller instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. $this->middleware('guest');
  39. }
  40. /**
  41. * Get a validator for an incoming registration request.
  42. *
  43. * @param array $data
  44. *
  45. * @return \Illuminate\Contracts\Validation\Validator
  46. */
  47. protected function validator(array $data)
  48. {
  49. $this->validateUsername($data['username']);
  50. $this->validateEmail($data['email']);
  51. $usernameRules = [
  52. 'required',
  53. 'min:2',
  54. 'max:15',
  55. 'unique:users',
  56. function ($attribute, $value, $fail) {
  57. if (!ctype_alpha($value[0])) {
  58. return $fail($attribute.' is invalid. Username must be alpha-numeric and start with a letter.');
  59. }
  60. $val = str_replace(['-', '_'], '', $value);
  61. if(!ctype_alnum($val)) {
  62. return $fail($attribute . ' is invalid. Username must be alpha-numeric.');
  63. }
  64. },
  65. ];
  66. $rules = [
  67. 'name' => 'required|string|max:'.config('pixelfed.max_name_length'),
  68. 'username' => $usernameRules,
  69. 'email' => 'required|string|email|max:255|unique:users',
  70. 'password' => 'required|string|min:8|confirmed',
  71. ];
  72. return Validator::make($data, $rules);
  73. }
  74. /**
  75. * Create a new user instance after a valid registration.
  76. *
  77. * @param array $data
  78. *
  79. * @return \App\User
  80. */
  81. protected function create(array $data)
  82. {
  83. return User::create([
  84. 'name' => $data['name'],
  85. 'username' => $data['username'],
  86. 'email' => $data['email'],
  87. 'password' => Hash::make($data['password']),
  88. ]);
  89. }
  90. public function validateUsername($username)
  91. {
  92. $restricted = RestrictedNames::get();
  93. if (in_array($username, $restricted)) {
  94. return abort(403);
  95. }
  96. }
  97. public function validateEmail($email)
  98. {
  99. $banned = EmailService::isBanned($email);
  100. if($banned) {
  101. return abort(403, 'Invalid email.');
  102. }
  103. }
  104. /**
  105. * Show the application registration form.
  106. *
  107. * @return \Illuminate\Http\Response
  108. */
  109. public function showRegistrationForm()
  110. {
  111. if(config('pixelfed.open_registration')) {
  112. $limit = config('pixelfed.max_users');
  113. if($limit) {
  114. abort_if($limit <= User::count(), 404);
  115. return view('auth.register');
  116. } else {
  117. return view('auth.register');
  118. }
  119. } else {
  120. abort(404);
  121. }
  122. }
  123. /**
  124. * Handle a registration request for the application.
  125. *
  126. * @param \Illuminate\Http\Request $request
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function register(Request $request)
  130. {
  131. $count = User::count();
  132. $limit = config('pixelfed.max_users');
  133. if(false == config('pixelfed.open_registration') || $limit && $limit <= $count) {
  134. return abort(403);
  135. }
  136. $this->validator($request->all())->validate();
  137. event(new Registered($user = $this->create($request->all())));
  138. $this->guard()->login($user);
  139. return $this->registered($request, $user)
  140. ?: redirect($this->redirectPath());
  141. }
  142. }