|
- <?php
-
- namespace App\Sessions;
-
- use App\Codes\UserRole;
- use App\Exceptions\AppCommonException;
- use App\Features\InstanceAble;
- use App\Models\HtpmsCustomer\HtpmsCustomerConnectionSwitch;
- use App\Models\User;
- use Auth;
- use Illuminate\Auth\AuthenticationException;
- use Illuminate\Support\Facades\Session;
- use LogicException;
-
- class SessionUser
- {
- use InstanceAble;
-
- private const KEY_成り代わりログインユーザーID = "KEY_成り代わりログインユーザーID";
-
- private User|null $user;
- private bool $isSwtiched = false;
- private bool $isInit = false;
-
- public function init(array $param = [])
- {
- if ($this->isInit === true) {
- return;
- }
- $this->initSessionUser($param);
- $this->isInit = true;
- }
- private function initSessionUser(array $param)
- {
- // 認証していない場合はスキップ
- $this->user = Auth::user();
-
- if ($this->user instanceof User && $this->user->customer_id) {
- HtpmsCustomerConnectionSwitch::switch($this->user->customer_id);
- } else if (!!data_get($param, "customer_id")) {
- HtpmsCustomerConnectionSwitch::switch(data_get($param, "customer_id"));
- }
-
-
- $user = Auth::user();
- $ckeck = Auth::check();
- if ($this->user === null) {
- return;
- }
-
- $userId = Session::get($this->getStoreKey(self::KEY_成り代わりログインユーザーID));
-
- if ($userId === null) {
- return;
- }
-
- $user = User::find($userId);
-
- if ($user) {
- $this->user = $user;
- if ($user->customer_id) {
- HtpmsCustomerConnectionSwitch::switch($user->customer_id);
- $this->isSwtiched = true;
- }
- } else {
- logger("無効な成り代わり 破棄");
- $this->switchEnd();
- }
- }
-
- public function switch(User $targetUser): void
- {
- $user = Auth::user();
- if ($user === null) throw new AuthenticationException();
-
- // 成り代わりできるかパターンチェック
- if ($user->role === UserRole::ADMIN) {
- if (in_array($targetUser->role, [UserRole::CUSTOMER, UserRole::SHOP], true) === false) {
- throw new LogicException("不適切な成り代わり");
- }
- } else if ($user->role === UserRole::CUSTOMER) {
- if (in_array($targetUser->role, [UserRole::SHOP], true) === false) {
- throw new LogicException("不適切な成り代わり");
- }
- } else {
- throw new LogicException("不適切な成り代わり");
- }
-
- // 顧客IDチェック
- if ($targetUser->customer_id === null) {
- throw new AppCommonException("顧客IDがnullのため成り代わり不可");
- }
-
-
- Session::put($this->getStoreKey(self::KEY_成り代わりログインユーザーID), $targetUser->id);
- HtpmsCustomerConnectionSwitch::switch($targetUser->customer_id);
- $this->isSwtiched = true;
- }
-
- public function switchEnd()
- {
- $this->isSwtiched = false;
- Session::remove($this->getStoreKey(self::KEY_成り代わりログインユーザーID));
- }
-
- public function user(): ?User
- {
- return $this->user ?? Auth::user();
- }
-
- public function isSwtiched(): bool
- {
- return $this->isSwtiched;
- }
-
- public function shopId(): string
- {
- if ($this->user === null) throw new LogicException();
- if ($this->user->shop_id === null) throw new LogicException();
- return $this->user->shop_id;
- }
-
- public function customerCode(): string
- {
- if ($this->user === null) throw new LogicException();
- if ($this->user->customer_code === null) throw new LogicException();
- return $this->user->customer_code;
- }
- public function customerId(): int
- {
- if ($this->user === null) throw new LogicException();
- if ($this->user->customer_id === null) throw new LogicException();
- return $this->user->customer_id;
- }
-
- private function getStoreKey(string $key): string
- {
- return sprintf("%s-%s", self::class, $key);
- }
- }
|