Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DevelopmentApiAdministratorLogic
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateImpl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeImpl
0.00% covered (danger)
0.00%
0 / 82
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain\Api\DevelopmentApi;
6
7use PeServer\Core\Mime;
8use PeServer\Core\Cryptography;
9use PeServer\Core\Mvc\LogicCallMode;
10use PeServer\App\Models\ResponseJson;
11use PeServer\Core\Mvc\LogicParameter;
12use PeServer\App\Models\AppCryptography;
13use PeServer\App\Models\Domain\UserLevel;
14use PeServer\App\Models\Domain\UserState;
15use PeServer\Core\Database\IDatabaseContext;
16use PeServer\App\Models\Domain\Api\ApiLogicBase;
17use PeServer\App\Models\Dao\Entities\UsersEntityDao;
18use PeServer\App\Models\Dao\Entities\UserAuthenticationsEntityDao;
19
20class DevelopmentApiAdministratorLogic extends ApiLogicBase
21{
22    public function __construct(LogicParameter $parameter, private AppCryptography $cryptography)
23    {
24        parent::__construct($parameter);
25    }
26
27    #region ApiLogicBase
28
29    protected function validateImpl(LogicCallMode $callMode): void
30    {
31        //NOP
32    }
33
34    protected function executeImpl(LogicCallMode $callMode): void
35    {
36        $users = [
37            [
38                'user_id' => 'ffffffff-ffff-4fff-ffff-ffffffffffff',
39                'login_id' => 'root',
40                'password' => 'root',
41                'email' => 'root@localhost.localdomain',
42                'level' => UserLevel::ADMINISTRATOR,
43                'note' => '開発用 自動生成 管理者'
44            ],
45            [
46                'user_id' => 'eeeeeeee-eeee-4eee-eeee-eeeeeeeeeeee',
47                'login_id' => 'user',
48                'password' => 'user',
49                'email' => 'user@localhost.localdomain',
50                'level' => UserLevel::USER,
51                'note' => '開発用 自動生成 ユーザー'
52            ],
53            [
54                'user_id' => '99999999-1000-4999-9999-999999999999',
55                'login_id' => 'zap-admin',
56                'password' => 'zap-admin',
57                'email' => 'zap-admin@localhost.localdomain',
58                'level' => UserLevel::ADMINISTRATOR,
59                'note' => 'ZAP用 自動生成 管理者'
60            ],
61            [
62                'user_id' => '99999999-2000-4999-9999-999999999999',
63                'login_id' => 'zap-user',
64                'password' => 'zap-user',
65                'email' => 'zap-user@localhost.localdomain',
66                'level' => UserLevel::USER,
67                'note' => 'ZAP用 自動生成 ユーザー'
68            ],
69        ];
70
71        $params = array_map(function ($i) {
72            return [
73                'user_id' => $i['user_id'],
74                'login_id' => $i['login_id'],
75                'password' => Cryptography::hashPassword($i['password']),
76                'user_name' => 'user-' . $i['login_id'],
77                'level' => $i['level'],
78                'email' => $this->cryptography->encrypt($i['email']),
79                'mark_email' => $this->cryptography->toMark($i['email']),
80                'website' => 'http://localhost',
81                'description' => $i['note'],
82                'note' => $i['note'],
83            ];
84        }, $users);
85
86        $database = $this->openDatabase();
87
88        $result = $database->transaction(function (IDatabaseContext $context) use ($params) {
89            $usersEntityDao = new UsersEntityDao($context);
90            $userAuthenticationsEntityDao = new UserAuthenticationsEntityDao($context);
91
92            foreach ($params as $user) {
93                $usersEntityDao->insertUser(
94                    $user['user_id'],
95                    $user['login_id'],
96                    $user['level'],
97                    UserState::ENABLED,
98                    $user['user_name'],
99                    $user['email'],
100                    (int)$user['mark_email'],
101                    $user['website'],
102                    $user['description'],
103                    $user['note']
104                );
105
106                $userAuthenticationsEntityDao->insertUserAuthentication(
107                    $user['user_id'],
108                    $user['password']
109                );
110            }
111
112            $context->update(
113                <<<SQL
114
115                update
116                    users
117                set
118                    state = :state
119                where
120                    level = :level
121
122                SQL,
123                [
124                    'state' => UserState::DISABLED,
125                    'level' => UserLevel::SETUP,
126                ]
127            );
128
129            return true;
130        });
131
132        $this->setResponseJson(ResponseJson::success([
133            'success' => $result,
134        ]));
135    }
136
137    #endregion
138}