Skip to content

Commit b84d3c8

Browse files
committed
fix: add missing assertion $user->id !== null
1 parent 9b889b3 commit b84d3c8

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/Models/UserIdentityModel.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function create($data): void
5959
*/
6060
public function createEmailIdentity(User $user, array $credentials): void
6161
{
62+
assert($user->id !== null, '"$user->id" must not be null.');
63+
6264
/** @var Passwords $passwords */
6365
$passwords = service('passwords');
6466

@@ -85,7 +87,7 @@ public function createCodeIdentity(
8587
array $data,
8688
callable $codeGenerator
8789
): string {
88-
assert($user->id !== null);
90+
assert($user->id !== null, '"$user->id" must not be null.');
8991

9092
helper('text');
9193

@@ -120,6 +122,8 @@ public function createCodeIdentity(
120122
*/
121123
public function generateAccessToken(User $user, string $name, array $scopes = ['*']): AccessToken
122124
{
125+
assert($user->id !== null, '"$user->id" must not be null.');
126+
123127
helper('text');
124128

125129
$return = $this->insert([
@@ -153,6 +157,8 @@ public function getAccessTokenByRawToken(string $rawToken): ?AccessToken
153157

154158
public function getAccessToken(User $user, string $rawToken): ?AccessToken
155159
{
160+
assert($user->id !== null, '"$user->id" must not be null.');
161+
156162
return $this->where('user_id', $user->id)
157163
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
158164
->where('secret', hash('sha256', $rawToken))
@@ -167,6 +173,8 @@ public function getAccessToken(User $user, string $rawToken): ?AccessToken
167173
*/
168174
public function getAccessTokenById($id, User $user): ?AccessToken
169175
{
176+
assert($user->id !== null, '"$user->id" must not be null.');
177+
170178
return $this->where('user_id', $user->id)
171179
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
172180
->where('id', $id)
@@ -179,6 +187,8 @@ public function getAccessTokenById($id, User $user): ?AccessToken
179187
*/
180188
public function getAllAccessTokens(User $user): array
181189
{
190+
assert($user->id !== null, '"$user->id" must not be null.');
191+
182192
return $this
183193
->where('user_id', $user->id)
184194
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
@@ -208,6 +218,8 @@ public function getIdentityBySecret(string $type, ?string $secret): ?UserIdentit
208218
*/
209219
public function getIdentities(User $user): array
210220
{
221+
assert($user->id !== null, '"$user->id" must not be null.');
222+
211223
return $this->where('user_id', $user->id)->orderBy($this->primaryKey)->findAll();
212224
}
213225

@@ -226,6 +238,8 @@ public function getIdentitiesByUserIds(array $userIds): array
226238
*/
227239
public function getIdentityByType(User $user, string $type): ?UserIdentity
228240
{
241+
assert($user->id !== null, '"$user->id" must not be null.');
242+
229243
return $this->where('user_id', $user->id)
230244
->where('type', $type)
231245
->orderBy($this->primaryKey)
@@ -241,6 +255,8 @@ public function getIdentityByType(User $user, string $type): ?UserIdentity
241255
*/
242256
public function getIdentitiesByTypes(User $user, array $types): array
243257
{
258+
assert($user->id !== null, '"$user->id" must not be null.');
259+
244260
if ($types === []) {
245261
return [];
246262
}
@@ -265,6 +281,8 @@ public function touchIdentity(UserIdentity $identity): void
265281

266282
public function deleteIdentitiesByType(User $user, string $type): void
267283
{
284+
assert($user->id !== null, '"$user->id" must not be null.');
285+
268286
$return = $this->where('user_id', $user->id)
269287
->where('type', $type)
270288
->delete();
@@ -277,6 +295,8 @@ public function deleteIdentitiesByType(User $user, string $type): void
277295
*/
278296
public function revokeAccessToken(User $user, string $rawToken): void
279297
{
298+
assert($user->id !== null, '"$user->id" must not be null.');
299+
280300
$return = $this->where('user_id', $user->id)
281301
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
282302
->where('secret', hash('sha256', $rawToken))
@@ -290,6 +310,8 @@ public function revokeAccessToken(User $user, string $rawToken): void
290310
*/
291311
public function revokeAllAccessTokens(User $user): void
292312
{
313+
assert($user->id !== null, '"$user->id" must not be null.');
314+
293315
$return = $this->where('user_id', $user->id)
294316
->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN)
295317
->delete();

src/Models/UserModel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ protected function saveEmailIdentity(array $data): array
303303
/** @var User $user */
304304
$user = $this->find($this->db->insertID());
305305

306+
// If you get identity (email/password), the User object must have the id.
307+
$this->tempUser->id = $user->id;
308+
306309
$user->email = $this->tempUser->email ?? '';
307310
$user->password = $this->tempUser->password ?? '';
308311
$user->password_hash = $this->tempUser->password_hash ?? '';

0 commit comments

Comments
 (0)