Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

132 rindas
3.8KB

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