From 8da03f27e8bca167245e56ffeda6c66570bc790d Mon Sep 17 00:00:00 2001 From: "sosuke.iwabuchi" Date: Tue, 27 Jun 2023 20:51:46 +0900 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E8=80=85=E3=83=A6=E3=83=BC?= =?UTF-8?q?=E3=82=B6=E4=BD=9C=E6=88=90=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/CreateAdminUser.php | 105 +++++++++++++ app/Logic/User/AdminUserManager.php | 46 ++++++ app/Logic/User/ContractAdminUserManager.php | 13 ++ app/Logic/User/LoginUserManager.php | 139 +---------------- app/Logic/User/UserManager.php | 157 ++++++++++++++++++++ 5 files changed, 324 insertions(+), 136 deletions(-) create mode 100644 app/Console/Commands/CreateAdminUser.php create mode 100644 app/Logic/User/AdminUserManager.php create mode 100644 app/Logic/User/ContractAdminUserManager.php create mode 100644 app/Logic/User/UserManager.php diff --git a/app/Console/Commands/CreateAdminUser.php b/app/Console/Commands/CreateAdminUser.php new file mode 100644 index 0000000..4b7b3e7 --- /dev/null +++ b/app/Console/Commands/CreateAdminUser.php @@ -0,0 +1,105 @@ +getParams(); + + + try { + $db->beginTransaction(); + + $manager = new AdminUserManager(); + $messages = $manager->initForCreateAdmin() + ->fill($params) + ->create(); + + if (count($messages) !== 0) { + throw new AppCommonException(Arr::first($messages)); + } + + $db->commit(); + } catch (Exception $e) { + $db->rollBack(); + throw $e; + } + + return self::RESULTCODE_SUCCESS; + } + + private function getParams(): array + { + + $email = $this->option("email"); + if (!$email) { + throw new Exception("引数不正:email"); + } + $name = $this->option("name"); + if (!$name) { + throw new Exception("引数不正:name"); + } + $password = $this->option("password"); + if (!$password) { + throw new Exception("引数不正:password"); + } + + + return [ + User::COL_NAME_NAME => $name, + User::COL_NAME_EMAIL => $email, + User::COL_NAME_PASSWORD => $password, + ]; + } +} diff --git a/app/Logic/User/AdminUserManager.php b/app/Logic/User/AdminUserManager.php new file mode 100644 index 0000000..2f9731a --- /dev/null +++ b/app/Logic/User/AdminUserManager.php @@ -0,0 +1,46 @@ +setUser(null); + $this->initialized = true; + return $this; + } + + public function initForModifyAdmin(string|User $userId) + { + $this->setUser($userId); + $this->initialized = true; + return $this; + } + + /** + * @override + */ + public function initForCreate(string|Contract $contractId) + { + throw new LogicException("不許可な関数アクセス"); + } + + /** + * @override + */ + public function initForModify(string|Contract $contractId, string|User $userId) + { + throw new LogicException("不許可な関数アクセス"); + } + + protected function role(): UserRole + { + return UserRole::SUPER_ADMIN; + } +} diff --git a/app/Logic/User/ContractAdminUserManager.php b/app/Logic/User/ContractAdminUserManager.php new file mode 100644 index 0000000..a09b199 --- /dev/null +++ b/app/Logic/User/ContractAdminUserManager.php @@ -0,0 +1,13 @@ +setContract($contractId); - $this->setUser(null); - $this->initialized = true; - return $this; - } - - public function initForModify(string|Contract $contractId, string|User $userId) - { - $this->setContract($contractId); - $this->setUser($userId); - $this->initialized = true; - return $this; - } - - public function getTimestamp(): Carbon + protected function role(): UserRole { - if (!$this->initialized) { - throw new LogicException("初期化不正"); - } - return $this->user->updated_at < $this->contract->updated_at ? $this->contract->updated_at : $this->user->updated_at; - } - - public function fill(array $attr) - { - if (!$this->initialized) { - throw new LogicException("初期化不正"); - } - $this->user->fill($attr); - return $this; - } - - public function create(): array - { - $messages = $this->checkParam(); - - if (count($messages) !== 0) { - return $messages; - } - - $this->user->save(); - return []; - } - public function update(): array - { - $messages = $this->checkParam(); - - if (count($messages) !== 0) { - return $messages; - } - - $this->user->save(); - return []; - } - - private function setContract(string|Contract $contractId) - { - if ($contractId instanceof Contract) { - $this->contract = $contractId; - $this->initialized = true; - return; - } - - $this->contract = Contract::findOrFail($contractId); - $this->initialized = true; - return; - } - - private function setUser(string|User|null $userId) - { - if ($userId instanceof User) { - $this->user = $userId; - return; - } else if (is_string($userId)) { - $this->user = User::findOrFail($userId); - return; - } - - $this->user = new User(); - $this->user->setContract($this->contract); - $this->user->role = UserRole::NORMAL_ADMIN; - } - - private function checkParam() - { - $validator = Validator::make($this->user->toArray(), []); - - if ($validator->failed()) { - throw new LogicException("バリデートエラー"); - } - - $messages = []; - - $this->checkEmailUnique($messages); - $this->passwordEncrypto($messages); - - return $messages; - } - - private function passwordEncrypto(array &$messages) - { - if ($this->user->isDirty(User::COL_NAME_PASSWORD)) { - $this->user->password = Hash::make($this->user->password); - } - } - - private function checkEmailUnique(array &$messages) - { - if ($this->user->isDirty(User::COL_NAME_EMAIL)) { - - $exists = User::whereEmail($this->user->email) - ->where(User::COL_NAME_ID, '<>', $this->user->id) - ->exists(); - - if ($exists) { - $messages[User::COL_NAME_EMAIL] = trans('validation.unique'); - } - } + return UserRole::NORMAL_ADMIN; } } diff --git a/app/Logic/User/UserManager.php b/app/Logic/User/UserManager.php new file mode 100644 index 0000000..4387dd3 --- /dev/null +++ b/app/Logic/User/UserManager.php @@ -0,0 +1,157 @@ +setContract($contractId); + $this->setUser(null); + $this->initialized = true; + return $this; + } + + public function initForModify(string|Contract $contractId, string|User $userId) + { + $this->setContract($contractId); + $this->setUser($userId); + $this->initialized = true; + return $this; + } + + public function getTimestamp(): Carbon + { + if (!$this->initialized) { + throw new LogicException("初期化不正"); + } + return $this->user->updated_at < $this->contract->updated_at ? $this->contract->updated_at : $this->user->updated_at; + } + + public function fill(array $attr) + { + if (!$this->initialized) { + throw new LogicException("初期化不正"); + } + $this->user->fill($attr); + return $this; + } + + public function create(): array + { + $messages = $this->checkParam(); + + if (count($messages) !== 0) { + return $messages; + } + + $this->user->save(); + return []; + } + public function update(): array + { + $messages = $this->checkParam(); + + if (count($messages) !== 0) { + return $messages; + } + + $this->user->save(); + return []; + } + + protected function setContract(string|Contract|null $contractId) + { + if ($contractId instanceof Contract) { + $this->contract = (new Contract())->copy($contractId); + $this->initialized = true; + return; + } else if ($contractId === null) { + $this->initialized = true; + return; + } + + $this->contract = Contract::findOrFail($contractId); + $this->initialized = true; + return; + } + + protected function setUser(string|User|null $userId) + { + if ($userId instanceof User) { + $this->user = (new User())->copy($userId); + return; + } else if (is_string($userId)) { + $this->user = User::findOrFail($userId); + return; + } + + $this->user = new User(); + if ($this->contract) { + $this->user->setContract($this->contract); + } + $this->user->role = $this->role(); + } + + protected function checkParam() + { + $validator = Validator::make($this->user->toArray(), []); + + if ($validator->failed()) { + throw new LogicException("バリデートエラー"); + } + + $messages = []; + + $this->checkContract($messages); + $this->checkEmailUnique($messages); + $this->passwordEncrypto($messages); + + $this->user->role = $this->role(); + + return $messages; + } + + protected function checkContract(array &$messages) + { + if ($this->user->role !== UserRole::SUPER_ADMIN && $this->user->contract_id === null) { + throw new LogicException("契約nullエラー"); + } + } + + protected function passwordEncrypto(array &$messages) + { + if ($this->user->isDirty(User::COL_NAME_PASSWORD)) { + $this->user->password = Hash::make($this->user->password); + } + } + + protected function checkEmailUnique(array &$messages) + { + if ($this->user->isDirty(User::COL_NAME_EMAIL)) { + + $exists = User::whereEmail($this->user->email) + ->where(User::COL_NAME_ID, '<>', $this->user->id) + ->exists(); + + if ($exists) { + $messages[User::COL_NAME_EMAIL] = trans('validation.unique'); + } + } + } + + abstract protected function role(): UserRole; +}