您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

116 行
3.4KB

  1. <?php
  2. namespace App\Sessions;
  3. use App\Codes\UserRole;
  4. use App\Exceptions\AppCommonException;
  5. use App\Features\InstanceAble;
  6. use App\Models\HtpmsCustomer\HtpmsCustomerConnectionSwitch;
  7. use App\Models\User;
  8. use Illuminate\Auth\AuthenticationException;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Session;
  11. use LogicException;
  12. class SessionUser
  13. {
  14. use InstanceAble;
  15. private const KEY_成り代わりログインユーザーID = "KEY_成り代わりログインユーザーID";
  16. private User|null $user;
  17. private bool $isSwtiched = false;
  18. public function __construct()
  19. {
  20. // 認証していない場合はスキップ
  21. $this->user = Auth::user();
  22. if ($this->user === null) {
  23. return;
  24. }
  25. $userId = Session::get($this->getStoreKey(self::KEY_成り代わりログインユーザーID));
  26. if ($userId === null) {
  27. return;
  28. }
  29. $user = User::find($userId);
  30. if ($user) {
  31. $this->user = $user;
  32. if ($user->customer_id) {
  33. HtpmsCustomerConnectionSwitch::switch($user->customer_id);
  34. $this->isSwtiched = true;
  35. }
  36. } else {
  37. logger("無効な成り代わり 破棄");
  38. $this->switchEnd();
  39. }
  40. }
  41. public function switch(User $targetUser): void
  42. {
  43. $user = Auth::user();
  44. if ($user === null) throw new AuthenticationException();
  45. // 成り代わりできるかパターンチェック
  46. if ($user->role === UserRole::ADMIN) {
  47. if (in_array($targetUser->role, [UserRole::CUSTOMER, UserRole::SHOP], true) === false) {
  48. throw new LogicException("不適切な成り代わり");
  49. }
  50. } else if ($user->role === UserRole::CUSTOMER) {
  51. if (in_array($targetUser->role, [UserRole::SHOP], true) === false) {
  52. throw new LogicException("不適切な成り代わり");
  53. }
  54. } else {
  55. throw new LogicException("不適切な成り代わり");
  56. }
  57. // 顧客IDチェック
  58. if ($targetUser->customer_id === null) {
  59. throw new AppCommonException("顧客IDがnullのため成り代わり不可");
  60. }
  61. Session::put($this->getStoreKey(self::KEY_成り代わりログインユーザーID), $targetUser->id);
  62. HtpmsCustomerConnectionSwitch::switch($targetUser->customer_id);
  63. $this->isSwtiched = true;
  64. }
  65. public function switchEnd()
  66. {
  67. $this->isSwtiched = false;
  68. Session::remove($this->getStoreKey(self::KEY_成り代わりログインユーザーID));
  69. }
  70. public function user(): ?User
  71. {
  72. return $this->user ?? Auth::user();
  73. }
  74. public function isSwtiched(): bool
  75. {
  76. return $this->isSwtiched;
  77. }
  78. public function shopId(): string
  79. {
  80. if ($this->user === null) throw new LogicException();
  81. if ($this->user->shop_id === null) throw new LogicException();
  82. return $this->user->shop_id;
  83. }
  84. public function customerCode(): string
  85. {
  86. if ($this->user === null) throw new LogicException();
  87. if ($this->user->customer_code === null) throw new LogicException();
  88. return $this->user->customer_code;
  89. }
  90. private function getStoreKey(string $key): string
  91. {
  92. return sprintf("%s-%s", self::class, $key);
  93. }
  94. }