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.

128 line
3.6KB

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