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

FastyBird / simple-auth / 10019980238

20 Jul 2024 11:33AM UTC coverage: 68.883% (+2.3%) from 66.621%
10019980238

push

github

web-flow
Using custom database adapter for casbin (#8)

48 of 64 new or added lines in 4 files covered. (75.0%)

1 existing line in 1 file now uncovered.

518 of 752 relevant lines covered (68.88%)

4.85 hits per line

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

64.44
/src/Models/Casbin/Adapter.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * Adapter.php
5
 *
6
 * @license        More in LICENSE.md
7
 * @copyright      https://www.fastybird.com
8
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
9
 * @package        FastyBird:SimpleAuth!
10
 * @subpackage     Models
11
 * @since          0.1.0
12
 *
13
 * @date           09.07.20
14
 */
15

16
namespace FastyBird\SimpleAuth\Models\Casbin;
17

18
use Casbin\Model as CasbinModel;
19
use Casbin\Persist as CasbinPersist;
20
use FastyBird\SimpleAuth\Exceptions;
21
use FastyBird\SimpleAuth\Models;
22
use FastyBird\SimpleAuth\Queries;
23
use FastyBird\SimpleAuth\Types\PolicyType;
24
use IPub\DoctrineCrud\Exceptions as DoctrineCrudExceptions;
25
use Nette\Utils\ArrayHash;
26
use TypeError;
27
use ValueError;
28
use function array_filter;
29
use function count;
30
use function implode;
31
use function intval;
32
use function range;
33
use function strval;
34
use function trim;
35
use function var_dump;
36

37
/**
38
 * Casbin database adapter
39
 *
40
 * @package        FastyBird:SimpleAuth!
41
 * @subpackage     Models
42
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
43
 */
44
class Adapter implements CasbinPersist\Adapter
45
{
46

47
        use CasbinPersist\AdapterHelper;
48

49
        public function __construct(
50
                private readonly Models\Policies\Repository $policiesRepository,
51
                private readonly Models\Policies\Manager $policiesManager,
52
        )
53
        {
54
        }
3✔
55

56
        /**
57
         * @param array<string, string|null> $rule
58
         *
59
         * @throws TypeError
60
         * @throws ValueError
61
         */
62
        public function savePolicyLine(string $ptype, array $rule): void
63
        {
64
                $data = [
3✔
65
                        'type' => PolicyType::from($ptype),
3✔
66
                ];
3✔
67

68
                foreach ($rule as $key => $value) {
3✔
69
                        $data['v' . strval($key)] = $value;
3✔
70
                }
71

72
                $this->policiesManager->create(ArrayHash::from($data));
3✔
73
        }
74

75
        /**
76
         * @throws Exceptions\InvalidState
77
         */
78
        public function loadPolicy(CasbinModel\Model $model): void
79
        {
80
                $findPoliciesQuery = new Queries\FindPolicies();
3✔
81

82
                $policies = $this->policiesRepository->findAllBy($findPoliciesQuery);
3✔
83

84
                foreach ($policies as $policy) {
3✔
85
                        $data = [
3✔
86
                                $policy->getType()->value,
3✔
87
                                $policy->getV0(),
3✔
88
                                $policy->getV1(),
3✔
89
                                $policy->getV2(),
3✔
90
                                $policy->getV3(),
3✔
91
                                $policy->getV4(),
3✔
92
                                $policy->getV5(),
3✔
93
                        ];
3✔
94

95
                        $line = implode(', ', array_filter($data, static fn ($val) => $val != '' && $val !== null));
3✔
96

97
                        $this->loadPolicyLine(trim($line), $model);
3✔
98
                }
99
        }
100

101
        /**
102
         * @throws TypeError
103
         * @throws ValueError
104
         */
105
        public function savePolicy(CasbinModel\Model $model): void
106
        {
NEW
107
                foreach ($model['p'] ?? [] as $type => $ast) {
×
NEW
108
                        foreach ($ast->policy as $rule) {
×
NEW
109
                                $this->savePolicyLine($type, $rule);
×
110
                        }
111
                }
112

NEW
113
                foreach ($model['g'] ?? [] as $type => $ast) {
×
NEW
114
                        foreach ($ast->policy as $rule) {
×
NEW
115
                                $this->savePolicyLine($type, $rule);
×
116
                        }
117
                }
118
        }
119

120
        /**
121
         * @throws TypeError
122
         * @throws ValueError
123
         */
124
        public function addPolicy(string $sec, string $ptype, array $rule): void
125
        {
126
                $this->savePolicyLine($ptype, $rule);
3✔
127
        }
128

129
        /**
130
         * @throws DoctrineCrudExceptions\InvalidArgumentException
131
         * @throws Exceptions\InvalidState
132
         * @throws TypeError
133
         * @throws ValueError
134
         */
135
        public function removePolicy(string $sec, string $ptype, array $rule): void
136
        {
137
                $findPoliciesQuery = new Queries\FindPolicies();
3✔
138
                $findPoliciesQuery->byType(PolicyType::from($ptype));
3✔
139

140
                foreach ($rule as $key => $value) {
3✔
141
                        $findPoliciesQuery->byValue(intval($key), $value);
3✔
142
                }
143

144
                $policies = $this->policiesRepository->findAllBy($findPoliciesQuery);
3✔
145

146
                foreach ($policies as $policy) {
3✔
147
                        $this->policiesManager->delete($policy);
3✔
148
                }
149
        }
150

151
        /**
152
         * @throws DoctrineCrudExceptions\InvalidArgumentException
153
         * @throws Exceptions\InvalidState
154
         * @throws TypeError
155
         * @throws ValueError
156
         */
157
        public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
158
        {
NEW
159
                var_dump('REMOVE 2');
×
NEW
160
                $findPoliciesQuery = new Queries\FindPolicies();
×
NEW
161
                $findPoliciesQuery->byType(PolicyType::from($ptype));
×
162

NEW
163
                foreach (range(0, 5) as $value) {
×
NEW
164
                        if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
×
NEW
165
                                if ($fieldValues[$value - $fieldIndex] != '') {
×
NEW
166
                                        $findPoliciesQuery->byValue(intval($value), $fieldValues[$value - $fieldIndex]);
×
167
                                }
168
                        }
169
                }
170

NEW
171
                $policies = $this->policiesRepository->findAllBy($findPoliciesQuery);
×
172

NEW
173
                foreach ($policies as $policy) {
×
NEW
174
                        $this->policiesManager->delete($policy);
×
175
                }
176
        }
177

178
}
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