RegisterController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use Illuminate\Foundation\Auth\RegistersUsers;
  4. use Illuminate\Support\Facades\Validator;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Auth\Events\Registered;
  7. use Illuminate\Http\JsonResponse;
  8. use App\Http\Controllers\Controller;
  9. use App\Providers\RouteServiceProvider;
  10. use App\Models\User;
  11. use App\Models\Document;
  12. use App\Rules\AllowNickname;
  13. use App\Rules\NumberLength;
  14. use App\Rules\SpecialCharLength;
  15. use App\Rules\UppercaseLength;
  16. use App\Rules\DeniedEmail;
  17. class RegisterController extends Controller
  18. {
  19. /*
  20. |--------------------------------------------------------------------------
  21. | Register Controller
  22. |--------------------------------------------------------------------------
  23. |
  24. | This controller handles the registration of new users as well as their
  25. | validation and creation. By default this controller uses a trait to
  26. | provide this functionality without requiring any additional code.
  27. |
  28. */
  29. use RegistersUsers;
  30. /**
  31. * Where to redirect users after registration.
  32. *
  33. * @var string
  34. */
  35. protected $redirectTo = RouteServiceProvider::HOME;
  36. /**
  37. * Create a new controller instance.
  38. *
  39. * @return void
  40. */
  41. public function __construct()
  42. {
  43. $this->middleware('guest');
  44. }
  45. /**
  46. * Show the application registration form.
  47. *
  48. * @return \Illuminate\View\View
  49. */
  50. public function showRegistrationForm()
  51. {
  52. // 개인정보처리방침
  53. $privacy = (new Document)->findByCode('privacy');
  54. // 비밀번호 조건 확인
  55. $passwordMinLength = config('password_min_length');
  56. $passwordUppercaseLength = config('password_uppercase_length');
  57. $passwordNumbersLength = config('password_numbers_length');
  58. $passwordSpecialcharsLength = config('password_specialchars_length');
  59. $passwordGuideTip = "";
  60. if($passwordMinLength > 0) {
  61. $passwordGuideTip .= sprintf('최소 %d자 이상, ', $passwordMinLength);
  62. }
  63. if($passwordUppercaseLength > 0) {
  64. $passwordGuideTip .= sprintf('대문자 %d자 이상, ', $passwordUppercaseLength);
  65. }
  66. if($passwordNumbersLength > 0) {
  67. $passwordGuideTip .= sprintf('숫자 %d자 이상, ', $passwordNumbersLength);
  68. }
  69. if($passwordSpecialcharsLength > 0) {
  70. $passwordGuideTip .= sprintf('특수문자 %d자 이상, ', $passwordSpecialcharsLength);
  71. }
  72. $passwordGuideTip = rtrim($passwordGuideTip, ', ');
  73. return view('auth.register', [
  74. 'privacy' => $privacy,
  75. 'passwordGuideTip' => $passwordGuideTip
  76. ]);
  77. }
  78. /**
  79. * Get a validator for an incoming registration request.
  80. *
  81. * @param array $data
  82. * @return \Illuminate\Contracts\Validation\Validator
  83. */
  84. protected function validator(array $data)
  85. {
  86. return Validator::make($data, [
  87. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email', new DeniedEmail],
  88. 'nickname' => ['required', 'string', 'unique:users,nickname', 'min:2', 'max:20', new AllowNickname],
  89. 'password' => ['required', 'string', 'min:' . config('password_min_length', 4), 'confirmed', new NumberLength, new SpecialCharLength, new UppercaseLength],
  90. 'agree_1' => 'required|numeric|in:1',
  91. 'agree_2' => 'required|numeric|in:2'
  92. ], [], [
  93. 'email' => '이메일',
  94. 'nickname' => '닉네임',
  95. 'password' => '비밀번호',
  96. 'agree_1' => '이용약관 동의',
  97. 'agree_2' => '개인정보처리방침 동의'
  98. ]);
  99. }
  100. /**
  101. * Create a new user instance after a valid registration.
  102. *
  103. * @param array $data
  104. * @return \App\Models\User
  105. */
  106. protected function create(array $data): mixed
  107. {
  108. return (new User)->register($data);
  109. }
  110. /**
  111. * Handle a registration request for the application.
  112. *
  113. * @param \Illuminate\Http\Request $request
  114. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
  115. */
  116. public function register(Request $request)
  117. {
  118. // 회원가입 차단 확인
  119. if(config('use_register_block')) {
  120. return back()->withErrors('현재 회원 신청이 차단되어 회원가입을 할 수 없습니다. 관리자에게 문의하십시오.');
  121. }
  122. $this->validator($request->all())->validate();
  123. event(new Registered($user = $this->create($request->all())));
  124. $this->guard()->login($user);
  125. if ($response = $this->registered($request, $user)) {
  126. return $response;
  127. }
  128. return $request->wantsJson()
  129. ? new JsonResponse([], 201)
  130. : redirect($this->redirectPath());
  131. }
  132. }