Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
24 / 26
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SetupVersion_0000
92.31% covered (success)
92.31%
24 / 26
50.00% covered (danger)
50.00%
1 / 2
4.01
0.00% covered (danger)
0.00%
0 / 1
 migrateIOSystem
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 migrateDatabase
92.00% covered (success)
92.00%
23 / 25
0.00% covered (danger)
0.00%
0 / 1
3.00
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Setup\Versions;
6
7use PeServer\App\Models\Setup\DatabaseSetupArgument;
8use PeServer\App\Models\Setup\IOSetupArgument;
9use PeServer\Core\Code;
10use PeServer\Core\Regex;
11
12/**
13 * @SuppressWarnings(PHPMD.CamelCaseClassName)
14 */
15#[Version(0)]
16class SetupVersion_0000 extends SetupVersionBase //phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
17{
18    #region SetupVersionBase
19
20    protected function migrateIOSystem(IOSetupArgument $argument): void
21    {
22        //NOP
23    }
24
25    /**
26     * Undocumented function
27     *
28     * @param DatabaseSetupArgument $argument
29     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
30     */
31    protected function migrateDatabase(DatabaseSetupArgument $argument): void
32    {
33        //TODO: 全削除処理
34        $tableNameResult = $argument->default->query(
35            <<<SQL
36
37            select
38                sqlite_master.name as name
39            from
40                sqlite_master
41            where
42                sqlite_master.type='table'
43                and
44                sqlite_master.name <> 'sqlite_sequence'
45
46            SQL
47        );
48
49        foreach ($tableNameResult->rows as $tableNameRow) {
50            $tableName = $tableNameRow['name'];
51            $argument->default->execute(Code::toLiteralString("drop table $tableName"));
52        }
53
54        //TODO: 暗号化とかとか
55        $userId = '00000000-0000-4000-0000-000000000000';
56        $loginId = 'setup_' . bin2hex(openssl_random_pseudo_bytes(4)) . '_' . date('YmdHis');
57        $rawPassword = bin2hex(openssl_random_pseudo_bytes(4));
58        $encPassword = password_hash($rawPassword, PASSWORD_DEFAULT);
59
60        $statements = <<<SQL
61
62            create table
63                [database_version] -- DBバージョン
64                (
65                    [version] integer not null
66                )
67            ;
68
69            create table
70                [users] -- ユーザー情報
71                (
72                    [user_id] text not null, -- ユーザーID
73                    [login_id] text not null, -- ログインID
74                    [level] text not null, -- ユーザーレベル(権限てきな)
75                    [state] text not null, -- 状態
76                    [name] text not null, -- 名前
77                    [email] text not null, -- メールアドレス(暗号化)
78                    [mark_email] integer not null, -- 絞り込み用メールアドレス(ハッシュ:fnv)
79                    [website] text not null, -- Webサイト
80                    [description] text not null, -- 説明文
81                    [note] text not null, -- 管理者用メモ
82                    primary key([user_id]),
83                    unique([login_id])
84                )
85            ;
86
87            create table
88                [user_authentications] -- ユーザー認証情報
89                (
90                    [user_id] text not null, -- ユーザーID
91                    [generated_password] text not null, -- 自動生成パスワード(ハッシュ) 空白の可能性あり(セットアップ・管理者等)
92                    [current_password] text not null, -- 現在パスワード(ハッシュ)
93                    primary key([user_id]),
94                    foreign key ([user_id]) references users([user_id])
95                )
96            ;
97
98            create table
99                [access_keys]
100                (
101                    [access_key] text not null, -- アクセスキー
102                    [user_id] text not null, -- ユーザーID
103                    primary key([access_key], [user_id]),
104                    unique([access_key]),
105                    foreign key ([user_id]) references users([user_id])
106                )
107            ;
108
109            create table
110                [user_audit_logs] -- 監査ログ
111                (
112                    [sequence] integer not null,
113                    [user_id] text not null, -- ユーザーID
114                    [timestamp] datetime not null, -- 書き込み日時(UTC)
115                    [event] text not null, -- イベント
116                    [info] text not null, -- 追加情報(JSON)
117                    [ip_address] text not null, -- クライアントIPアドレス
118                    [user_agent] text not null, -- クライアントUA
119                    primary key([sequence] autoincrement),
120                    foreign key ([user_id]) references users([user_id])
121                )
122            ;
123
124            create table
125                [user_change_wait_emails] -- ユーザーメールアドレス変更確認
126                (
127                    [user_id] text not null, -- ユーザーID
128                    [token] text not null, -- トークン
129                    [timestamp] text not null, -- トークン発行日時(UTC)
130                    [email] text not null, -- 変更後メールアドレス(暗号化)
131                    [mark_email] integer not null, -- 絞り込み用メールアドレス(ハッシュ:fnv)
132                    primary key([user_id]),
133                    foreign key ([user_id]) references users([user_id])
134                )
135            ;
136
137            create table
138                [sign_up_wait_emails] -- 新規登録時のユーザーメールアドレス待機
139                (
140                    [token] text not null, -- トークン
141                    [email] text not null, -- メールアドレス(暗号化)
142                    [mark_email] integer not null, -- 絞り込み用メールアドレス(ハッシュ:fnv)
143                    [timestamp] text not null, -- トークン発行日時(UTC)
144                    [ip_address] text not null, -- クライアントIPアドレス
145                    [user_agent] text not null, -- クライアントUA
146                    primary key([token])
147                )
148            ;
149
150            create index
151                [sign_up_wait_emails_index_mark]
152                on
153                    [sign_up_wait_emails]
154                    (
155                        [mark_email]
156                    )
157            ;
158
159            create index
160                [sign_up_wait_emails_index_search]
161                on
162                    [sign_up_wait_emails]
163                    (
164                        [mark_email],
165                        [token]
166                    )
167            ;
168
169            create table
170                [plugins]
171                (
172                    [plugin_id] text not null, -- プラグインID
173                    [user_id] text not null, -- プラグイン所有ユーザー
174                    [plugin_name] text not null, -- プラグイン名,
175                    [display_name] text not null, -- プラグイン表示名
176                    [state] text not null, -- 状態
177                    [description] text not null, -- 説明文
178                    [note] text not null, -- 管理者用メモ
179                    primary key([plugin_id]),
180                    unique ([plugin_name]),
181                    foreign key ([user_id]) references users([user_id])
182                )
183            ;
184
185            create table
186                [plugin_urls]
187                (
188                    [plugin_id] text not null, -- プラグインID
189                    [key] text not null, -- 種類
190                    [url] text not null, -- URL
191                    primary key([plugin_id], [key]),
192                    foreign key ([plugin_id]) references plugins([plugin_id])
193                )
194            ;
195
196            create table
197                [plugin_categories]
198                (
199                    [plugin_category_id] text not null, -- カテゴリID
200                    [display_name] text not null, -- 表示名
201                    [description] text not null, -- 説明文
202                    primary key([plugin_category_id])
203                )
204            ;
205
206            create table
207                [plugin_category_mappings]
208                (
209                    [plugin_id] text not null, -- プラグインID
210                    [plugin_category_id] text not null, -- カテゴリID
211                    primary key([plugin_id], [plugin_category_id]),
212                    foreign key ([plugin_id]) references plugins([plugin_id])
213                    foreign key ([plugin_category_id]) references plugin_categories([plugin_category_id])
214                )
215            ;
216
217            insert into
218                [users]
219                (
220                    [user_id],
221                    [login_id],
222                    [level],
223                    [state],
224                    [name],
225                    [email],
226                    [mark_email],
227                    [website],
228                    [description],
229                    [note]
230                )
231                values
232                (
233                    {$argument->default->escapeValue($userId)},
234                    {$argument->default->escapeValue($loginId)},
235                    'setup',
236                    'enabled',
237                    'setup user',
238                    '',
239                    0,
240                    '',
241                    '',
242                    ''
243                )
244            ;
245
246            insert into
247                [user_authentications]
248                (
249                    [user_id],
250                    [generated_password],
251                    [current_password]
252                )
253                values
254                (
255                    {$argument->default->escapeValue($userId)},
256                    '',
257                    {$argument->default->escapeValue($encPassword)}
258                )
259            ;
260
261            insert into
262                [plugin_categories]
263                (
264                    [plugin_category_id],
265                    [display_name],
266                    [description]
267                )
268                values
269                ( 'theme', 'テーマ', '' ),
270                --
271                ( 'file', 'ファイル', '' ),
272                ( 'network', 'ネットワーク', '' ),
273                ( 'news', 'ニュース', '' ),
274                ( 'search', '検索', '' ),
275                ( 'system', 'システム', '' ),
276                --
277                ( 'utility', 'ユーティリティ', '' ),
278                ( 'toy', 'おもちゃ', '' )
279            ;
280
281        SQL;
282
283        foreach ($this->splitStatements($statements) as $statement) {
284            $argument->default->execute($statement);
285        }
286
287        $this->logger->info('SETUP LOG');
288        $this->logger->info([
289            'user_id' => $userId,
290            'login_id' => $loginId,
291            'password' => $rawPassword,
292        ]);
293    }
294
295    #endregion
296}