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

FastyBird / simple-auth / 10026986032

21 Jul 2024 08:32AM UTC coverage: 67.871% (-1.0%) from 68.883%
10026986032

push

github

web-flow
Enable cache + added file fallback (#10)

13 of 29 new or added lines in 3 files covered. (44.83%)

1 existing line in 1 file now uncovered.

526 of 775 relevant lines covered (67.87%)

4.74 hits per line

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

51.61
/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 Doctrine\DBAL;
21
use FastyBird\SimpleAuth\Exceptions;
22
use FastyBird\SimpleAuth\Models;
23
use FastyBird\SimpleAuth\Queries;
24
use FastyBird\SimpleAuth\Types\PolicyType;
25
use IPub\DoctrineCrud\Exceptions as DoctrineCrudExceptions;
26
use Nette\Utils\ArrayHash;
27
use TypeError;
28
use ValueError;
29
use function array_filter;
30
use function count;
31
use function implode;
32
use function intval;
33
use function is_file;
34
use function range;
35
use function strval;
36
use function trim;
37

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

48
        use CasbinPersist\AdapterHelper;
49

50
        private CasbinPersist\Adapter|null $fallback;
51

52
        private bool $useFallback = false;
53

54
        public function __construct(
55
                private readonly Models\Policies\Repository $policiesRepository,
56
                private readonly Models\Policies\Manager $policiesManager,
57
                string|null $policyFile = null,
58
        )
59
        {
60
                $this->fallback = $policyFile !== null && is_file($policyFile)
3✔
61
                        ? new CasbinPersist\Adapters\FileAdapter($policyFile)
3✔
NEW
62
                        : null;
×
63
        }
64

65
        /**
66
         * @param array<string, string|null> $rule
67
         *
68
         * @throws TypeError
69
         * @throws ValueError
70
         */
71
        public function savePolicyLine(string $ptype, array $rule): void
72
        {
73
                $data = [
3✔
74
                        'type' => PolicyType::from($ptype),
3✔
75
                ];
3✔
76

77
                foreach ($rule as $key => $value) {
3✔
78
                        $data['v' . strval($key)] = $value;
3✔
79
                }
80

81
                $this->policiesManager->create(ArrayHash::from($data));
3✔
82
        }
83

84
        /**
85
         * @throws Exceptions\InvalidState
86
         */
87
        public function loadPolicy(CasbinModel\Model $model): void
88
        {
89
                try {
90
                        $findPoliciesQuery = new Queries\FindPolicies();
3✔
91

92
                        $policies = $this->policiesRepository->findAllBy($findPoliciesQuery);
3✔
NEW
93
                } catch (DBAL\Driver\Exception) {
×
NEW
94
                        $this->fallback?->loadPolicy($model);
×
95

NEW
96
                        $this->useFallback = true;
×
97

NEW
98
                        return;
×
99
                }
100

101
                foreach ($policies as $policy) {
3✔
102
                        $data = [
3✔
103
                                $policy->getType()->value,
3✔
104
                                $policy->getV0(),
3✔
105
                                $policy->getV1(),
3✔
106
                                $policy->getV2(),
3✔
107
                                $policy->getV3(),
3✔
108
                                $policy->getV4(),
3✔
109
                                $policy->getV5(),
3✔
110
                        ];
3✔
111

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

114
                        $this->loadPolicyLine(trim($line), $model);
3✔
115
                }
116
        }
117

118
        /**
119
         * @throws TypeError
120
         * @throws ValueError
121
         */
122
        public function savePolicy(CasbinModel\Model $model): void
123
        {
NEW
124
                if ($this->useFallback) {
×
NEW
125
                        $this->fallback?->savePolicy($model);
×
126

NEW
127
                        return;
×
128
                }
129

130
                foreach ($model['p'] ?? [] as $type => $ast) {
×
131
                        foreach ($ast->policy as $rule) {
×
132
                                $this->savePolicyLine($type, $rule);
×
133
                        }
134
                }
135

136
                foreach ($model['g'] ?? [] as $type => $ast) {
×
137
                        foreach ($ast->policy as $rule) {
×
138
                                $this->savePolicyLine($type, $rule);
×
139
                        }
140
                }
141
        }
142

143
        /**
144
         * @throws TypeError
145
         * @throws ValueError
146
         */
147
        public function addPolicy(string $sec, string $ptype, array $rule): void
148
        {
149
                if ($this->useFallback) {
3✔
NEW
150
                        $this->fallback?->addPolicy($sec, $ptype, $rule);
×
151

NEW
152
                        return;
×
153
                }
154

155
                $this->savePolicyLine($ptype, $rule);
3✔
156
        }
157

158
        /**
159
         * @throws DoctrineCrudExceptions\InvalidArgumentException
160
         * @throws Exceptions\InvalidState
161
         * @throws TypeError
162
         * @throws ValueError
163
         */
164
        public function removePolicy(string $sec, string $ptype, array $rule): void
165
        {
166
                if ($this->useFallback) {
3✔
NEW
167
                        $this->fallback?->removePolicy($sec, $ptype, $rule);
×
168

NEW
169
                        return;
×
170
                }
171

172
                $findPoliciesQuery = new Queries\FindPolicies();
3✔
173
                $findPoliciesQuery->byType(PolicyType::from($ptype));
3✔
174

175
                foreach ($rule as $key => $value) {
3✔
176
                        $findPoliciesQuery->byValue(intval($key), $value);
3✔
177
                }
178

179
                $policies = $this->policiesRepository->findAllBy($findPoliciesQuery);
3✔
180

181
                foreach ($policies as $policy) {
3✔
182
                        $this->policiesManager->delete($policy);
3✔
183
                }
184
        }
185

186
        /**
187
         * @throws DoctrineCrudExceptions\InvalidArgumentException
188
         * @throws Exceptions\InvalidState
189
         * @throws TypeError
190
         * @throws ValueError
191
         */
192
        public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
193
        {
NEW
194
                if ($this->useFallback) {
×
NEW
195
                        $this->fallback?->removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
×
196

NEW
197
                        return;
×
198
                }
199

200
                $findPoliciesQuery = new Queries\FindPolicies();
×
201
                $findPoliciesQuery->byType(PolicyType::from($ptype));
×
202

203
                foreach (range(0, 5) as $value) {
×
204
                        if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
×
205
                                if ($fieldValues[$value - $fieldIndex] != '') {
×
206
                                        $findPoliciesQuery->byValue(intval($value), $fieldValues[$value - $fieldIndex]);
×
207
                                }
208
                        }
209
                }
210

211
                $policies = $this->policiesRepository->findAllBy($findPoliciesQuery);
×
212

213
                foreach ($policies as $policy) {
×
214
                        $this->policiesManager->delete($policy);
×
215
                }
216
        }
217

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