-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add permission and authentication template tags
- Loading branch information
1 parent
35fea5a
commit ef6a2be
Showing
5 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters