Initial commit with Symfony 2.1+Vendors
Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
93
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/ClassMetadata.php
vendored
Normal file
93
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/ClassMetadata.php
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace JMS\SecurityExtraBundle\Metadata;
|
||||
|
||||
use JMS\SecurityExtraBundle\Exception\RuntimeException;
|
||||
use JMS\SecurityExtraBundle\Exception\InvalidArgumentException;
|
||||
use Metadata\MethodMetadata;
|
||||
use Metadata\MergeableInterface;
|
||||
use Metadata\MergeableClassMetadata;
|
||||
|
||||
/**
|
||||
* Contains class metadata information
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class ClassMetadata extends MergeableClassMetadata
|
||||
{
|
||||
public function addMethodMetadata(MethodMetadata $metadata)
|
||||
{
|
||||
if ($this->reflection->isFinal()) {
|
||||
throw new RuntimeException(sprintf('Class "%s" is declared final, and cannot be secured.', $reflection->name));
|
||||
}
|
||||
|
||||
if ($metadata->reflection->isStatic()) {
|
||||
throw new RuntimeException(sprintf('Method "%s::%s" is declared static and cannot be secured.', $metadata->reflection->class, $metadata->reflection->name));
|
||||
}
|
||||
|
||||
if ($metadata->reflection->isFinal()) {
|
||||
throw new RuntimeException(sprintf('Method "%s::%s" is declared final and cannot be secured.', $metadata->reflection->class, $metadata->reflection->name));
|
||||
}
|
||||
|
||||
parent::addMethodMetadata($metadata);
|
||||
}
|
||||
|
||||
public function merge(MergeableInterface $metadata)
|
||||
{
|
||||
if (!$metadata instanceof ClassMetadata) {
|
||||
throw new InvalidArgumentException('$metadata must be an instance of ClassMetadata.');
|
||||
}
|
||||
|
||||
foreach ($this->methodMetadata as $name => $methodMetadata) {
|
||||
// check if metadata was declared on an interface
|
||||
if (!$metadata->reflection->hasMethod($name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($metadata->reflection->getMethod($name)->getDeclaringClass()->name
|
||||
!== $methodMetadata->class) {
|
||||
if (!isset($metadata->methodMetadata[$name])) {
|
||||
if ($methodMetadata->reflection->isAbstract()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf(
|
||||
'You have overridden a secured method "%s::%s" in "%s". '
|
||||
.'Please copy over the applicable security metadata, and '
|
||||
.'also add @SatisfiesParentSecurityPolicy.',
|
||||
$methodMetadata->reflection->class,
|
||||
$name,
|
||||
$metadata->reflection->name
|
||||
));
|
||||
}
|
||||
|
||||
if (!$metadata->methodMetadata[$name]->satisfiesParentSecurityPolicy) {
|
||||
throw new RuntimeException(sprintf('Unresolved security metadata conflict for method "%s::%s" in "%s". Please copy the respective annotations, and add @SatisfiesParentSecurityPolicy to the child method.', $metadata->reflection->name, $name, $methodMetadata->reflection->getDeclaringClass()->getFilename()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
parent::merge($metadata);
|
||||
}
|
||||
|
||||
public function isProxyRequired()
|
||||
{
|
||||
return !empty($this->methodMetadata);
|
||||
}
|
||||
}
|
107
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/Driver/AnnotationDriver.php
vendored
Normal file
107
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/Driver/AnnotationDriver.php
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace JMS\SecurityExtraBundle\Metadata\Driver;
|
||||
|
||||
use JMS\SecurityExtraBundle\Exception\InvalidArgumentException;
|
||||
use Doctrine\Common\Annotations\Reader;
|
||||
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
|
||||
use JMS\SecurityExtraBundle\Annotation\RunAs;
|
||||
use JMS\SecurityExtraBundle\Annotation\SatisfiesParentSecurityPolicy;
|
||||
use JMS\SecurityExtraBundle\Annotation\Secure;
|
||||
use JMS\SecurityExtraBundle\Annotation\SecureParam;
|
||||
use JMS\SecurityExtraBundle\Annotation\SecureReturn;
|
||||
use JMS\SecurityExtraBundle\Metadata\ClassMetadata;
|
||||
use JMS\SecurityExtraBundle\Metadata\MethodMetadata;
|
||||
use Metadata\Driver\DriverInterface;
|
||||
use \ReflectionClass;
|
||||
use \ReflectionMethod;
|
||||
use JMS\SecurityExtraBundle\Security\Authorization\Expression\Expression;
|
||||
|
||||
/**
|
||||
* Loads security annotations and converts them to metadata
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class AnnotationDriver implements DriverInterface
|
||||
{
|
||||
private $reader;
|
||||
|
||||
public function __construct(Reader $reader)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
public function loadMetadataForClass(ReflectionClass $reflection)
|
||||
{
|
||||
$metadata = new ClassMetadata($reflection->getName());
|
||||
|
||||
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED) as $method) {
|
||||
// check if the method was defined on this class
|
||||
if ($method->getDeclaringClass()->getName() !== $reflection->getName()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$annotations = $this->reader->getMethodAnnotations($method);
|
||||
|
||||
if ($annotations && null !== $methodMetadata = $this->convertMethodAnnotations($method, $annotations)) {
|
||||
$metadata->addMethodMetadata($methodMetadata);
|
||||
}
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
private function convertMethodAnnotations(\ReflectionMethod $method, array $annotations)
|
||||
{
|
||||
$parameters = array();
|
||||
foreach ($method->getParameters() as $index => $parameter) {
|
||||
$parameters[$parameter->getName()] = $index;
|
||||
}
|
||||
|
||||
$methodMetadata = new MethodMetadata($method->getDeclaringClass()->getName(), $method->getName());
|
||||
$hasSecurityMetadata = false;
|
||||
foreach ($annotations as $annotation) {
|
||||
if ($annotation instanceof Secure) {
|
||||
$methodMetadata->roles = $annotation->roles;
|
||||
$hasSecurityMetadata = true;
|
||||
} else if ($annotation instanceof PreAuthorize) {
|
||||
$methodMetadata->roles = array(new Expression($annotation->expr));
|
||||
$hasSecurityMetadata = true;
|
||||
} else if ($annotation instanceof SecureParam) {
|
||||
if (!isset($parameters[$annotation->name])) {
|
||||
throw new InvalidArgumentException(sprintf('The parameter "%s" does not exist for method "%s".', $annotation->name, $method->getName()));
|
||||
}
|
||||
|
||||
$methodMetadata->addParamPermissions($parameters[$annotation->name], $annotation->permissions);
|
||||
$hasSecurityMetadata = true;
|
||||
} else if ($annotation instanceof SecureReturn) {
|
||||
$methodMetadata->returnPermissions = $annotation->permissions;
|
||||
$hasSecurityMetadata = true;
|
||||
} else if ($annotation instanceof SatisfiesParentSecurityPolicy) {
|
||||
$methodMetadata->satisfiesParentSecurityPolicy = true;
|
||||
$hasSecurityMetadata = true;
|
||||
} else if ($annotation instanceof RunAs) {
|
||||
$methodMetadata->runAsRoles = $annotation->roles;
|
||||
$hasSecurityMetadata = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $hasSecurityMetadata ? $methodMetadata : null;
|
||||
}
|
||||
}
|
100
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/Driver/ConfigDriver.php
vendored
Normal file
100
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/Driver/ConfigDriver.php
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace JMS\SecurityExtraBundle\Metadata\Driver;
|
||||
|
||||
use JMS\SecurityExtraBundle\Security\Authorization\Expression\Expression;
|
||||
use JMS\SecurityExtraBundle\Metadata\MethodMetadata;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
use JMS\SecurityExtraBundle\Metadata\ClassMetadata;
|
||||
use Metadata\Driver\DriverInterface;
|
||||
|
||||
/**
|
||||
* Uses Symfony2 DI configuration for metadata.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class ConfigDriver implements DriverInterface
|
||||
{
|
||||
private $bundles;
|
||||
private $config;
|
||||
|
||||
public function __construct(array $bundles, array $config)
|
||||
{
|
||||
uasort($bundles, function($a, $b) {
|
||||
return strlen($b) - strlen($a);
|
||||
});
|
||||
|
||||
foreach ($bundles as $name => $namespace) {
|
||||
$bundles[$name] = substr($namespace, 0, strrpos($namespace, '\\'));
|
||||
}
|
||||
|
||||
$this->bundles = $bundles;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function loadMetadataForClass(\ReflectionClass $class)
|
||||
{
|
||||
$metadata = new ClassMetadata($class->name);
|
||||
|
||||
foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) {
|
||||
if ($method->getDeclaringClass()->name !== $class->name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$expression = null;
|
||||
if (null !== $notation = $this->getControllerNotation($method)) {
|
||||
$expression = $this->getExpressionForSignature($notation);
|
||||
}
|
||||
|
||||
if (null === $expression && null === $expression =
|
||||
$this->getExpressionForSignature($method->class.'::'.$method->name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$methodMetadata = new MethodMetadata($method->class, $method->name);
|
||||
$methodMetadata->roles = array(new Expression($expression));
|
||||
$metadata->addMethodMetadata($methodMetadata);
|
||||
}
|
||||
|
||||
if (!$metadata->methodMetadata) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
}
|
||||
|
||||
private function getExpressionForSignature($signature)
|
||||
{
|
||||
foreach ($this->config as $pattern => $expr) {
|
||||
if (!preg_match('#'.$pattern.'#i', $signature)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $expr;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Is it feasible to reverse-engineer the notation for service controllers?
|
||||
private function getControllerNotation(\ReflectionMethod $method)
|
||||
{
|
||||
$signature = $method->class.'::'.$method->name;
|
||||
|
||||
// check if class is a controller
|
||||
if (0 === preg_match('#\\\\Controller\\\\([^\\\\]+)Controller::(.+)Action$#', $signature, $match)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($this->bundles as $name => $namespace) {
|
||||
if (0 !== strpos($method->class, $namespace)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// controller notation (AcmeBundle:Foo:foo)
|
||||
return $name.':'.$match[1].':'.$match[2];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
84
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/MethodMetadata.php
vendored
Normal file
84
vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Metadata/MethodMetadata.php
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace JMS\SecurityExtraBundle\Metadata;
|
||||
|
||||
use Metadata\MethodMetadata as BaseMethodMetadata;
|
||||
|
||||
/**
|
||||
* Contains method metadata information
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class MethodMetadata extends BaseMethodMetadata
|
||||
{
|
||||
public $roles = array();
|
||||
public $paramPermissions = array();
|
||||
public $returnPermissions = array();
|
||||
public $runAsRoles = array();
|
||||
public $satisfiesParentSecurityPolicy = false;
|
||||
|
||||
/**
|
||||
* Adds a parameter restriction
|
||||
*
|
||||
* @param integer $index 0-based
|
||||
* @param array $permissions
|
||||
*/
|
||||
public function addParamPermissions($index, array $permissions)
|
||||
{
|
||||
$this->paramPermissions[$index] = $permissions;
|
||||
}
|
||||
|
||||
public function isDeclaredOnInterface()
|
||||
{
|
||||
foreach ($this->reflection->getDeclaringClass()->getInterfaces() as $interface) {
|
||||
if ($interface->hasMethod($this->name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This allows to merge in metadata from an interface
|
||||
*
|
||||
* @param MethodMetadata $method
|
||||
* @return void
|
||||
*/
|
||||
public function merge(MethodMetadata $method)
|
||||
{
|
||||
if (!$this->roles) {
|
||||
$this->roles = $method->roles;
|
||||
}
|
||||
|
||||
if (!$this->returnPermissions) {
|
||||
$this->returnPermissions = $method->returnPermissions;
|
||||
}
|
||||
|
||||
if (!$this->runAsRoles) {
|
||||
$this->runAsRoles = $method->runAsRoles;
|
||||
}
|
||||
|
||||
foreach ($method->paramPermissions as $index => $permissions) {
|
||||
if (!isset($this->paramPermissions[$index])) {
|
||||
$this->paramPermissions[$index] = $permissions;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user