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

nette / security / 20546871958

28 Dec 2025 01:17AM UTC coverage: 84.79% (-0.1%) from 84.896%
20546871958

push

github

dg
User: deprecated magic properties (BC break)

485 of 572 relevant lines covered (84.79%)

0.85 hits per line

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

97.1
/src/Bridges/SecurityDI/SecurityExtension.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\Bridges\SecurityDI;
11

12
use Nette;
13
use Nette\Schema\Expect;
14
use Tracy;
15
use function is_array;
16

17

18
/**
19
 * Security extension for Nette DI.
20
 */
21
class SecurityExtension extends Nette\DI\CompilerExtension
22
{
23
        public function __construct(
1✔
24
                private readonly bool $debugMode = false,
25
        ) {
26
        }
1✔
27

28

29
        public function getConfigSchema(): Nette\Schema\Schema
30
        {
31
                return Expect::structure([
1✔
32
                        'debugger' => Expect::bool(),
1✔
33
                        'users' => Expect::arrayOf(
1✔
34
                                Expect::anyOf(
1✔
35
                                        Expect::string()->dynamic(), // user => password
1✔
36
                                        Expect::structure([ // user => password + roles + data
1✔
37
                                                'password' => Expect::string()->dynamic(),
1✔
38
                                                'roles' => Expect::anyOf(Expect::string(), Expect::listOf('string')),
1✔
39
                                                'data' => Expect::array(),
1✔
40
                                        ])->castTo('array'),
1✔
41
                                ),
42
                        ),
43
                        'roles' => Expect::arrayOf('string|array|null'), // role => parent(s)
1✔
44
                        'resources' => Expect::arrayOf('string|null'), // resource => parent
1✔
45
                        'authentication' => Expect::structure([
1✔
46
                                'storage' => Expect::anyOf('session', 'cookie')->default('session'),
1✔
47
                                'expiration' => Expect::string()->dynamic(),
1✔
48
                                'cookieName' => Expect::string(),
1✔
49
                                'cookieDomain' => Expect::string(),
1✔
50
                                'cookieSamesite' => Expect::anyOf('Lax', 'Strict', 'None'),
1✔
51
                        ]),
52
                ]);
53
        }
54

55

56
        public function loadConfiguration(): void
57
        {
58
                /** @var object{debugger: bool, users: array, roles: array, resources: array, authentication: \stdClass} $config */
59
                $config = $this->config;
1✔
60
                $builder = $this->getContainerBuilder();
1✔
61

62
                $builder->addDefinition($this->prefix('passwords'))
1✔
63
                        ->setFactory(Nette\Security\Passwords::class);
1✔
64

65
                $auth = $config->authentication;
1✔
66
                $storage = $builder->addDefinition($this->prefix('userStorage'))
1✔
67
                        ->setType(Nette\Security\UserStorage::class)
1✔
68
                        ->setFactory([
1✔
69
                                'session' => Nette\Bridges\SecurityHttp\SessionStorage::class,
70
                                'cookie' => Nette\Bridges\SecurityHttp\CookieStorage::class,
71
                        ][$auth->storage]);
1✔
72

73
                if ($auth->storage === 'cookie') {
1✔
74
                        if ($auth->cookieDomain === 'domain') {
1✔
75
                                $auth->cookieDomain = $builder::literal('$this->getByType(Nette\Http\IRequest::class)->getUrl()->getDomain(2)');
1✔
76
                        }
77

78
                        $storage->addSetup('setCookieParameters', [$auth->cookieName, $auth->cookieDomain, $auth->cookieSamesite]);
1✔
79
                }
80

81
                $user = $builder->addDefinition($this->prefix('user'))
1✔
82
                        ->setFactory(Nette\Security\User::class);
1✔
83

84
                if ($auth->expiration) {
1✔
85
                        $user->addSetup('setExpiration', [$auth->expiration]);
1✔
86
                }
87

88
                if ($config->users) {
1✔
89
                        $usersList = $usersRoles = $usersData = [];
1✔
90
                        foreach ($config->users as $username => $data) {
1✔
91
                                $data = is_array($data) ? $data : ['password' => $data];
1✔
92
                                $usersList[$username] = $data['password'];
1✔
93
                                $usersRoles[$username] = $data['roles'] ?? null;
1✔
94
                                $usersData[$username] = $data['data'] ?? [];
1✔
95
                        }
96

97
                        $builder->addDefinition($this->prefix('authenticator'))
1✔
98
                                ->setType(Nette\Security\Authenticator::class)
1✔
99
                                ->setFactory(Nette\Security\SimpleAuthenticator::class, [$usersList, $usersRoles, $usersData]);
1✔
100

101
                        if ($this->name === 'security') {
1✔
102
                                $builder->addAlias('nette.authenticator', $this->prefix('authenticator'));
1✔
103
                        }
104
                }
105

106
                if ($config->roles || $config->resources) {
1✔
107
                        $authorizator = $builder->addDefinition($this->prefix('authorizator'))
1✔
108
                                ->setType(Nette\Security\Authorizator::class)
1✔
109
                                ->setFactory(Nette\Security\Permission::class);
1✔
110

111
                        foreach ($config->roles as $role => $parents) {
1✔
112
                                $authorizator->addSetup('addRole', [$role, $parents]);
1✔
113
                        }
114

115
                        foreach ($config->resources as $resource => $parents) {
1✔
116
                                $authorizator->addSetup('addResource', [$resource, $parents]);
1✔
117
                        }
118

119
                        if ($this->name === 'security') {
1✔
120
                                $builder->addAlias('nette.authorizator', $this->prefix('authorizator'));
1✔
121
                        }
122
                }
123

124
                if ($this->name === 'security') {
1✔
125
                        $builder->addAlias('user', $this->prefix('user'));
1✔
126
                        $builder->addAlias('nette.userStorage', $this->prefix('userStorage'));
1✔
127
                }
128
        }
1✔
129

130

131
        public function beforeCompile(): void
132
        {
133
                $builder = $this->getContainerBuilder();
1✔
134

135
                if (
136
                        $this->debugMode &&
1✔
137
                        ($this->config->debugger ?? $builder->getByType(Tracy\Bar::class))
1✔
138
                ) {
139
                        $builder->getDefinition($this->prefix('user'))->addSetup('@Tracy\Bar::addPanel', [
×
140
                                new Nette\DI\Definitions\Statement(Nette\Bridges\SecurityTracy\UserPanel::class),
×
141
                        ]);
142
                }
143
        }
1✔
144
}
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

© 2026 Coveralls, Inc