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.

141 line
4.2KB

  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 Auth;
  9. use Illuminate\Auth\AuthenticationException;
  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. private bool $isInit = false;
  19. public function init(array $param = [])
  20. {
  21. if ($this->isInit === true) {
  22. return;
  23. }
  24. $this->initSessionUser($param);
  25. $this->isInit = true;
  26. }
  27. private function initSessionUser(array $param)
  28. {
  29. // 認証していない場合はスキップ
  30. $this->user = Auth::user();
  31. if ($this->user instanceof User && $this->user->customer_id) {
  32. HtpmsCustomerConnectionSwitch::switch($this->user->customer_id);
  33. } else if (!!data_get($param, "customer_id")) {
  34. HtpmsCustomerConnectionSwitch::switch(data_get($param, "customer_id"));
  35. }
  36. $user = Auth::user();
  37. $ckeck = Auth::check();
  38. if ($this->user === null) {
  39. return;
  40. }
  41. $userId = Session::get($this->getStoreKey(self::KEY_成り代わりログインユーザーID));
  42. if ($userId === null) {
  43. return;
  44. }
  45. $user = User::find($userId);
  46. if ($user) {
  47. $this->user = $user;
  48. if ($user->customer_id) {
  49. HtpmsCustomerConnectionSwitch::switch($user->customer_id);
  50. $this->isSwtiched = true;
  51. }
  52. } else {
  53. logger("無効な成り代わり 破棄");
  54. $this->switchEnd();
  55. }
  56. }
  57. public function switch(User $targetUser): void
  58. {
  59. $user = Auth::user();
  60. if ($user === null) throw new AuthenticationException();
  61. // 成り代わりできるかパターンチェック
  62. if ($user->role === UserRole::ADMIN) {
  63. if (in_array($targetUser->role, [UserRole::CUSTOMER, UserRole::SHOP], true) === false) {
  64. throw new LogicException("不適切な成り代わり");
  65. }
  66. } else if ($user->role === UserRole::CUSTOMER) {
  67. if (in_array($targetUser->role, [UserRole::SHOP], true) === false) {
  68. throw new LogicException("不適切な成り代わり");
  69. }
  70. } else {
  71. throw new LogicException("不適切な成り代わり");
  72. }
  73. // 顧客IDチェック
  74. if ($targetUser->customer_id === null) {
  75. throw new AppCommonException("顧客IDがnullのため成り代わり不可");
  76. }
  77. Session::put($this->getStoreKey(self::KEY_成り代わりログインユーザーID), $targetUser->id);
  78. HtpmsCustomerConnectionSwitch::switch($targetUser->customer_id);
  79. $this->isSwtiched = true;
  80. }
  81. public function switchEnd()
  82. {
  83. $this->isSwtiched = false;
  84. Session::remove($this->getStoreKey(self::KEY_成り代わりログインユーザーID));
  85. }
  86. public function user(): ?User
  87. {
  88. return $this->user ?? Auth::user();
  89. }
  90. public function isSwtiched(): bool
  91. {
  92. return $this->isSwtiched;
  93. }
  94. public function shopId(): string
  95. {
  96. if ($this->user === null) throw new LogicException();
  97. if ($this->user->shop_id === null) throw new LogicException();
  98. return $this->user->shop_id;
  99. }
  100. public function customerCode(): string
  101. {
  102. if ($this->user === null) throw new LogicException();
  103. if ($this->user->customer_code === null) throw new LogicException();
  104. return $this->user->customer_code;
  105. }
  106. public function customerId(): int
  107. {
  108. if ($this->user === null) throw new LogicException();
  109. if ($this->user->customer_id === null) throw new LogicException();
  110. return $this->user->customer_id;
  111. }
  112. private function getStoreKey(string $key): string
  113. {
  114. return sprintf("%s-%s", self::class, $key);
  115. }
  116. }