1212use CodeIgniter \Shield \Entities \AccessToken ;
1313use CodeIgniter \Shield \Entities \User ;
1414use CodeIgniter \Shield \Entities \UserIdentity ;
15+ use CodeIgniter \Shield \Exceptions \LogicException ;
1516use Faker \Generator ;
1617
1718class UserIdentityModel extends Model
@@ -59,6 +60,8 @@ public function create($data): void
5960 */
6061 public function createEmailIdentity (User $ user , array $ credentials ): void
6162 {
63+ $ this ->checkUserId ($ user );
64+
6265 /** @var Passwords $passwords */
6366 $ passwords = service ('passwords ' );
6467
@@ -72,6 +75,15 @@ public function createEmailIdentity(User $user, array $credentials): void
7275 $ this ->checkQueryReturn ($ return );
7376 }
7477
78+ private function checkUserId (User $ user ): void
79+ {
80+ if ($ user ->id === null ) {
81+ throw new LogicException (
82+ '"$user->id" is null. You should not use the incomplete User object. '
83+ );
84+ }
85+ }
86+
7587 /**
7688 * Create an identity with 6 digits code for auth action
7789 *
@@ -85,7 +97,7 @@ public function createCodeIdentity(
8597 array $ data ,
8698 callable $ codeGenerator
8799 ): string {
88- assert ( $ user -> id !== null );
100+ $ this -> checkUserId ( $ user );
89101
90102 helper ('text ' );
91103
@@ -120,6 +132,8 @@ public function createCodeIdentity(
120132 */
121133 public function generateAccessToken (User $ user , string $ name , array $ scopes = ['* ' ]): AccessToken
122134 {
135+ $ this ->checkUserId ($ user );
136+
123137 helper ('text ' );
124138
125139 $ return = $ this ->insert ([
@@ -153,6 +167,8 @@ public function getAccessTokenByRawToken(string $rawToken): ?AccessToken
153167
154168 public function getAccessToken (User $ user , string $ rawToken ): ?AccessToken
155169 {
170+ $ this ->checkUserId ($ user );
171+
156172 return $ this ->where ('user_id ' , $ user ->id )
157173 ->where ('type ' , AccessTokens::ID_TYPE_ACCESS_TOKEN )
158174 ->where ('secret ' , hash ('sha256 ' , $ rawToken ))
@@ -167,6 +183,8 @@ public function getAccessToken(User $user, string $rawToken): ?AccessToken
167183 */
168184 public function getAccessTokenById ($ id , User $ user ): ?AccessToken
169185 {
186+ $ this ->checkUserId ($ user );
187+
170188 return $ this ->where ('user_id ' , $ user ->id )
171189 ->where ('type ' , AccessTokens::ID_TYPE_ACCESS_TOKEN )
172190 ->where ('id ' , $ id )
@@ -179,6 +197,8 @@ public function getAccessTokenById($id, User $user): ?AccessToken
179197 */
180198 public function getAllAccessTokens (User $ user ): array
181199 {
200+ $ this ->checkUserId ($ user );
201+
182202 return $ this
183203 ->where ('user_id ' , $ user ->id )
184204 ->where ('type ' , AccessTokens::ID_TYPE_ACCESS_TOKEN )
@@ -208,6 +228,8 @@ public function getIdentityBySecret(string $type, ?string $secret): ?UserIdentit
208228 */
209229 public function getIdentities (User $ user ): array
210230 {
231+ $ this ->checkUserId ($ user );
232+
211233 return $ this ->where ('user_id ' , $ user ->id )->orderBy ($ this ->primaryKey )->findAll ();
212234 }
213235
@@ -226,6 +248,8 @@ public function getIdentitiesByUserIds(array $userIds): array
226248 */
227249 public function getIdentityByType (User $ user , string $ type ): ?UserIdentity
228250 {
251+ $ this ->checkUserId ($ user );
252+
229253 return $ this ->where ('user_id ' , $ user ->id )
230254 ->where ('type ' , $ type )
231255 ->orderBy ($ this ->primaryKey )
@@ -241,6 +265,8 @@ public function getIdentityByType(User $user, string $type): ?UserIdentity
241265 */
242266 public function getIdentitiesByTypes (User $ user , array $ types ): array
243267 {
268+ $ this ->checkUserId ($ user );
269+
244270 if ($ types === []) {
245271 return [];
246272 }
@@ -265,6 +291,8 @@ public function touchIdentity(UserIdentity $identity): void
265291
266292 public function deleteIdentitiesByType (User $ user , string $ type ): void
267293 {
294+ $ this ->checkUserId ($ user );
295+
268296 $ return = $ this ->where ('user_id ' , $ user ->id )
269297 ->where ('type ' , $ type )
270298 ->delete ();
@@ -277,6 +305,8 @@ public function deleteIdentitiesByType(User $user, string $type): void
277305 */
278306 public function revokeAccessToken (User $ user , string $ rawToken ): void
279307 {
308+ $ this ->checkUserId ($ user );
309+
280310 $ return = $ this ->where ('user_id ' , $ user ->id )
281311 ->where ('type ' , AccessTokens::ID_TYPE_ACCESS_TOKEN )
282312 ->where ('secret ' , hash ('sha256 ' , $ rawToken ))
@@ -290,6 +320,8 @@ public function revokeAccessToken(User $user, string $rawToken): void
290320 */
291321 public function revokeAllAccessTokens (User $ user ): void
292322 {
323+ $ this ->checkUserId ($ user );
324+
293325 $ return = $ this ->where ('user_id ' , $ user ->id )
294326 ->where ('type ' , AccessTokens::ID_TYPE_ACCESS_TOKEN )
295327 ->delete ();
0 commit comments