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
6d86c60d
Commit
6d86c60d
authored
Dec 25, 2014
by
freepius
Committed by
Fabien Potencier
Apr 11, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a isGranted() function to Silex\Application\SecurityTrait + unit tests
parent
04885557
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
src/Silex/Application/SecurityTrait.php
src/Silex/Application/SecurityTrait.php
+16
-0
tests/Silex/Tests/Application/SecurityTraitTest.php
tests/Silex/Tests/Application/SecurityTraitTest.php
+36
-0
No files found.
src/Silex/Application/SecurityTrait.php
View file @
6d86c60d
...
...
@@ -12,6 +12,7 @@
namespace
Silex\Application
;
use
Symfony\Component\Security\Core\Authentication\Token\TokenInterface
;
use
Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
;
use
Symfony\Component\Security\Core\User\UserInterface
;
/**
...
...
@@ -55,4 +56,19 @@ trait SecurityTrait
{
return
$this
[
'security.encoder_factory'
]
->
getEncoder
(
$user
)
->
encodePassword
(
$password
,
$user
->
getSalt
());
}
/**
* Checks if the attributes are granted against the current authentication token and optionally supplied object.
*
* @param mixed $attributes
* @param mixed $object
*
* @return bool
*
* @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token.
*/
public
function
isGranted
(
$attributes
,
$object
=
null
)
{
return
$this
[
'security.authorization_checker'
]
->
isGranted
(
$attributes
,
$object
);
}
}
tests/Silex/Tests/Application/SecurityTraitTest.php
View file @
6d86c60d
...
...
@@ -76,6 +76,42 @@ class SecurityTraitTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
'5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='
,
$app
->
encodePassword
(
$user
,
'foo'
));
}
/**
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
*/
public
function
testIsGrantedWithoutTokenThrowsException
()
{
$app
=
$this
->
createApplication
();
$app
->
get
(
'/'
,
function
()
{
return
'foo'
;
});
$app
->
handle
(
Request
::
create
(
'/'
));
$app
->
isGranted
(
'ROLE_ADMIN'
);
}
public
function
testIsGranted
()
{
$request
=
Request
::
create
(
'/'
);
$app
=
$this
->
createApplication
(
array
(
'fabien'
=>
array
(
'ROLE_ADMIN'
,
'5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='
),
'monique'
=>
array
(
'ROLE_USER'
,
'5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='
),
));
$app
->
get
(
'/'
,
function
()
{
return
'foo'
;
});
// User is Monique (ROLE_USER)
$request
->
headers
->
set
(
'PHP_AUTH_USER'
,
'monique'
);
$request
->
headers
->
set
(
'PHP_AUTH_PW'
,
'foo'
);
$app
->
handle
(
$request
);
$this
->
assertTrue
(
$app
->
isGranted
(
'ROLE_USER'
));
$this
->
assertFalse
(
$app
->
isGranted
(
'ROLE_ADMIN'
));
// User is Fabien (ROLE_ADMIN)
$request
->
headers
->
set
(
'PHP_AUTH_USER'
,
'fabien'
);
$request
->
headers
->
set
(
'PHP_AUTH_PW'
,
'foo'
);
$app
->
handle
(
$request
);
$this
->
assertFalse
(
$app
->
isGranted
(
'ROLE_USER'
));
$this
->
assertTrue
(
$app
->
isGranted
(
'ROLE_ADMIN'
));
}
public
function
createApplication
(
$users
=
array
())
{
$app
=
new
SecurityApplication
();
...
...
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