選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

72 行
2.4KB

  1. <?php
  2. namespace App\Controllers;
  3. use Myth\Auth\Entities\User;
  4. use Myth\Auth\Controllers\AuthController as MythAuthController;
  5. class Auth extends MythAuthController
  6. {
  7. /**
  8. * Attempt to register a new user.
  9. */
  10. public function attemptRegister()
  11. {
  12. // Check if registration is allowed
  13. if (!$this->config->allowRegistration) {
  14. return redirect()->back()->withInput()->with('error', lang('Auth.registerDisabled'));
  15. }
  16. $users = model(UserModel::class);
  17. // Validate basics first since some password rules rely on these fields
  18. $rules = config('Validation')->registrationRules ?? [
  19. 'email' => 'required|valid_email|is_unique[users.email]',
  20. ];
  21. if (!$this->validate($rules)) {
  22. return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
  23. }
  24. // Validate passwords since they can only be validated properly here
  25. $rules = [
  26. 'password' => 'required',
  27. 'pass_confirm' => 'required|matches[password]',
  28. ];
  29. if (!$this->validate($rules)) {
  30. return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
  31. }
  32. // Save the user
  33. $allowedPostFields = array_merge(['password'], $this->config->validFields, $this->config->personalFields);
  34. $user = new User($this->request->getPost($allowedPostFields));
  35. $this->config->requireActivation === null ? $user->activate() : $user->generateActivateHash();
  36. // Ensure default group gets assigned if set
  37. if (!empty($this->config->defaultUserGroup)) {
  38. $users = $users->withGroup($this->config->defaultUserGroup);
  39. }
  40. if (!$users->save($user)) {
  41. return redirect()->back()->withInput()->with('errors', $users->errors());
  42. }
  43. if ($this->config->requireActivation !== null) {
  44. $activator = service('activator');
  45. $sent = $activator->send($user);
  46. if (!$sent) {
  47. return redirect()->back()->withInput()->with('error', $activator->error() ?? lang('Auth.unknownError'));
  48. }
  49. // Success!
  50. return redirect()->route('login')->with('message', lang('Auth.activationSuccess'));
  51. }
  52. // Success!
  53. return redirect()->route('login')->with('message', lang('Auth.registerSuccess'));
  54. }
  55. }