Skip to content

Commit

Permalink
Add permission and authentication template tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Dec 5, 2023
1 parent 35fea5a commit ef6a2be
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 1 deletion.
74 changes: 74 additions & 0 deletions src/Template/Tag/AuthTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Platine Framework
*
* Platine Framework is a lightweight, high-performance, simple and elegant
* PHP Web framework
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2020 Platine Framework
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* @file AuthTag.php
*
* The template authentication tag class
*
* @package Platine\Framework\Template\Tag
* @author Platine Developers team
* @copyright Copyright (c) 2020
* @license http://opensource.org/licenses/MIT MIT License
* @link https://www.platine-php.com
* @version 1.0.0
* @filesource
*/

declare(strict_types=1);

namespace Platine\Framework\Template\Tag;

use Platine\Framework\Auth\AuthenticationInterface;
use Platine\Template\Parser\AbstractBlock;
use Platine\Template\Parser\Context;

/**
* @class AuthTag
* @package Platine\Framework\Template\Tag
*/
class AuthTag extends AbstractBlock
{
/**
* {@inheritdoc}
*/
public function render(Context $context): string
{
/** @var AuthenticationInterface $authentication */
$authentication = app(AuthenticationInterface::class);

if ($authentication->isLogged() === false) {
return '';
}

return parent::render($context);
}
}
104 changes: 104 additions & 0 deletions src/Template/Tag/PermissionTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* Platine Framework
*
* Platine Framework is a lightweight, high-performance, simple and elegant
* PHP Web framework
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2020 Platine Framework
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* @file AuthTag.php
*
* The template authorization tag class
*
* @package Platine\Framework\Template\Tag
* @author Platine Developers team
* @copyright Copyright (c) 2020
* @license http://opensource.org/licenses/MIT MIT License
* @link https://www.platine-php.com
* @version 1.0.0
* @filesource
*/

declare(strict_types=1);

namespace Platine\Framework\Template\Tag;

use Platine\Framework\Auth\AuthorizationInterface;
use Platine\Template\Exception\ParseException;
use Platine\Template\Parser\AbstractBlock;
use Platine\Template\Parser\Context;
use Platine\Template\Parser\Lexer;
use Platine\Template\Parser\Parser;

/**
* @class PermissionTag
* @package Platine\Framework\Template\Tag
*/
class PermissionTag extends AbstractBlock
{
/**
* The code of the permission
* @var string
*/
protected string $permission;

/**
* {@inheritdoc}
*/
public function __construct(string $markup, &$tokens, Parser $parser)
{
$lexer = new Lexer('/(\w+)/');
if ($lexer->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);
}
}
57 changes: 57 additions & 0 deletions tests/Template/Tag/AuthTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Platine\Test\Framework\Template\Tag;

use Platine\Dev\PlatineTestCase;
use Platine\Framework\Auth\Authentication\SessionAuthentication;
use Platine\Framework\Template\Tag\AuthTag;
use Platine\Template\Parser\Context;
use Platine\Template\Parser\Parser;

/*
* @group core
* @group framework
*/
class AuthTagTest extends PlatineTestCase
{
public function testRenderNoLogin(): void
{
global $mock_app_auth_object,
$mock_app_to_instance;

$mock_app_to_instance = true;
$mock_app_auth_object = $this->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);
}
}
95 changes: 95 additions & 0 deletions tests/Template/Tag/PermissionTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace Platine\Test\Framework\Template\Tag;

use Platine\Dev\PlatineTestCase;
use Platine\Framework\Auth\Authorization\SessionAuthorization;
use Platine\Framework\Template\Tag\PermissionTag;
use Platine\Template\Exception\ParseException;
use Platine\Template\Parser\Context;
use Platine\Template\Parser\Parser;

/*
* @group core
* @group framework
*/
class PermissionTagTest extends PlatineTestCase
{
public function testConstructor(): void
{
$parser = $this->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);
}
}
13 changes: 12 additions & 1 deletion tests/fixtures/mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [];
Expand All @@ -361,6 +366,7 @@ function sha1(string $str)
$mock_app_config_items = [];
$mock_sha1_foo = true;


function sha1(string $str)
{
global $mock_sha1_foo;
Expand All @@ -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);
}
Expand Down

0 comments on commit ef6a2be

Please sign in to comment.