|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* Copyright 2010 Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
5 |
* |
|
|
6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
7 |
* you may not use this file except in compliance with the License. |
|
|
8 |
* You may obtain a copy of the License at |
|
|
9 |
* |
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
|
11 |
* |
|
|
12 |
* Unless required by applicable law or agreed to in writing, software |
|
|
13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
|
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
15 |
* See the License for the specific language governing permissions and |
|
|
16 |
* limitations under the License. |
|
|
17 |
*/ |
|
|
18 |
|
|
|
19 |
namespace JMS\SecurityExtraBundle\Security\Authorization; |
|
|
20 |
|
|
|
21 |
use JMS\SecurityExtraBundle\Security\Authentication\Token\RunAsUserToken; |
|
|
22 |
use Symfony\Component\Security\Core\Role\Role; |
|
|
23 |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
|
24 |
|
|
|
25 |
/** |
|
|
26 |
* The RunAsManager creates throw-away Tokens which are temporarily injected into |
|
|
27 |
* the security context for the duration of the invocation of a specific method. |
|
|
28 |
* |
|
|
29 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
30 |
*/ |
|
|
31 |
class RunAsManager implements RunAsManagerInterface |
|
|
32 |
{ |
|
|
33 |
private $key; |
|
|
34 |
private $rolePrefix; |
|
|
35 |
|
|
|
36 |
public function __construct($key, $rolePrefix = 'ROLE_') |
|
|
37 |
{ |
|
|
38 |
$this->key = $key; |
|
|
39 |
$this->rolePrefix = $rolePrefix; |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
/** |
|
|
43 |
* {@inheritDoc} |
|
|
44 |
*/ |
|
|
45 |
public function buildRunAs(TokenInterface $token, $secureObject, array $attributes) |
|
|
46 |
{ |
|
|
47 |
$roles = array(); |
|
|
48 |
foreach ($attributes as $attribute) |
|
|
49 |
{ |
|
|
50 |
if ($this->supportsAttribute($attribute)) { |
|
|
51 |
$roles[] = new Role($attribute); |
|
|
52 |
} |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
if (0 === count($roles)) { |
|
|
56 |
return null; |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
$roles = array_merge($roles, $token->getRoles()); |
|
|
60 |
|
|
|
61 |
return new RunAsUserToken($this->key, $token->getUser(), $token->getCredentials(), $roles, $token); |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
/** |
|
|
65 |
* {@inheritDoc} |
|
|
66 |
*/ |
|
|
67 |
public function supportsAttribute($attribute) |
|
|
68 |
{ |
|
|
69 |
return !empty($attribute) && 0 === strpos($attribute, $this->rolePrefix); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
/** |
|
|
73 |
* {@inheritDoc} |
|
|
74 |
*/ |
|
|
75 |
public function supportsClass($className) |
|
|
76 |
{ |
|
|
77 |
return true; |
|
|
78 |
} |
|
|
79 |
} |