• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / shield / 18221196185

03 Oct 2025 11:37AM UTC coverage: 92.818% (-0.04%) from 92.855%
18221196185

push

github

web-flow
fix rector (#1287)

6 of 6 new or added lines in 2 files covered. (100.0%)

1 existing line in 1 file now uncovered.

2895 of 3119 relevant lines covered (92.82%)

25.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/src/Models/LoginModel.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter Shield.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Shield\Models;
15

16
use CodeIgniter\I18n\Time;
17
use CodeIgniter\Shield\Authentication\Authenticators\Session;
18
use CodeIgniter\Shield\Entities\Login;
19
use CodeIgniter\Shield\Entities\User;
20
use Faker\Generator;
21

22
class LoginModel extends BaseModel
23
{
24
    protected $primaryKey     = 'id';
25
    protected $returnType     = Login::class;
26
    protected $useSoftDeletes = false;
27
    protected $allowedFields  = [
28
        'ip_address',
29
        'user_agent',
30
        'id_type',
31
        'identifier',
32
        'user_id',
33
        'date',
34
        'success',
35
    ];
36
    protected $useTimestamps   = false;
37
    protected $validationRules = [
38
        'ip_address' => 'required',
39
        'id_type'    => 'required',
40
        'identifier' => 'permit_empty|string',
41
        'user_agent' => 'permit_empty|string',
42
        'user_id'    => 'permit_empty',
43
        'date'       => 'required',
44
    ];
45
    protected $validationMessages = [];
46
    protected $skipValidation     = false;
47

48
    protected function initialize(): void
49
    {
50
        parent::initialize();
151✔
51

52
        $this->table = $this->tables['logins'];
151✔
53
    }
54

55
    /**
56
     * Records login attempt.
57
     *
58
     * @param string          $idType Identifier type. See const ID_YPE_* in Authenticator.
59
     *                                auth_logins: 'email_password'|'username'|'magic-link'
60
     *                                auth_token_logins: 'access-token'
61
     * @param int|string|null $userId
62
     */
63
    public function recordLoginAttempt(
64
        string $idType,
65
        string $identifier,
66
        bool $success,
67
        ?string $ipAddress = null,
68
        ?string $userAgent = null,
69
        $userId = null,
70
    ): void {
71
        $this->disableDBDebug();
35✔
72

73
        if ($this->db->getPlatform() === 'OCI8' && $identifier === '') {
35✔
UNCOV
74
            $identifier = ' ';
×
75
        }
76

77
        $return = $this->insert([
35✔
78
            'ip_address' => $ipAddress,
35✔
79
            'user_agent' => $userAgent,
35✔
80
            'id_type'    => $idType,
35✔
81
            'identifier' => $identifier,
35✔
82
            'user_id'    => $userId,
35✔
83
            'date'       => Time::now(),
35✔
84
            'success'    => (int) $success,
35✔
85
        ]);
35✔
86

87
        $this->checkQueryReturn($return);
35✔
88
    }
89

90
    /**
91
     * Returns the previous login information for the user,
92
     * useful to display to the user the last time the account
93
     * was accessed.
94
     */
95
    public function previousLogin(User $user): ?Login
96
    {
97
        return $this->where('success', 1)
1✔
98
            ->where('user_id', $user->id)
1✔
99
            ->orderBy('id', 'desc')
1✔
100
            ->limit(1, 1)->first();
1✔
101
    }
102

103
    /**
104
     * Returns the last login information for the user
105
     */
106
    public function lastLogin(User $user): ?Login
107
    {
108
        return $this->where('success', 1)
1✔
109
            ->where('user_id', $user->id)
1✔
110
            ->orderBy('id', 'desc')
1✔
111
            ->first();
1✔
112
    }
113

114
    /**
115
     * Generate a fake login for testing
116
     */
117
    public function fake(Generator &$faker): Login
118
    {
119
        return new Login([
2✔
120
            'ip_address' => $faker->ipv4(),
2✔
121
            'id_type'    => Session::ID_TYPE_EMAIL_PASSWORD,
2✔
122
            'identifier' => $faker->email(),
2✔
123
            'user_id'    => null,
2✔
124
            'date'       => Time::parse('-1 day'),
2✔
125
            'success'    => true,
2✔
126
        ]);
2✔
127
    }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc