Skip to content

Commit

Permalink
Merge pull request #262 from CakeDC/feature/is-authorized-helper
Browse files Browse the repository at this point in the history
Adding isAuthorized method in UserHelper
  • Loading branch information
ajibarra committed Nov 5, 2015
2 parents 705366d + af23eac commit f67a687
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/View/Helper/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ public function link($title, $url = null, array $options = [])
return false;
}

/**
* Retunrs true if the target url is authorized for the logged in user
*
* @param type $url url that the user is making request.
* @return bool
*/
public function isAuthorized($url = null)
{
$event = new Event(UsersAuthComponent::EVENT_IS_AUTHORIZED, $this, ['url' => $url]);
$result = $this->_View->eventManager()->dispatch($event);
return $result->result;
}

/**
* Welcome display
* @return mixed
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/View/Helper/UserHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,32 @@ public function testLinkAuthorized()
$this->assertSame('before_<a href="/" class="link-class">title</a>_after', $link);
}


/**
* Test isAuthorized
*
* @return void
*/
public function testIsAuthorizedHH()
{
$view = new View();
$eventManagerMock = $this->getMockBuilder('Cake\Event\EventManager')
->setMethods(['dispatch'])
->getMock();
$view->eventManager($eventManagerMock);
$this->User = new UserHelper($view);
$result = new Event('dispatch-result');
$result->result = true;
$eventManagerMock->expects($this->once())
->method('dispatch')
->will($this->returnValue($result));

$link = $this->User->isAuthorized(['controller' => 'MyController', 'action' => 'myAction']);
$this->assertTrue($link);
}



/**
* Test link
*
Expand Down

0 comments on commit f67a687

Please sign in to comment.