RegisterController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Recipient;
  5. use App\Models\User;
  6. use App\Rules\NotBlacklisted;
  7. use App\Rules\NotDeletedUsername;
  8. use App\Rules\NotLocalRecipient;
  9. use App\Rules\RegisterUniqueRecipient;
  10. use Illuminate\Foundation\Auth\RegistersUsers;
  11. use Illuminate\Support\Facades\App;
  12. use Illuminate\Support\Facades\Hash;
  13. use Illuminate\Support\Facades\Validator;
  14. use Ramsey\Uuid\Uuid;
  15. class RegisterController extends Controller
  16. {
  17. /*
  18. |--------------------------------------------------------------------------
  19. | Register Controller
  20. |--------------------------------------------------------------------------
  21. |
  22. | This controller handles the registration of new users as well as their
  23. | validation and creation. By default this controller uses a trait to
  24. | provide this functionality without requiring any additional code.
  25. |
  26. */
  27. use RegistersUsers;
  28. /**
  29. * Where to redirect users after registration.
  30. *
  31. * @var string
  32. */
  33. protected $redirectTo = '/';
  34. /**
  35. * Create a new controller instance.
  36. *
  37. * @return void
  38. */
  39. public function __construct()
  40. {
  41. $this->middleware('guest');
  42. }
  43. /**
  44. * Get a validator for an incoming registration request.
  45. *
  46. * @param array $data
  47. * @return \Illuminate\Contracts\Validation\Validator
  48. */
  49. protected function validator(array $data)
  50. {
  51. return Validator::make($data, [
  52. 'username' => [
  53. 'required',
  54. 'regex:/^[a-zA-Z0-9]*$/',
  55. 'max:20',
  56. 'unique:users,username',
  57. 'unique:additional_usernames,username',
  58. new NotBlacklisted,
  59. new NotDeletedUsername
  60. ],
  61. 'email' => [
  62. 'required',
  63. 'email:rfc,dns',
  64. 'max:254',
  65. 'confirmed',
  66. new RegisterUniqueRecipient,
  67. new NotLocalRecipient
  68. ],
  69. 'password' => ['required', 'min:8'],
  70. ], [
  71. 'captcha.captcha' => 'The text entered was incorrect, please try again.',
  72. ])
  73. ->sometimes('captcha', 'required|captcha', function () {
  74. return ! App::environment('testing');
  75. });
  76. }
  77. /**
  78. * Create a new user instance after a valid registration.
  79. *
  80. * @param array $data
  81. * @return \App\Models\User
  82. */
  83. protected function create(array $data)
  84. {
  85. $userId = Uuid::uuid4();
  86. $recipient = Recipient::create([
  87. 'email' => $data['email'],
  88. 'user_id' => $userId
  89. ]);
  90. $twoFactor = app('pragmarx.google2fa');
  91. return User::create([
  92. 'id' => $userId,
  93. 'username' => $data['username'],
  94. 'default_recipient_id' => $recipient->id,
  95. 'password' => Hash::make($data['password']),
  96. 'two_factor_secret' => $twoFactor->generateSecretKey()
  97. ]);
  98. }
  99. }