-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathSecurityExtension.php
More file actions
114 lines (91 loc) · 3.38 KB
/
Copy pathSecurityExtension.php
File metadata and controls
114 lines (91 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\SecurityDI;
use Nette;
use Nette\Schema\Expect;
/**
* Security extension for Nette DI.
*/
class SecurityExtension extends Nette\DI\CompilerExtension
{
/** @var bool */
private $debugMode;
public function __construct(bool $debugMode = false)
{
$this->debugMode = $debugMode;
}
public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::structure([
'debugger' => Expect::bool(interface_exists(\Tracy\IBarPanel::class)),
'users' => Expect::arrayOf(
Expect::anyOf(
Expect::string(), // user => password
Expect::structure([ // user => password + roles + data
'password' => Expect::string(),
'roles' => Expect::anyOf(Expect::string(), Expect::listOf('string')),
'data' => Expect::array(),
])->castTo('array')
)
),
'roles' => Expect::arrayOf('string|array|null'), // role => parent(s)
'resources' => Expect::arrayOf('string|null'), // resource => parent
]);
}
public function loadConfiguration()
{
/** @var object{debugger: bool, users: array, roles: array, resources: array} $config */
$config = $this->config;
$builder = $this->getContainerBuilder();
$builder->addDefinition($this->prefix('passwords'))
->setFactory(Nette\Security\Passwords::class);
$builder->addDefinition($this->prefix('userStorage'))
->setType(Nette\Security\IUserStorage::class)
->setFactory(Nette\Http\UserStorage::class);
$user = $builder->addDefinition($this->prefix('user'))
->setFactory(Nette\Security\User::class);
if ($this->debugMode && $config->debugger) {
$user->addSetup('@Tracy\Bar::addPanel', [
new Nette\DI\Definitions\Statement(Nette\Bridges\SecurityTracy\UserPanel::class),
]);
}
if ($config->users) {
$usersList = $usersRoles = $usersData = [];
foreach ($config->users as $username => $data) {
$data = is_array($data) ? $data : ['password' => $data];
$this->validateConfig(['password' => null, 'roles' => null, 'data' => []], $data, $this->prefix("security.users.$username"));
$usersList[$username] = $data['password'];
$usersRoles[$username] = $data['roles'] ?? null;
$usersData[$username] = $data['data'] ?? [];
}
$builder->addDefinition($this->prefix('authenticator'))
->setType(Nette\Security\IAuthenticator::class)
->setFactory(Nette\Security\SimpleAuthenticator::class, [$usersList, $usersRoles, $usersData]);
if ($this->name === 'security') {
$builder->addAlias('nette.authenticator', $this->prefix('authenticator'));
}
}
if ($config->roles || $config->resources) {
$authorizator = $builder->addDefinition($this->prefix('authorizator'))
->setType(Nette\Security\IAuthorizator::class)
->setFactory(Nette\Security\Permission::class);
foreach ($config->roles as $role => $parents) {
$authorizator->addSetup('addRole', [$role, $parents]);
}
foreach ($config->resources as $resource => $parents) {
$authorizator->addSetup('addResource', [$resource, $parents]);
}
if ($this->name === 'security') {
$builder->addAlias('nette.authorizator', $this->prefix('authorizator'));
}
}
if ($this->name === 'security') {
$builder->addAlias('user', $this->prefix('user'));
$builder->addAlias('nette.userStorage', $this->prefix('userStorage'));
}
}
}