You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.3KB

  1. <?php
  2. namespace App\Logic;
  3. use App\Email\Guests\PasswordSettingStart;
  4. use App\Models\PasswordSettingToken;
  5. use App\Models\User;
  6. use App\Util\DateUtil;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Str;
  9. class PasswordSettingManager
  10. {
  11. public function __construct()
  12. {
  13. }
  14. public function generate(User $user)
  15. {
  16. // トークン生成
  17. $model = PasswordSettingToken::whereUserId($user->id)->firstOrNew();
  18. $model->user_id = $user->id;
  19. $model->token = Str::uuid();
  20. $model->expires_at = DateUtil::now()->addHours(24);
  21. $model->save();
  22. // メール送信
  23. $email = (new PasswordSettingStart($model))
  24. ->setEmail($user->email);
  25. $emailManager = new EmailManager($email);
  26. $emailManager->confirm();
  27. }
  28. /**
  29. * @param string $token
  30. * @return Collection<int, User>|null
  31. */
  32. public function verify(string $token): Collection|null
  33. {
  34. $passwords =
  35. PasswordSettingToken::whereToken($token)
  36. ->expiresIn()
  37. ->get();
  38. $users = User::whereIn(
  39. User::COL_NAME_ID,
  40. $passwords->pluck(PasswordSettingToken::COL_NAME_USER_ID)
  41. )
  42. ->get();
  43. foreach ($passwords as $password) {
  44. $password->delete();
  45. }
  46. return $users;
  47. }
  48. }