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

FastyBird / simple-auth / 10030791279

21 Jul 2024 07:23PM UTC coverage: 69.153% (+0.2%) from 68.983%
10030791279

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.

547 of 791 relevant lines covered (69.15%)

5.68 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());
11✔
57
                };
36✔
58
        }
59

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

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

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

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

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

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

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

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

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

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

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

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

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

157
                /**
158
                 * Casbin
159
                 */
160

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

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

176
                        if (!is_string($policyFile) || !is_file($policyFile)) {
4✔
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())
4✔
181
                                ->setType(Casbin\Persist\Adapters\FileAdapter::class)
4✔
182
                                ->setArguments([
4✔
183
                                        'filePath' => $policyFile,
4✔
184
                                ]);
4✔
185
                }
186

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

189
                if (!is_string($modelFile) || !is_file($modelFile)) {
11✔
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())
11✔
194
                        ->setType(Security\EnforcerFactory::class)
11✔
195
                        ->setArguments([
11✔
196
                                'modelFile' => $modelFile,
11✔
197
                                'adapter' => $adapter,
11✔
198
                        ]);
11✔
199

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

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

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

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

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

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

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

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

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

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

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

247
                if ($configuration->enable->nette->application) {
11✔
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();
11✔
259

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

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

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

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

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

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

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

297
                if ($configuration->enable->nette->application) {
11✔
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