Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Moved the preconditions of function arguments before the function #108

Merged
merged 1 commit into from

2 participants

@stof

This allows to call compileInternal on arguments when compiling
preconditions of the function as the preconditions should be compiled
before compiling the expression.

I got an error when the argument was #arg as the ParameterExpressionCompiler had not yet populated the parameter_mapping_name attribute

@schmittjoh
Owner

Could you add tests for this (same for the other PRs that you made)?

@stof stof Moved the preconditions of function arguments before the function
This allows to call compileInternal on arguments when compiling
preconditions of the function as the preconditions should be compiled
before compiling the expression.
0089574
@stof

test added

@schmittjoh schmittjoh merged commit 0225405 into from
@schmittjoh
Owner

Thanks!

@stof stof deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 31, 2013
  1. @stof

    Moved the preconditions of function arguments before the function

    stof authored
    This allows to call compileInternal on arguments when compiling
    preconditions of the function as the preconditions should be compiled
    before compiling the expression.
This page is out of date. Refresh to see the latest.
View
6 Security/Authorization/Expression/ExpressionCompiler.php
@@ -129,7 +129,7 @@ public function outdent()
$this->indentationLevel -= 1;
if ($this->indentationLevel < 0) {
- throw new RuntimeException('The identation level cannot be less than zero.');
+ throw new RuntimeException('The indentation level cannot be less than zero.');
}
return $this;
@@ -266,12 +266,12 @@ public function compilePreconditions(ExpressionInterface $expr)
}
if ($expr instanceof FunctionExpression) {
- $this->getFunctionCompiler($expr->name)->compilePreconditions($this, $expr);
-
foreach ($expr->args as $arg) {
$this->compilePreconditions($arg);
}
+ $this->getFunctionCompiler($expr->name)->compilePreconditions($this, $expr);
+
return $this;
}
View
54 Tests/Security/Authorization/Expression/ExpressionCompilerTest.php
@@ -3,6 +3,8 @@
namespace JMS\SecurityExtraBundle\Tests\Security\Authorization\Expression;
use JMS\SecurityExtraBundle\Security\Authorization\Expression\Compiler\ParameterExpressionCompiler;
+use JMS\SecurityExtraBundle\Security\Authorization\Expression\Ast\FunctionExpression;
+use JMS\SecurityExtraBundle\Security\Authorization\Expression\Compiler\Func\FunctionCompilerInterface;
use JMS\SecurityExtraBundle\Security\Acl\Expression\HasPermissionFunctionCompiler;
@@ -110,6 +112,27 @@ public function testCompileWhenParameterIsWrappedInMethodCall()
$this->assertFalse($evaluator($context));
}
+ public function testCompileInternalInPreconditions()
+ {
+ $this->compiler->addTypeCompiler(new ParameterExpressionCompiler());
+ $this->compiler->addFunctionCompiler(new TestIssue108FunctionCompiler());
+
+ // the first call ensure that state is reset correctly
+ $this->compiler->compileExpression(new Expression(
+ 'testIssue(#project)'));
+ $evaluator = eval($this->compiler->compileExpression(
+ new Expression('testIssue(#project)')));
+
+ $secureObject = new SecuredObject();
+ $project = new Project();
+
+ $context = array(
+ 'object' => new MethodInvocation(new \ReflectionMethod($secureObject, 'delete'), $secureObject, array($project), array()),
+ );
+
+ $this->assertTrue($evaluator($context));
+ }
+
/**
* @dataProvider getUnaryNotTests
*/
@@ -148,3 +171,34 @@ protected function setUp()
$this->compiler = new ExpressionCompiler();
}
}
+
+class TestIssue108FunctionCompiler implements FunctionCompilerInterface
+{
+ public function getName()
+ {
+ return 'testIssue';
+ }
+
+ public function compilePreconditions(ExpressionCompiler $compiler, FunctionExpression $function)
+ {
+ if (1 !== count($function->args)) {
+ throw new \RuntimeException(sprintf('The %s() function expects exactly one argument, but got "%s".', $this->getName(), var_export($function->args, true)));
+ }
+
+ $argName = $compiler->nextName();
+
+ $compiler
+ ->write("\$$argName = ")
+ ->compileInternal($function->args[0])
+ ->writeln(';');
+
+ $compiler->attributes['arg_name'] = $argName;
+ }
+
+ public function compile(ExpressionCompiler $compiler, FunctionExpression $function)
+ {
+ $argName = $compiler->attributes['arg_name'];
+
+ $compiler->write("\$$argName !== null");
+ }
+}
Something went wrong with that request. Please try again.