Skip to content

Commit

Permalink
Merge pull request #46 from dereuromark/master-remove-exception
Browse files Browse the repository at this point in the history
Master remove exception
  • Loading branch information
dereuromark authored Oct 23, 2016
2 parents c58472e + b17af50 commit 8738c1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
14 changes: 8 additions & 6 deletions src/Auth/AclTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Cake\Core\Exception\Exception;
use Cake\ORM\TableRegistry;
use Cake\Utility\Hash;
use RuntimeException;
use TinyAuth\Utility\Utility;

trait AclTrait {
Expand Down Expand Up @@ -342,10 +341,13 @@ protected function _getAvailableRoles() {
protected function _getUserRoles($user) {
// Single-role from session
if (!$this->_config['multiRole']) {
if (isset($user[$this->_config['roleColumn']])) {
return $this->_mapped([$user[$this->_config['roleColumn']]]);
if (!array_key_exists($this->_config['roleColumn'], $user)) {
throw new Exception(sprintf('Missing TinyAuth role id field (%s) in user session', 'Auth.User.' . $this->_config['roleColumn']));
}
throw new Exception(sprintf('Missing TinyAuth role id field (%s) in user session', 'Auth.User.' . $this->_config['roleColumn']));
if (!isset($user[$this->_config['roleColumn']])) {
return [];
}
return $this->_mapped([$user[$this->_config['roleColumn']]]);
}

// Multi-role from session
Expand Down Expand Up @@ -388,7 +390,7 @@ protected function _getUserRoles($user) {
->toArray();

if (!count($roles)) {
throw new Exception('Missing TinyAuth roles for user in pivot table');
return [];
}

return $this->_mapped($roles);
Expand All @@ -406,7 +408,7 @@ protected function _mapped($roles) {
$alias = array_keys($availableRoles, $role);
$alias = array_shift($alias);
if (!$alias || !is_string($alias)) {
throw new RuntimeException('Cannot find role alias for role ' . $role);
continue;
}

$array[$alias] = $role;
Expand Down
19 changes: 7 additions & 12 deletions tests/TestCase/Auth/TinyAuthorizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,16 @@ public function testGetAcl() {
}

/**
* @expectedException \RuntimeException
* @return void
*/
public function testBasicUserMethodInexistentRole() {
$object = new TestTinyAuthorize($this->collection, [
'autoClearCache' => true
]);

$user = ['role_id' => 4]; // invalid non-existing role

//$this->expectException(\RuntimeException::class);

$object->authorize($user, $this->request);
$user = ['role_id' => 99]; // invalid non-existing role
$result = $object->authorize($user, $this->request);
$this->assertFalse($result);
}

/**
Expand Down Expand Up @@ -1516,14 +1513,12 @@ public function testUserRolesMissingRoleColumn() {
$method->setAccessible(true);

$user = ['id' => 1];
$res = $method->invokeArgs($object, [$user]);
$method->invoke($object);
$method->invokeArgs($object, [$user]);
}

/**
* Tests multi-role exception thrown when user has no roles in the pivot table.
* Tests multi-role when user has no roles in the pivot table.
*
* @expectedException \Cake\Core\Exception\Exception
* @return void
*/
public function testUserRolesUserWithoutPivotRoles() {
Expand All @@ -1538,8 +1533,8 @@ public function testUserRolesUserWithoutPivotRoles() {
$method->setAccessible(true);

$user = ['id' => 5];
$res = $method->invokeArgs($object, [$user]);
$method->invoke($object);
$result = $method->invokeArgs($object, [$user]);
$this->assertSame([], $result);
}

}

0 comments on commit 8738c1f

Please sign in to comment.