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

lonnieezell / myth-auth / 3871728138

pending completion
3871728138

Pull #587

github

GitHub
Merge a8c192379 into da3522738
Pull Request #587: Update development kit

815 of 2284 relevant lines covered (35.68%)

8.48 hits per line

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

88.57
/src/Models/PermissionModel.php
1
<?php
2

3
namespace Myth\Auth\Models;
4

5
use CodeIgniter\Model;
6
use Faker\Generator;
7
use Myth\Auth\Entities\Permission;
8

9
class PermissionModel extends Model
10
{
11
    protected $table         = 'auth_permissions';
12
    protected $returnType    = Permission::class;
13
    protected $allowedFields = [
14
        'name',
15
        'description',
16
    ];
17
    protected $validationRules = [
18
        'name'        => 'required|max_length[255]|is_unique[auth_permissions.name,name,{name}]',
19
        'description' => 'max_length[255]',
20
    ];
21

22
    /**
23
     * Checks to see if a user, or one of their groups,
24
     * has a specific permission.
25
     */
26
    public function doesUserHavePermission(int $userId, int $permissionId): bool
27
    {
28
        return array_key_exists($permissionId, $this->getPermissionsForUser($userId));
9✔
29
    }
30

31
    /**
32
     * Adds a single permission to a single user.
33
     *
34
     * @return bool
35
     */
36
    public function addPermissionToUser(int $permissionId, int $userId)
37
    {
38
        cache()->delete("{$userId}_permissions");
5✔
39

40
        return $this->db->table('auth_users_permissions')->insert([
5✔
41
            'user_id'       => $userId,
5✔
42
            'permission_id' => $permissionId,
5✔
43
        ]);
5✔
44
    }
45

46
    /**
47
     * Removes a permission from a user.
48
     *
49
     * @return bool
50
     */
51
    public function removePermissionFromUser(int $permissionId, int $userId)
52
    {
53
        cache()->delete("{$userId}_permissions");
1✔
54

55
        return $this->db->table('auth_users_permissions')->where([
1✔
56
            'user_id'       => $userId,
1✔
57
            'permission_id' => $permissionId,
1✔
58
        ])->delete();
1✔
59
    }
60

61
    /**
62
     * Gets all permissions for a user in a way that can be
63
     * easily used to check against:
64
     *
65
     * @return array<int, string> An array in format permissionId => permissionName
66
     */
67
    public function getPermissionsForUser(int $userId): array
68
    {
69
        if (null === $found = cache("{$userId}_permissions")) {
18✔
70
            $fromUser = $this->db->table('auth_users_permissions')
16✔
71
                ->select('id, auth_permissions.name')
16✔
72
                ->join('auth_permissions', 'auth_permissions.id = permission_id', 'inner')
16✔
73
                ->where('user_id', $userId)
16✔
74
                ->get()
16✔
75
                ->getResultObject();
16✔
76
            $fromGroup = $this->db->table('auth_groups_users')
16✔
77
                ->select('auth_permissions.id, auth_permissions.name')
16✔
78
                ->join('auth_groups_permissions', 'auth_groups_permissions.group_id = auth_groups_users.group_id', 'inner')
16✔
79
                ->join('auth_permissions', 'auth_permissions.id = auth_groups_permissions.permission_id', 'inner')
16✔
80
                ->where('user_id', $userId)
16✔
81
                ->get()
16✔
82
                ->getResultObject();
16✔
83

84
            $combined = array_merge($fromUser, $fromGroup);
16✔
85

86
            $found = [];
16✔
87

88
            foreach ($combined as $row) {
16✔
89
                $found[$row->id] = strtolower($row->name);
14✔
90
            }
91

92
            cache()->save("{$userId}_permissions", $found, 300);
16✔
93
        }
94

95
        return $found;
18✔
96
    }
97

98
    /**
99
     * Faked data for Fabricator.
100
     *
101
     * @return array|Permission See PermissionFaker
102
     */
103
    public function fake(Generator &$faker)
104
    {
105
        return new Permission([
×
106
            'name'        => $faker->word,
×
107
            'description' => $faker->sentence,
×
108
        ]);
×
109
    }
110
}
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