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

FastyBird / simple-auth / 10030599651

21 Jul 2024 06:48PM UTC coverage: 68.983% (+1.1%) from 67.871%
10030599651

push

github

web-flow
User roles check as was before casbin (#11)

* User roles check as was before casbin

* Fix interface

* Use storage instead of user

* QA fixes

45 of 49 new or added lines in 4 files covered. (91.84%)

1 existing line in 1 file now uncovered.

536 of 777 relevant lines covered (68.98%)

5.97 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
        public function loadConfiguration(): void
98
        {
99
                $builder = $this->getContainerBuilder();
13✔
100
                $configuration = $this->getConfig();
13✔
101
                assert($configuration instanceof stdClass);
102

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

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

119
                /**
120
                 * Token utilities
121
                 */
122

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

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

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

136
                /**
137
                 * User security
138
                 */
139

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

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

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

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

154
                /**
155
                 * Web server extension
156
                 */
157

158
                if ($configuration->enable->middleware) {
13✔
159
                        $builder->addDefinition($this->prefix('middleware.access'), new DI\Definitions\ServiceDefinition())
13✔
160
                                ->setType(Middleware\Access::class);
13✔
161

162
                        $builder->addDefinition($this->prefix('middleware.user'), new DI\Definitions\ServiceDefinition())
13✔
163
                                ->setType(Middleware\User::class);
13✔
164
                }
165

166
                /**
167
                 * Doctrine extension
168
                 */
169

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

174
                        $builder->addDefinition($this->prefix('doctrine.subscriber'), new DI\Definitions\ServiceDefinition())
13✔
175
                                ->setType(Subscribers\User::class);
13✔
176
                }
177

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

182
                        $builder->addDefinition($this->prefix('doctrine.tokensManager'), new DI\Definitions\ServiceDefinition())
13✔
183
                                ->setType(SimpleAuth\Models\Tokens\Manager::class);
13✔
184
                }
185

186
                if ($configuration->enable->casbin->database) {
13✔
187
                        $builder->addDefinition(
8✔
188
                                $this->prefix('doctrine.policiesRepository'),
8✔
189
                                new DI\Definitions\ServiceDefinition(),
8✔
190
                        )
8✔
191
                                ->setType(SimpleAuth\Models\Policies\Repository::class);
8✔
192

193
                        $builder->addDefinition($this->prefix('doctrine.policiesManager'), new DI\Definitions\ServiceDefinition())
8✔
194
                                ->setType(SimpleAuth\Models\Policies\Manager::class);
8✔
195
                }
196

197
                /**
198
                 * Nette application extension
199
                 */
200

201
                if ($configuration->enable->nette->application) {
13✔
202
                        $builder->addDefinition($this->prefix('nette.application'), new DI\Definitions\ServiceDefinition())
×
203
                                ->setType(Subscribers\Application::class);
×
204
                }
205
        }
206

207
        /**
208
         * @throws DI\MissingServiceException
209
         * @throws Exceptions\Logical
210
         */
211
        public function beforeCompile(): void
212
        {
213
                parent::beforeCompile();
13✔
214

215
                $builder = $this->getContainerBuilder();
13✔
216
                $configuration = $this->getConfig();
13✔
217
                assert($configuration instanceof stdClass);
218

219
                /**
220
                 * Casbin
221
                 */
222

223
                if ($configuration->enable->casbin->database) {
13✔
224
                        $adapter = $builder->addDefinition(
8✔
225
                                $this->prefix('casbin.adapter'),
8✔
226
                                new DI\Definitions\ServiceDefinition(),
8✔
227
                        )
8✔
228
                                ->setType(SimpleAuth\Models\Casbin\Adapter::class)
8✔
229
                                ->setArguments([
8✔
230
                                        'policyFile' => $configuration->casbin->policy,
8✔
231
                                ]);
8✔
232

233
                        $builder->addDefinition($this->prefix('casbin.subscriber'), new DI\Definitions\ServiceDefinition())
8✔
234
                                ->setType(Subscribers\Policy::class);
8✔
235
                } else {
236
                        $policyFile = $configuration->casbin->policy;
5✔
237

238
                        if (!is_string($policyFile) || !is_file($policyFile)) {
5✔
NEW
239
                                throw new Exceptions\Logical('Casbin policy file is not configured');
×
240
                        }
241

242
                        $adapter = $builder->addDefinition($this->prefix('casbin.adapter'), new DI\Definitions\ServiceDefinition())
5✔
243
                                ->setType(Casbin\Persist\Adapters\FileAdapter::class)
5✔
244
                                ->setArguments([
5✔
245
                                        'filePath' => $policyFile,
5✔
246
                                ]);
5✔
247
                }
248

249
                $modelFile = $configuration->casbin->model;
13✔
250

251
                if (!is_string($modelFile) || !is_file($modelFile)) {
13✔
NEW
252
                        throw new Exceptions\Logical('Casbin model file is not configured');
×
253
                }
254

255
                $builder->addDefinition($this->prefix('casbin.enforcer'), new DI\Definitions\ServiceDefinition())
13✔
256
                        ->setType(Casbin\CachedEnforcer::class)
13✔
257
                        ->setArguments([
13✔
258
                                $modelFile,
13✔
259
                                $adapter,
13✔
260
                        ]);
13✔
261

262
                /**
263
                 * Doctrine extension
264
                 */
265

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

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

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

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

291
                /**
292
                 * Nette application extension
293
                 */
294

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

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

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

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

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