Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 84 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ManagementSetupLogic | |
0.00% |
0 / 84 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
startup | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
validateImpl | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
6 | |||
executeImpl | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models\Domain\Page\Management; |
6 | |
7 | use PeServer\Core\Cryptography; |
8 | use PeServer\Core\Text; |
9 | use PeServer\App\Models\AuditLog; |
10 | use PeServer\Core\Mvc\LogicCallMode; |
11 | use PeServer\Core\Mvc\LogicParameter; |
12 | use PeServer\App\Models\SessionKey; |
13 | use PeServer\App\Models\AppCryptography; |
14 | use PeServer\App\Models\Domain\UserLevel; |
15 | use PeServer\App\Models\Domain\UserState; |
16 | use PeServer\App\Models\Domain\UserUtility; |
17 | use PeServer\Core\Database\IDatabaseContext; |
18 | use PeServer\App\Models\Domain\AccountValidator; |
19 | use PeServer\App\Models\Domain\Page\PageLogicBase; |
20 | use PeServer\App\Models\Dao\Entities\UsersEntityDao; |
21 | use PeServer\App\Models\Dao\Entities\UserAuthenticationsEntityDao; |
22 | use PeServer\Core\Collection\Arr; |
23 | |
24 | class ManagementSetupLogic extends PageLogicBase |
25 | { |
26 | public function __construct(LogicParameter $parameter, private AppCryptography $cryptography) |
27 | { |
28 | parent::__construct($parameter); |
29 | } |
30 | |
31 | protected function startup(LogicCallMode $callMode): void |
32 | { |
33 | $this->registerParameterKeys([ |
34 | 'setting_setup_login_id', |
35 | 'setting_setup_password', |
36 | 'setting_setup_user_name', |
37 | 'setting_setup_email', |
38 | 'setting_setup_website', |
39 | ], true); |
40 | |
41 | $this->setValue('setting_setup_password', Text::EMPTY); |
42 | } |
43 | |
44 | protected function validateImpl(LogicCallMode $callMode): void |
45 | { |
46 | if ($callMode === LogicCallMode::Initialize) { |
47 | return; |
48 | } |
49 | |
50 | $this->validation('setting_setup_login_id', function ($key, $value) { |
51 | $accountValidator = new AccountValidator($this, $this->validator); |
52 | $accountValidator->isLoginId($key, $value); |
53 | }); |
54 | |
55 | $this->validation('setting_setup_password', function (string $key, string $value) { |
56 | $accountValidator = new AccountValidator($this, $this->validator); |
57 | $accountValidator->isPassword($key, $value); |
58 | }, ['trim' => false]); |
59 | |
60 | $this->validation('setting_setup_user_name', function (string $key, string $value) { |
61 | $accountValidator = new AccountValidator($this, $this->validator); |
62 | $accountValidator->isUserName($key, $value); |
63 | }); |
64 | |
65 | $this->validation('setting_setup_email', function (string $key, string $value) { |
66 | $accountValidator = new AccountValidator($this, $this->validator); |
67 | $accountValidator->isEmail($key, $value); |
68 | }); |
69 | |
70 | $this->validation('setting_setup_website', function (string $key, string $value) { |
71 | $accountValidator = new AccountValidator($this, $this->validator); |
72 | $accountValidator->isWebsite($key, $value); |
73 | }); |
74 | } |
75 | |
76 | protected function executeImpl(LogicCallMode $callMode): void |
77 | { |
78 | if ($callMode === LogicCallMode::Initialize) { |
79 | return; |
80 | } |
81 | |
82 | $currentUserInfo = $this->requireSession(SessionKey::ACCOUNT); |
83 | |
84 | $email = $this->getRequest('setting_setup_email'); |
85 | |
86 | $params = [ |
87 | 'login_id' => $this->getRequest('setting_setup_login_id'), |
88 | 'password' => $this->getRequest('setting_setup_password', Text::EMPTY, false), |
89 | 'user_name' => $this->getRequest('setting_setup_user_name'), |
90 | 'email' => $this->cryptography->encrypt($email), |
91 | 'mark_email' => $this->cryptography->toMark($email), |
92 | 'website' => $this->getRequest('setting_setup_website'), |
93 | ]; |
94 | |
95 | $userInfo = [ |
96 | 'id' => UserUtility::generateUserId(), |
97 | 'current_password' => Cryptography::hashPassword($params['password']), |
98 | ]; |
99 | |
100 | $database = $this->openDatabase(); |
101 | |
102 | $result = $database->transaction(function (IDatabaseContext $database) use ($currentUserInfo, $params, $userInfo) { |
103 | $accountValidator = new AccountValidator($this, $this->validator); |
104 | |
105 | $loginId = $params['login_id']; |
106 | if (!$accountValidator->isFreeLoginId($database, 'setting_setup_login_id', $loginId)) { |
107 | return false; |
108 | } |
109 | |
110 | $usersEntityDao = new UsersEntityDao($database); |
111 | $userAuthenticationsEntityDao = new UserAuthenticationsEntityDao($database); |
112 | |
113 | // 管理者ユーザーの登録 |
114 | $usersEntityDao->insertUser( |
115 | $userInfo['id'], |
116 | $loginId, |
117 | UserLevel::ADMINISTRATOR, |
118 | UserState::ENABLED, |
119 | $params['user_name'], |
120 | $params['email'], |
121 | $params['mark_email'], |
122 | $params['website'], |
123 | Text::EMPTY, |
124 | Text::EMPTY |
125 | ); |
126 | |
127 | $userAuthenticationsEntityDao->insertUserAuthentication( |
128 | $userInfo['id'], |
129 | $userInfo['current_password'] |
130 | ); |
131 | |
132 | // 現在のセットアップユーザーを無効化 |
133 | $state = UserState::DISABLED; |
134 | $usersEntityDao->updateUserState( |
135 | $currentUserInfo->userId, |
136 | $state |
137 | ); |
138 | |
139 | // ユーザー生成記録を監査ログに追加 |
140 | $this->writeAuditLogCurrentUser(AuditLog::USER_STATE_CHANGE, ['state' => $state], $database); |
141 | $this->writeAuditLogCurrentUser(AuditLog::USER_CREATE, ['user_id' => $userInfo['id']], $database); |
142 | $this->writeAuditLogTargetUser($userInfo['id'], AuditLog::USER_GENERATED, ['user_id' => $currentUserInfo->userId], $database); |
143 | |
144 | return true; |
145 | }); |
146 | |
147 | // 生成したのであれば現在のセットアップユーザーは用済みなのでログアウト |
148 | if ($result) { |
149 | $this->logger->info("セットアップユーザーお役目終了"); |
150 | $this->shutdownSession(); |
151 | } |
152 | } |
153 | } |