-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from dereuromark/policy
Add Request Policy.
Showing
9 changed files
with
256 additions
and
46 deletions.
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
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
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
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
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,47 @@ | ||
<?php | ||
namespace TinyAuth\Policy; | ||
|
||
use Authorization\Policy\RequestPolicyInterface; | ||
use Cake\Core\InstanceConfigTrait; | ||
use Cake\Http\ServerRequest; | ||
use TinyAuth\Auth\AclTrait; | ||
use TinyAuth\Utility\Config; | ||
|
||
/** | ||
* Used for middleware approach. | ||
*/ | ||
class RequestPolicy implements RequestPolicyInterface { | ||
|
||
use AclTrait; | ||
use InstanceConfigTrait; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $_defaultConfig = [ | ||
]; | ||
|
||
/** | ||
* @param array $config | ||
*/ | ||
public function __construct(array $config = []) { | ||
$config += Config::all(); | ||
|
||
$this->setConfig($config); | ||
} | ||
|
||
/** | ||
* Method to check if the request can be accessed | ||
* | ||
* @param \Authorization\IdentityInterface|null $identity Identity | ||
* @param \Cake\Http\ServerRequest $request Request | ||
* @return bool | ||
*/ | ||
public function canAccess($identity, ServerRequest $request) { | ||
$params = $request->getAttribute('params'); | ||
$user = $identity->getOriginalData(); | ||
|
||
return $this->_checkUser((array)$user, $params); | ||
} | ||
|
||
} |
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
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
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,90 @@ | ||
<?php | ||
|
||
namespace TinyAuth\Test\TestCase\Policy; | ||
|
||
use Authorization\AuthorizationService; | ||
use Authorization\IdentityDecorator; | ||
use Cake\Core\Configure; | ||
use Cake\Core\Plugin; | ||
use Cake\Http\ServerRequest; | ||
use Cake\TestSuite\TestCase; | ||
use TinyAuth\Policy\RequestPolicy; | ||
|
||
class RequestPolicyTest extends TestCase { | ||
|
||
/** | ||
* @var \TinyAuth\Controller\Component\AuthorizationComponent | ||
*/ | ||
protected $policy; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $config = []; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function setUp() { | ||
Configure::write('Roles', [ | ||
'user' => ROLE_USER, | ||
'moderator' => ROLE_MODERATOR, | ||
'admin' => ROLE_ADMIN | ||
]); | ||
|
||
$this->config = [ | ||
'aclFilePath' => Plugin::path('TinyAuth') . 'tests' . DS . 'test_files' . DS, | ||
'autoClearCache' => true, | ||
]; | ||
|
||
$this->policy = new RequestPolicy($this->config); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testPolicyCanAccessSuccess() { | ||
$request = new ServerRequest(['params' => [ | ||
'controller' => 'Tags', | ||
'action' => 'delete', | ||
'plugin' => null, | ||
]]); | ||
|
||
$identityArray = [ | ||
'id' => 1, | ||
'role_id' => ROLE_ADMIN, | ||
]; | ||
$service = $this->getService(); | ||
$identity = new IdentityDecorator($service, $identityArray); | ||
$result = $this->policy->canAccess($identity, $request); | ||
$this->assertTrue($result); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testPolicyCanAccessFail() { | ||
$request = new ServerRequest(['params' => [ | ||
'controller' => 'Tags', | ||
'action' => 'edit', | ||
'plugin' => null, | ||
]]); | ||
|
||
$identityArray = [ | ||
'id' => 1, | ||
'role_id' => ROLE_ADMIN, | ||
]; | ||
$service = $this->getService(); | ||
$identity = new IdentityDecorator($service, $identityArray); | ||
$result = $this->policy->canAccess($identity, $request); | ||
$this->assertFalse($result); | ||
} | ||
|
||
/** | ||
* @return \Authorization\AuthorizationService|\PHPUnit\Framework\MockObject\MockObject | ||
*/ | ||
protected function getService() { | ||
return $this->getMockBuilder(AuthorizationService::class)->disableOriginalConstructor()->getMock(); | ||
} | ||
|
||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace TinyAuth\Test\TestCase\Utility; | ||
|
||
use Cake\TestSuite\TestCase; | ||
use TinyAuth\Utility\TinyAuth; | ||
|
||
class TinyAuthTest extends TestCase { | ||
|
||
/** | ||
* @var array | ||
*/ | ||
public $fixtures = [ | ||
'plugin.TinyAuth.DatabaseRoles', | ||
]; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testGetAvailableRoles() { | ||
$config = [ | ||
'rolesTable' => 'DatabaseRoles', | ||
]; | ||
|
||
$result = (new TinyAuth($config))->getAvailableRoles(); | ||
$expected = [ | ||
'user' => 11, | ||
'moderator' => 12, | ||
'admin' => 13 | ||
]; | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
} |