Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
e2c48152
Commit
e2c48152
authored
Feb 26, 2018
by
Haralan Dobrev
Committed by
Fabien Potencier
Feb 26, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use ServiceIterator in Security provider for voters
parent
eb559d4c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
5 deletions
+47
-5
doc/providers/security.rst
doc/providers/security.rst
+26
-0
src/Silex/Provider/SecurityServiceProvider.php
src/Silex/Provider/SecurityServiceProvider.php
+21
-5
No files found.
doc/providers/security.rst
View file @
e2c48152
...
@@ -681,6 +681,32 @@ Symfony `cookbook`_.
...
@@ -681,6 +681,32 @@ Symfony `cookbook`_.
providers. :doc:`How to Create a Custom Authentication System with Guard
providers. :doc:`How to Create a Custom Authentication System with Guard
</cookbook/guard_authentication>`
</cookbook/guard_authentication>`
Using Voters to check user permissions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
See the `Security component documentation on voters <http://symfony.com/doc/current/security/voters.html>`_.
By default Silex includes the role hierarchy and authenticated voters.
If you want to add a custom voter, you need to register it as a service and extend ``security.voter_services``.
.. code-block:: php
$app['custom_voter'] = function () {
return MyCustomVoter();
};
$app->extend('security.voter_services', function (array $voters) {
$voters[] = 'custom_voter';
return $voters;
});
.. note::
Using the above approach with the service names circular references are avoided
and you can use the ``AccessDecisionManager`` in your custom voter
to `check for roles inside a voter
<http://symfony.com/doc/current/security/voters.html#checking-for-roles-inside-a-voter>`_.
Stateless Authentication
Stateless Authentication
~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~
...
...
src/Silex/Provider/SecurityServiceProvider.php
View file @
e2c48152
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
namespace
Silex\Provider
;
namespace
Silex\Provider
;
use
Pimple\Container
;
use
Pimple\Container
;
use
Pimple\ServiceIterator
;
use
Pimple\ServiceProviderInterface
;
use
Pimple\ServiceProviderInterface
;
use
Silex\Application
;
use
Silex\Application
;
use
Silex\Api\BootableProviderInterface
;
use
Silex\Api\BootableProviderInterface
;
...
@@ -140,14 +141,29 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
...
@@ -140,14 +141,29 @@ class SecurityServiceProvider implements ServiceProviderInterface, EventListener
};
};
$app
[
'security.access_manager'
]
=
function
(
$app
)
{
$app
[
'security.access_manager'
]
=
function
(
$app
)
{
return
new
AccessDecisionManager
(
$app
[
'security.voters'
]);
$votersIterator
=
new
ServiceIterator
(
$app
,
$app
[
'security.voter_services'
]);
return
new
AccessDecisionManager
(
$votersIterator
);
};
$app
[
'security.voter_services'
]
=
function
()
{
return
[
RoleHierarchyVoter
::
class
,
AuthenticatedVoter
::
class
];
};
$app
[
RoleHierarchyVoter
::
class
]
=
function
(
$app
)
{
return
new
RoleHierarchyVoter
(
new
RoleHierarchy
(
$app
[
'security.role_hierarchy'
]));
};
};
$app
[
AuthenticatedVoter
::
class
]
=
function
(
$app
)
{
return
new
AuthenticatedVoter
(
$app
[
'security.trust_resolver'
]);
};
// Unused, kept for backwards-compatibility
// Extend security.voter_services instead to prevent circular references
$app
[
'security.voters'
]
=
function
(
$app
)
{
$app
[
'security.voters'
]
=
function
(
$app
)
{
return
[
return
array_map
(
function
(
$voterServiceId
)
use
(
$app
)
{
new
RoleHierarchyVoter
(
new
RoleHierarchy
(
$app
[
'security.role_hierarchy'
])),
return
$app
[
$voterServiceId
];
new
AuthenticatedVoter
(
$app
[
'security.trust_resolver'
]),
});
];
};
};
$app
[
'security.firewall'
]
=
function
(
$app
)
{
$app
[
'security.firewall'
]
=
function
(
$app
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment