diff --git a/src/Template/Tag/AuthTag.php b/src/Template/Tag/AuthTag.php new file mode 100644 index 0000000..9cd4b79 --- /dev/null +++ b/src/Template/Tag/AuthTag.php @@ -0,0 +1,74 @@ +isLogged() === false) { + return ''; + } + + return parent::render($context); + } +} diff --git a/src/Template/Tag/PermissionTag.php b/src/Template/Tag/PermissionTag.php new file mode 100644 index 0000000..e6370f6 --- /dev/null +++ b/src/Template/Tag/PermissionTag.php @@ -0,0 +1,104 @@ +match($markup)) { + $this->permission = $lexer->getStringMatch(1); + parent::__construct($markup, $tokens, $parser); + } else { + throw new ParseException(sprintf( + 'Syntax Error in "%s" - Valid syntax: permission [code]', + 'permission' + )); + } + } + + /** + * {@inheritdoc} + */ + public function render(Context $context): string + { + if ($context->hasKey($this->permission)) { + $this->permission = (string) $context->get($this->permission); + } + + /** @var AuthorizationInterface $authorization */ + $authorization = app(AuthorizationInterface::class); + + if ($authorization->isGranted($this->permission) === false) { + return ''; + } + + return parent::render($context); + } +} diff --git a/tests/Template/Tag/AuthTagTest.php b/tests/Template/Tag/AuthTagTest.php new file mode 100644 index 0000000..4c38f9e --- /dev/null +++ b/tests/Template/Tag/AuthTagTest.php @@ -0,0 +1,57 @@ +getMockInstance(SessionAuthentication::class, [ + 'isLogged' => false + ]); + + + $parser = $this->getMockInstance(Parser::class); + $tokens = ['tnh', '{% endauth %}']; + $b = new AuthTag('foo', $tokens, $parser); + + $c = new Context(); + $res = $b->render($c); + $this->assertEmpty($res); + } + + public function testRender(): void + { + global $mock_app_auth_object, + $mock_app_to_instance; + + $mock_app_to_instance = true; + $mock_app_auth_object = $this->getMockInstance(SessionAuthentication::class, [ + 'isLogged' => true + ]); + + $parser = $this->getMockInstance(Parser::class); + $tokens = ['tnh', '{% endauth %}']; + $b = new AuthTag('foo', $tokens, $parser); + + $c = new Context(); + $res = $b->render($c); + $this->assertEquals('tnh', $res); + } +} diff --git a/tests/Template/Tag/PermissionTagTest.php b/tests/Template/Tag/PermissionTagTest.php new file mode 100644 index 0000000..62e0d5d --- /dev/null +++ b/tests/Template/Tag/PermissionTagTest.php @@ -0,0 +1,95 @@ +getMockInstance(Parser::class); + $tokens = ['{% endpermission %}']; + $b = new PermissionTag('permission', $tokens, $parser); + + $this->assertEquals('permission', $this->getPropertyValue(PermissionTag::class, $b, 'permission')); + } + + public function testConstructorInvalidSyntax(): void + { + $this->expectException(ParseException::class); + $parser = $this->getMockInstance(Parser::class); + $tokens = []; + (new PermissionTag('(+', $tokens, $parser)); + } + + public function testRenderNoPermission(): void + { + global $mock_app_auth_object, + $mock_app_to_instance; + + $mock_app_to_instance = true; + $mock_app_auth_object = $this->getMockInstance(SessionAuthorization::class, [ + 'isGranted' => false + ]); + + $parser = $this->getMockInstance(Parser::class); + $tokens = ['tnh', '{% endpermission %}']; + $b = new PermissionTag('permission', $tokens, $parser); + + $c = new Context(); + $res = $b->render($c); + $this->assertEmpty($res); + } + + public function testRender(): void + { + global $mock_app_auth_object, + $mock_app_to_instance; + + $mock_app_to_instance = true; + $mock_app_auth_object = $this->getMockInstance(SessionAuthorization::class, [ + 'isGranted' => true + ]); + + $parser = $this->getMockInstance(Parser::class); + $tokens = ['tnh', '{% endpermission %}']; + $b = new PermissionTag('permission', $tokens, $parser); + + $c = new Context(); + $res = $b->render($c); + $this->assertEquals('tnh', $res); + } + + public function testRenderPermissionCodeIsFromContext(): void + { + global $mock_app_auth_object, + $mock_app_to_instance; + + $mock_app_to_instance = true; + $mock_app_auth_object = $this->getMockInstance(SessionAuthorization::class, [ + 'isGranted' => true + ]); + + $parser = $this->getMockInstance(Parser::class); + $tokens = ['tnh', '{% endpermission %}']; + $b = new PermissionTag('permission', $tokens, $parser); + + $c = new Context(); + $c->set('permission', 'foo'); + + $res = $b->render($c); + $this->assertEquals('tnh', $res); + } +} diff --git a/tests/fixtures/mocks.php b/tests/fixtures/mocks.php index e659777..70b6352 100644 --- a/tests/fixtures/mocks.php +++ b/tests/fixtures/mocks.php @@ -338,7 +338,10 @@ function sha1(string $str) } namespace Platine\Framework\Template\Tag; + use Platine\Config\Config; +use Platine\Framework\Auth\AuthenticationInterface; +use Platine\Framework\Auth\AuthorizationInterface; use Platine\Framework\Http\RouteHelper; use Platine\Framework\Security\Csrf\CsrfManager; use Platine\Http\ServerRequestInterface; @@ -351,7 +354,9 @@ function sha1(string $str) use Platine\Test\Framework\Fixture\MyServerRequest; use Platine\Test\Framework\Fixture\MySession; + $mock_app_to_instance = false; +$mock_app_auth_object = null; $mock_app_lang_methods = []; $mock_app_route_helper_methods = []; $mock_app_server_request_methods = []; @@ -361,6 +366,7 @@ function sha1(string $str) $mock_app_config_items = []; $mock_sha1_foo = true; + function sha1(string $str) { global $mock_sha1_foo; @@ -380,9 +386,14 @@ function app(string $id) $mock_app_server_request_methods, $mock_app_lang_methods, $mock_app_route_helper_methods, - $mock_app_session_flash; + $mock_app_session_flash, + $mock_app_auth_object; if ($mock_app_to_instance) { + if ($id === AuthenticationInterface::class || $id === AuthorizationInterface::class) { + return $mock_app_auth_object; + } + if ($id === Config::class) { return new MyConfig($mock_app_config_items); }