| @@ -0,0 +1,105 @@ | |||
| <?php | |||
| namespace App\Console\Commands; | |||
| use App\Exceptions\AppCommonException; | |||
| use App\Logic\SMS\SMSManager; | |||
| use App\Logic\User\AdminUserManager; | |||
| use App\Models\User; | |||
| use App\Util\DBUtil; | |||
| use Exception; | |||
| use Illuminate\Support\Arr; | |||
| class SummaryUse extends BaseCommand | |||
| { | |||
| const COMMAND = "create:admin-user {--email=} {--name=} {--password=}"; | |||
| /** | |||
| * The name and signature of the console command. | |||
| * | |||
| * @var string | |||
| */ | |||
| protected $signature = self::COMMAND; | |||
| /** | |||
| * The console command description. | |||
| * | |||
| * @var string | |||
| */ | |||
| protected $description = '管理者ユーザーを作成する'; | |||
| static public function getCommand() | |||
| { | |||
| return self::COMMAND; | |||
| } | |||
| /** | |||
| * Create a new command instance. | |||
| * | |||
| * @return void | |||
| */ | |||
| public function __construct(private SMSManager $manager) | |||
| { | |||
| parent::__construct(); | |||
| } | |||
| /** | |||
| * Execute the console command. | |||
| * | |||
| * @return int | |||
| */ | |||
| public function service(): int | |||
| { | |||
| $db = DBUtil::instance(); | |||
| $params = $this->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, | |||
| ]; | |||
| } | |||
| } | |||
| @@ -0,0 +1,46 @@ | |||
| <?php | |||
| namespace App\Logic\User; | |||
| use App\Codes\UserRole; | |||
| use App\Models\Contract; | |||
| use App\Models\User; | |||
| use LogicException; | |||
| class AdminUserManager extends UserManager | |||
| { | |||
| public function initForCreateAdmin() | |||
| { | |||
| $this->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; | |||
| } | |||
| } | |||
| @@ -0,0 +1,13 @@ | |||
| <?php | |||
| namespace App\Logic\User; | |||
| use App\Codes\UserRole; | |||
| class ContractAdminUserManager extends UserManager | |||
| { | |||
| protected function role(): UserRole | |||
| { | |||
| return UserRole::CONTRACT_ADMIN; | |||
| } | |||
| } | |||
| @@ -3,144 +3,11 @@ | |||
| namespace App\Logic\User; | |||
| use App\Codes\UserRole; | |||
| use App\Models\Contract; | |||
| use App\Models\User; | |||
| use Illuminate\Support\Arr; | |||
| use Illuminate\Support\Carbon; | |||
| use Illuminate\Support\Facades\Hash; | |||
| use Illuminate\Support\Facades\Validator; | |||
| use LogicException; | |||
| class LoginUserManager | |||
| class LoginUserManager extends UserManager | |||
| { | |||
| private bool $initialized = false; | |||
| private User $user; | |||
| private Contract $contract; | |||
| public function __construct() | |||
| { | |||
| } | |||
| public function initForCreate(string|Contract $contractId) | |||
| { | |||
| $this->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; | |||
| } | |||
| } | |||
| @@ -0,0 +1,157 @@ | |||
| <?php | |||
| namespace App\Logic\User; | |||
| use App\Codes\UserRole; | |||
| use App\Models\Contract; | |||
| use App\Models\User; | |||
| use Illuminate\Support\Carbon; | |||
| use Illuminate\Support\Facades\Hash; | |||
| use Illuminate\Support\Facades\Validator; | |||
| use LogicException; | |||
| abstract class UserManager | |||
| { | |||
| protected bool $initialized = false; | |||
| protected ?User $user = null; | |||
| protected ?Contract $contract = null; | |||
| public function initForCreate(string|Contract $contractId) | |||
| { | |||
| $this->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; | |||
| } | |||