Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
21.74% covered (danger)
21.74%
15 / 69
22.22% covered (danger)
22.22%
2 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserAuthenticationsEntityDao
21.74% covered (danger)
21.74%
15 / 69
22.22% covered (danger)
22.22%
2 / 9
47.83
0.00% covered (danger)
0.00%
0 / 1
 selectPassword
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 selectPasswordReminderByToken
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 selectExistsToken
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 insertUserAuthentication
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 updateCurrentPassword
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 updatePasswordReminder
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 updateResetPassword
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 updatePasswordOnly
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 updateClearReminder
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Dao\Entities;
6
7use DateTimeInterface;
8use PeServer\Core\Database\DaoBase;
9use PeServer\Core\Database\DaoTrait;
10use PeServer\Core\Database\DatabaseRowResult;
11use PeServer\Core\Database\IDatabaseContext;
12
13class UserAuthenticationsEntityDao extends DaoBase
14{
15    use DaoTrait;
16
17    #region function
18
19    /**
20     * @template TFieldArray of array{current_password:string}
21     * @param string $userId
22     * @phpstan-return DatabaseRowResult<TFieldArray>
23     */
24    public function selectPassword(string $userId): DatabaseRowResult
25    {
26        /** @phpstan-var DatabaseRowResult<TFieldArray> */
27        return $this->context->querySingle(
28            <<<SQL
29
30            select
31                user_authentications.current_password
32            from
33                user_authentications
34            where
35                user_authentications.user_id = :user_id
36
37            SQL,
38            [
39                'user_id' => $userId
40            ]
41        );
42    }
43
44    /**
45     *
46     * @template TFieldArray of array{user_id:string,reminder_token:string,reminder_timestamp:string}
47     * @param string $token
48     * @return DatabaseRowResult|null
49     * @phpstan-return DatabaseRowResult<TFieldArray>|null
50     */
51    public function selectPasswordReminderByToken(string $token): ?DatabaseRowResult
52    {
53        /** @var DatabaseRowResult<TFieldArray>|null */
54        return $this->context->querySingleOrNull(
55            <<<SQL
56
57            select
58                user_authentications.current_password
59            from
60                user_authentications
61            where
62                user_authentications.reminder_token = :token
63
64            SQL,
65            [
66                'token' => $token
67            ]
68        );
69    }
70
71    public function selectExistsToken(string $token, int $limitMinutes): bool
72    {
73        return 1 === $this->context->selectSingleCount(
74            <<<SQL
75
76            select
77                count(*)
78            from
79                user_authentications
80            where
81                user_authentications.reminder_token = :token
82                and
83                (STRFTIME('%s', CURRENT_TIMESTAMP) - STRFTIME('%s', user_authentications.reminder_timestamp)) < :limit_minutes * 60
84
85            SQL,
86            [
87                'token' => $token,
88                'limit_minutes' => $limitMinutes,
89            ]
90        );
91    }
92
93    public function insertUserAuthentication(string $userId, string $currentPassword): void
94    {
95        $this->context->insertSingle(
96            <<<SQL
97
98            insert into
99            user_authentications
100                (
101                    user_id,
102                    reminder_token,
103                    reminder_timestamp,
104                    current_password
105                )
106                values
107                (
108                    :user_id,
109                    '',
110                    NULL,
111                    :current_password
112                )
113
114            SQL,
115            [
116                'user_id' => $userId,
117                'current_password' => $currentPassword
118            ]
119        );
120    }
121
122    public function updateCurrentPassword(string $userId, string $currentPassword): void
123    {
124        $this->context->updateByKey(
125            <<<SQL
126
127            update
128                user_authentications
129            set
130                current_password = :current_password
131            where
132                user_id = :user_id
133
134            SQL,
135            [
136                'user_id' => $userId,
137                'current_password' => $currentPassword
138            ]
139        );
140    }
141
142    public function updatePasswordReminder(string $userId, string $token): void
143    {
144        $this->context->updateByKey(
145            <<<SQL
146
147            update
148                user_authentications
149            set
150                reminder_token = :token,
151                reminder_timestamp = CURRENT_TIMESTAMP
152            where
153                user_id = :user_id
154
155            SQL,
156            [
157                'user_id' => $userId,
158                'token' => $token,
159            ]
160        );
161    }
162
163    public function updateResetPassword(string $userId, string $currentPassword): void
164    {
165        $this->context->updateByKey(
166            <<<SQL
167
168            update
169                user_authentications
170            set
171                current_password = :current_password,
172                reminder_token = '',
173                reminder_timestamp = NULL
174            where
175                user_id = :user_id
176
177            SQL,
178            [
179                'user_id' => $userId,
180                'current_password' => $currentPassword,
181            ]
182        );
183    }
184
185    /**
186     * パスワードの変更。
187     *
188     * 純粋に現在のパスワードのみを変更する。
189     *
190     * @param string $userId
191     * @param string $password
192     */
193    public function updatePasswordOnly(string $userId, string $password): void
194    {
195        $this->context->updateByKey(
196            <<<SQL
197
198            update
199                user_authentications
200            set
201                current_password = :password
202            where
203                user_id = :user_id
204
205            SQL,
206            [
207                'user_id' => $userId,
208                'password' => $password,
209            ]
210        );
211    }
212
213    public function updateClearReminder(string $userId): void
214    {
215        $this->context->updateByKey(
216            <<<SQL
217
218            update
219                user_authentications
220            set
221                reminder_token = '',
222                reminder_timestamp = NULL
223            where
224                user_id = :user_id
225
226            SQL,
227            [
228                'user_id' => $userId,
229            ]
230        );
231    }
232
233    #endregion
234}