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

lonnieezell / myth-auth / 3798654288

pending completion
3798654288

Pull #584

github

GitHub
Merge db6ea1b4f into da3522738
Pull Request #584: fixes bug with Group Permissions and User cache

829 of 2294 relevant lines covered (36.14%)

8.52 hits per line

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

44.44
/src/Models/UserModel.php
1
<?php
2

3
namespace Myth\Auth\Models;
4

5
use CodeIgniter\Model;
6
use Faker\Generator;
7
use Myth\Auth\Authorization\GroupModel;
8
use Myth\Auth\Entities\User;
9

10
/**
11
 * @method User|null first()
12
 */
13
class UserModel extends Model
14
{
15
    protected $table          = 'users';
16
    protected $primaryKey     = 'id';
17
    protected $returnType     = User::class;
18
    protected $useSoftDeletes = true;
19
    protected $allowedFields  = [
20
        'email', 'username', 'password_hash', 'reset_hash', 'reset_at', 'reset_expires', 'activate_hash',
21
        'status', 'status_message', 'active', 'force_pass_reset', 'permissions', 'deleted_at',
22
    ];
23
    protected $useTimestamps   = true;
24
    protected $validationRules = [
25
        'email'         => 'required|valid_email|is_unique[users.email,id,{id}]',
26
        'username'      => 'required|alpha_numeric_punct|min_length[3]|max_length[30]|is_unique[users.username,id,{id}]',
27
        'password_hash' => 'required',
28
    ];
29
    protected $validationMessages = [];
30
    protected $skipValidation     = false;
31
    protected $afterInsert        = ['addToGroup'];
32

33
    /**
34
     * The id of a group to assign.
35
     * Set internally by withGroup.
36
     *
37
     * @var int|null
38
     */
39
    protected $assignGroup;
40

41
    /**
42
     * Logs a password reset attempt for posterity sake.
43
     */
44
    public function logResetAttempt(string $email, ?string $token = null, ?string $ipAddress = null, ?string $userAgent = null)
45
    {
46
        $this->db->table('auth_reset_attempts')->insert([
×
47
            'email'      => $email,
×
48
            'ip_address' => $ipAddress,
×
49
            'user_agent' => $userAgent,
×
50
            'token'      => $token,
×
51
            'created_at' => date('Y-m-d H:i:s'),
×
52
        ]);
×
53
    }
54

55
    /**
56
     * Logs an activation attempt for posterity sake.
57
     */
58
    public function logActivationAttempt(?string $token = null, ?string $ipAddress = null, ?string $userAgent = null)
59
    {
60
        $this->db->table('auth_activation_attempts')->insert([
×
61
            'ip_address' => $ipAddress,
×
62
            'user_agent' => $userAgent,
×
63
            'token'      => $token,
×
64
            'created_at' => date('Y-m-d H:i:s'),
×
65
        ]);
×
66
    }
67

68
    /**
69
     * Sets the group to assign any users created.
70
     *
71
     * @return $this
72
     */
73
    public function withGroup(string $groupName)
74
    {
75
        $group = $this->db->table('auth_groups')->where('name', $groupName)->get()->getFirstRow();
2✔
76

77
        $this->assignGroup = $group->id;
2✔
78

79
        return $this;
2✔
80
    }
81

82
    /**
83
     * Clears the group to assign to newly created users.
84
     *
85
     * @return $this
86
     */
87
    public function clearGroup()
88
    {
89
        $this->assignGroup = null;
×
90

91
        return $this;
×
92
    }
93

94
    /**
95
     * If a default role is assigned in Config\Auth, will
96
     * add this user to that group. Will do nothing
97
     * if the group cannot be found.
98
     *
99
     * @param mixed $data
100
     *
101
     * @return mixed
102
     */
103
    protected function addToGroup($data)
104
    {
105
        if (is_numeric($this->assignGroup)) {
56✔
106
            $groupModel = model(GroupModel::class);
2✔
107
            $groupModel->addUserToGroup($data['id'], $this->assignGroup);
2✔
108
        }
109

110
        return $data;
56✔
111
    }
112

113
    /**
114
     * Faked data for Fabricator.
115
     */
116
    public function fake(Generator &$faker): User
117
    {
118
        return new User([
52✔
119
            'email'    => $faker->email,
52✔
120
            'username' => $faker->userName,
52✔
121
            'password' => bin2hex(random_bytes(16)),
52✔
122
        ]);
52✔
123
    }
124
}
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