|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* Copyright 2011 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\Metadata\Driver; |
|
|
20 |
|
|
|
21 |
use JMS\SecurityExtraBundle\Annotation\RunAs; |
|
|
22 |
use JMS\SecurityExtraBundle\Annotation\SatisfiesParentSecurityPolicy; |
|
|
23 |
use JMS\SecurityExtraBundle\Annotation\SecureReturn; |
|
|
24 |
use JMS\SecurityExtraBundle\Annotation\SecureParam; |
|
|
25 |
use JMS\SecurityExtraBundle\Annotation\Secure; |
|
|
26 |
use JMS\SecurityExtraBundle\Metadata\MethodMetadata; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Converts annotations to method metadata |
|
|
30 |
* |
|
|
31 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
|
32 |
*/ |
|
|
33 |
class AnnotationConverter |
|
|
34 |
{ |
|
|
35 |
public function convertMethodAnnotations(\ReflectionMethod $method, array $annotations) |
|
|
36 |
{ |
|
|
37 |
$parameters = array(); |
|
|
38 |
foreach ($method->getParameters() as $index => $parameter) { |
|
|
39 |
$parameters[$parameter->getName()] = $index; |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
$methodMetadata = new MethodMetadata($method->getDeclaringClass()->getName(), $method->getName()); |
|
|
43 |
$hasSecurityMetadata = false; |
|
|
44 |
foreach ($annotations as $annotation) { |
|
|
45 |
if ($annotation instanceof Secure) { |
|
|
46 |
$methodMetadata->roles = $annotation->roles; |
|
|
47 |
$hasSecurityMetadata = true; |
|
|
48 |
} else if ($annotation instanceof SecureParam) { |
|
|
49 |
if (!isset($parameters[$annotation->name])) { |
|
|
50 |
throw new \InvalidArgumentException(sprintf('The parameter "%s" does not exist for method "%s".', $annotation->name, $method->getName())); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
$methodMetadata->addParamPermissions($parameters[$annotation->name], $annotation->permissions); |
|
|
54 |
$hasSecurityMetadata = true; |
|
|
55 |
} else if ($annotation instanceof SecureReturn) { |
|
|
56 |
$methodMetadata->returnPermissions = $annotation->permissions; |
|
|
57 |
$hasSecurityMetadata = true; |
|
|
58 |
} else if ($annotation instanceof SatisfiesParentSecurityPolicy) { |
|
|
59 |
$methodMetadata->satisfiesParentSecurityPolicy = true; |
|
|
60 |
$hasSecurityMetadata = true; |
|
|
61 |
} else if ($annotation instanceof RunAs) { |
|
|
62 |
$methodMetadata->runAsRoles = $annotation->roles; |
|
|
63 |
$hasSecurityMetadata = true; |
|
|
64 |
} |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
return $hasSecurityMetadata ? $methodMetadata : null; |
|
|
68 |
} |
|
|
69 |
} |