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

FastyBird / simple-auth / 10030816951

21 Jul 2024 07:23PM UTC coverage: 69.279% (+0.3%) from 68.983%
10030816951

push

github

web-flow
Move enforcer instance into factory (#12)

46 of 50 new or added lines in 4 files covered. (92.0%)

1 existing line in 1 file now uncovered.

548 of 791 relevant lines covered (69.28%)

6.03 hits per line

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

87.5
/src/DI/SimpleAuthExtension.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * SimpleAuthExtension.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     DI
11
 * @since          0.1.0
12
 *
13
 * @date           09.07.20
14
 */
15

16
namespace FastyBird\SimpleAuth\DI;
17

18
use Casbin;
19
use Doctrine\Persistence;
20
use FastyBird\SimpleAuth;
21
use FastyBird\SimpleAuth\Events;
22
use FastyBird\SimpleAuth\Exceptions;
23
use FastyBird\SimpleAuth\Mapping;
24
use FastyBird\SimpleAuth\Middleware;
25
use FastyBird\SimpleAuth\Security;
26
use FastyBird\SimpleAuth\Subscribers;
27
use Nette;
28
use Nette\Application as NetteApplication;
29
use Nette\DI;
30
use Nette\PhpGenerator;
31
use Nette\Schema;
32
use stdClass;
33
use Symfony\Contracts\EventDispatcher;
34
use function assert;
35
use function is_file;
36
use function is_string;
37
use const DIRECTORY_SEPARATOR;
38

39
/**
40
 * Authentication helpers extension container
41
 *
42
 * @package        FastyBird:SimpleAuth!
43
 * @subpackage     DI
44
 *
45
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
46
 */
47
class SimpleAuthExtension extends DI\CompilerExtension
48
{
49

50
        public static function register(
51
                Nette\Bootstrap\Configurator $config,
52
                string $extensionName = 'fbSimpleAuth',
53
        ): void
54
        {
55
                $config->onCompile[] = static function (Nette\Bootstrap\Configurator $config, DI\Compiler $compiler) use ($extensionName): void {
36✔
56
                        $compiler->addExtension($extensionName, new self());
13✔
57
                };
36✔
58
        }
59

60
        public function getConfigSchema(): Schema\Schema
61
        {
62
                return Schema\Expect::structure([
13✔
63
                        'token' => Schema\Expect::structure([
13✔
64
                                'issuer' => Schema\Expect::string(),
13✔
65
                                'signature' => Schema\Expect::string('g3xHbkELpMD9LRqW4WmJkHL7kz2bdNYAQJyEuFVzR3k='),
13✔
66
                        ]),
13✔
67
                        'enable' => Schema\Expect::structure([
13✔
68
                                'middleware' => Schema\Expect::bool(false),
13✔
69
                                'doctrine' => Schema\Expect::structure([
13✔
70
                                        'mapping' => Schema\Expect::bool(false),
13✔
71
                                        'models' => Schema\Expect::bool(false),
13✔
72
                                ]),
13✔
73
                                'casbin' => Schema\Expect::structure([
13✔
74
                                        'database' => Schema\Expect::bool(false),
13✔
75
                                ]),
13✔
76
                                'nette' => Schema\Expect::structure([
13✔
77
                                        'application' => Schema\Expect::bool(false),
13✔
78
                                ]),
13✔
79
                        ]),
13✔
80
                        'application' => Schema\Expect::structure([
13✔
81
                                'signInUrl' => Schema\Expect::string(),
13✔
82
                                'homeUrl' => Schema\Expect::string('/'),
13✔
83
                        ]),
13✔
84
                        'services' => Schema\Expect::structure([
13✔
85
                                'identity' => Schema\Expect::bool(false),
13✔
86
                        ]),
13✔
87
                        'casbin' => Schema\Expect::structure([
13✔
88
                                'model' => Schema\Expect::string(
13✔
89
                                        // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
90
                                        __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'model.conf',
13✔
91
                                ),
13✔
92
                                'policy' => Schema\Expect::string(),
13✔
93
                        ]),
13✔
94
                ]);
13✔
95
        }
96

97
        /**
98
         * @throws Exceptions\Logical
99
         */
100
        public function loadConfiguration(): void
101
        {
102
                $builder = $this->getContainerBuilder();
13✔
103
                $configuration = $this->getConfig();
13✔
104
                assert($configuration instanceof stdClass);
105

106
                $builder->addDefinition($this->prefix('auth'), new DI\Definitions\ServiceDefinition())
13✔
107
                        ->setType(SimpleAuth\Auth::class);
13✔
108

109
                $builder->addDefinition($this->prefix('configuration'), new DI\Definitions\ServiceDefinition())
13✔
110
                        ->setType(SimpleAuth\Configuration::class)
13✔
111
                        ->setArguments([
13✔
112
                                'tokenIssuer' => $configuration->token->issuer,
13✔
113
                                'tokenSignature' => $configuration->token->signature,
13✔
114
                                'enableMiddleware' => $configuration->enable->middleware,
13✔
115
                                'enableDoctrineMapping' => $configuration->enable->doctrine->mapping,
13✔
116
                                'enableDoctrineModels' => $configuration->enable->doctrine->models,
13✔
117
                                'enableNetteApplication' => $configuration->enable->nette->application,
13✔
118
                                'applicationSignInUrl' => $configuration->application->signInUrl,
13✔
119
                                'applicationHomeUrl' => $configuration->application->homeUrl,
13✔
120
                        ]);
13✔
121

122
                /**
123
                 * Token utilities
124
                 */
125

126
                $builder->addDefinition($this->prefix('token.builder'), new DI\Definitions\ServiceDefinition())
13✔
127
                        ->setType(Security\TokenBuilder::class)
13✔
128
                        ->setArgument('tokenSignature', $configuration->token->signature)
13✔
129
                        ->setArgument('tokenIssuer', $configuration->token->issuer);
13✔
130

131
                $builder->addDefinition($this->prefix('token.reader'), new DI\Definitions\ServiceDefinition())
13✔
132
                        ->setType(Security\TokenReader::class);
13✔
133

134
                $builder->addDefinition($this->prefix('token.validator'), new DI\Definitions\ServiceDefinition())
13✔
135
                        ->setType(Security\TokenValidator::class)
13✔
136
                        ->setArgument('tokenSignature', $configuration->token->signature)
13✔
137
                        ->setArgument('tokenIssuer', $configuration->token->issuer);
13✔
138

139
                /**
140
                 * User security
141
                 */
142

143
                if ($configuration->services->identity) {
13✔
144
                        $builder->addDefinition($this->prefix('security.identityFactory'), new DI\Definitions\ServiceDefinition())
13✔
145
                                ->setType(Security\IdentityFactory::class);
13✔
146
                }
147

148
                $builder->addDefinition($this->prefix('security.userStorage'), new DI\Definitions\ServiceDefinition())
13✔
149
                        ->setType(Security\UserStorage::class);
13✔
150

151
                $builder->addDefinition($this->prefix('security.annotationChecker'), new DI\Definitions\ServiceDefinition())
13✔
152
                        ->setType(Security\AnnotationChecker::class);
13✔
153

154
                $builder->addDefinition($this->prefix('security.user'), new DI\Definitions\ServiceDefinition())
13✔
155
                        ->setType(Security\User::class);
13✔
156

157
                /**
158
                 * Casbin
159
                 */
160

161
                if ($configuration->enable->casbin->database) {
13✔
162
                        $adapter = $builder->addDefinition(
8✔
163
                                $this->prefix('casbin.adapter'),
8✔
164
                                new DI\Definitions\ServiceDefinition(),
8✔
165
                        )
8✔
166
                                ->setType(SimpleAuth\Models\Casbin\Adapter::class)
8✔
167
                                ->setArguments([
8✔
168
                                        'policyFile' => $configuration->casbin->policy,
8✔
169
                                ]);
8✔
170

171
                        $builder->addDefinition($this->prefix('casbin.subscriber'), new DI\Definitions\ServiceDefinition())
8✔
172
                                ->setType(Subscribers\Policy::class);
8✔
173
                } else {
174
                        $policyFile = $configuration->casbin->policy;
5✔
175

176
                        if (!is_string($policyFile) || !is_file($policyFile)) {
5✔
NEW
177
                                throw new Exceptions\Logical('Casbin policy file is not configured');
×
178
                        }
179

180
                        $adapter = $builder->addDefinition($this->prefix('casbin.adapter'), new DI\Definitions\ServiceDefinition())
5✔
181
                                ->setType(Casbin\Persist\Adapters\FileAdapter::class)
5✔
182
                                ->setArguments([
5✔
183
                                        'filePath' => $policyFile,
5✔
184
                                ]);
5✔
185
                }
186

187
                $modelFile = $configuration->casbin->model;
13✔
188

189
                if (!is_string($modelFile) || !is_file($modelFile)) {
13✔
NEW
190
                        throw new Exceptions\Logical('Casbin model file is not configured');
×
191
                }
192

193
                $builder->addDefinition($this->prefix('casbin.enforcerFactory'), new DI\Definitions\ServiceDefinition())
13✔
194
                        ->setType(Security\EnforcerFactory::class)
13✔
195
                        ->setArguments([
13✔
196
                                'modelFile' => $modelFile,
13✔
197
                                'adapter' => $adapter,
13✔
198
                        ]);
13✔
199

200
                /**
201
                 * Web server extension
202
                 */
203

204
                if ($configuration->enable->middleware) {
13✔
205
                        $builder->addDefinition($this->prefix('middleware.access'), new DI\Definitions\ServiceDefinition())
13✔
206
                                ->setType(Middleware\Access::class);
13✔
207

208
                        $builder->addDefinition($this->prefix('middleware.user'), new DI\Definitions\ServiceDefinition())
13✔
209
                                ->setType(Middleware\User::class);
13✔
210
                }
211

212
                /**
213
                 * Doctrine extension
214
                 */
215

216
                if ($configuration->enable->doctrine->mapping) {
13✔
217
                        $builder->addDefinition($this->prefix('doctrine.driver'), new DI\Definitions\ServiceDefinition())
13✔
218
                                ->setType(Mapping\Driver\Owner::class);
13✔
219

220
                        $builder->addDefinition($this->prefix('doctrine.subscriber'), new DI\Definitions\ServiceDefinition())
13✔
221
                                ->setType(Subscribers\User::class);
13✔
222
                }
223

224
                if ($configuration->enable->doctrine->models) {
13✔
225
                        $builder->addDefinition($this->prefix('doctrine.tokensRepository'), new DI\Definitions\ServiceDefinition())
13✔
226
                                ->setType(SimpleAuth\Models\Tokens\Repository::class);
13✔
227

228
                        $builder->addDefinition($this->prefix('doctrine.tokensManager'), new DI\Definitions\ServiceDefinition())
13✔
229
                                ->setType(SimpleAuth\Models\Tokens\Manager::class);
13✔
230
                }
231

232
                if ($configuration->enable->casbin->database) {
13✔
233
                        $builder->addDefinition(
8✔
234
                                $this->prefix('doctrine.policiesRepository'),
8✔
235
                                new DI\Definitions\ServiceDefinition(),
8✔
236
                        )
8✔
237
                                ->setType(SimpleAuth\Models\Policies\Repository::class);
8✔
238

239
                        $builder->addDefinition($this->prefix('doctrine.policiesManager'), new DI\Definitions\ServiceDefinition())
8✔
240
                                ->setType(SimpleAuth\Models\Policies\Manager::class);
8✔
241
                }
242

243
                /**
244
                 * Nette application extension
245
                 */
246

247
                if ($configuration->enable->nette->application) {
13✔
248
                        $builder->addDefinition($this->prefix('nette.application'), new DI\Definitions\ServiceDefinition())
×
249
                                ->setType(Subscribers\Application::class);
×
250
                }
251
        }
252

253
        /**
254
         * @throws DI\MissingServiceException
255
         */
256
        public function beforeCompile(): void
257
        {
258
                parent::beforeCompile();
13✔
259

260
                $builder = $this->getContainerBuilder();
13✔
261
                $configuration = $this->getConfig();
13✔
262
                assert($configuration instanceof stdClass);
263

264
                /**
265
                 * Doctrine extension
266
                 */
267

268
                if (
269
                        $configuration->enable->doctrine->models
13✔
270
                        || $configuration->enable->casbin->database
13✔
271
                ) {
272
                        $ormAttributeDriverService = $builder->getDefinition('nettrineOrmAttributes.attributeDriver');
13✔
273

274
                        if ($ormAttributeDriverService instanceof DI\Definitions\ServiceDefinition) {
13✔
275
                                $ormAttributeDriverService->addSetup(
13✔
276
                                        'addPaths',
13✔
277
                                        [[__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Entities']],
13✔
278
                                );
13✔
279
                        }
280

281
                        $ormAttributeDriverChainService = $builder->getDefinitionByType(
13✔
282
                                Persistence\Mapping\Driver\MappingDriverChain::class,
13✔
283
                        );
13✔
284

285
                        if ($ormAttributeDriverChainService instanceof DI\Definitions\ServiceDefinition) {
13✔
286
                                $ormAttributeDriverChainService->addSetup('addDriver', [
13✔
287
                                        $ormAttributeDriverService,
13✔
288
                                        'FastyBird\SimpleAuth\Entities',
13✔
289
                                ]);
13✔
290
                        }
291
                }
292

293
                /**
294
                 * Nette application extension
295
                 */
296

297
                if ($configuration->enable->nette->application) {
13✔
298
                        if ($builder->getByType(EventDispatcher\EventDispatcherInterface::class) !== null) {
×
299
                                if ($builder->getByType(NetteApplication\Application::class) !== null) {
×
300
                                        $dispatcher = $builder->getDefinition(
×
301
                                                $builder->getByType(EventDispatcher\EventDispatcherInterface::class),
×
302
                                        );
×
303

304
                                        $application = $builder->getDefinition($builder->getByType(NetteApplication\Application::class));
×
305
                                        assert($application instanceof DI\Definitions\ServiceDefinition);
306

307
                                        $application->addSetup('?->onRequest[] = function() {?->dispatch(new ?(...func_get_args()));}', [
×
308
                                                '@self',
×
309
                                                $dispatcher,
×
310
                                                new PhpGenerator\Literal(Events\Request::class),
×
311
                                        ]);
×
312

313
                                        $application->addSetup('?->onResponse[] = function() {?->dispatch(new ?(...func_get_args()));}', [
×
314
                                                '@self',
×
315
                                                $dispatcher,
×
316
                                                new PhpGenerator\Literal(Events\Response::class),
×
317
                                        ]);
×
318
                                }
319
                        }
320
                }
321
        }
322

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