lib: Reorder auth related functions in the ActionController

This commit is contained in:
Eric Lippmann 2015-01-23 09:36:05 +01:00
parent bd65f4d50a
commit 6da3cb8403
1 changed files with 49 additions and 37 deletions

View File

@ -5,7 +5,7 @@
namespace Icinga\Web\Controller;
use Exception;
use Icinga\Authentication\Manager as AuthManager;
use Icinga\Authentication\Manager;
use Icinga\Application\Benchmark;
use Icinga\Application\Config;
use Icinga\Exception\IcingaException;
@ -47,6 +47,11 @@ class ActionController extends Zend_Controller_Action
private $xhrLayout = 'inline';
/**
* Authentication manager
*
* @type \Icinga\Authentication\Manager|null
*/
private $auth;
protected $params;
@ -101,6 +106,49 @@ class ActionController extends Zend_Controller_Action
{
}
/**
* Get the authentication manager
*
* @return Manager
*/
public function Auth()
{
if ($this->auth === null) {
$this->auth = Manager::getInstance();
}
return $this->auth;
}
/**
* Whether the current user has the given permission
*
* @param string $permission Name of the permission
*
* @return bool
*/
public function hasPermission($permission)
{
return $this->Auth()->hasPermission($permission);
}
/**
* Throw an exception if user lacks the given permission
*
* @param string $name Permission name
* @throws Exception
*/
public function assertPermission($name)
{
if (! $this->Auth()->hasPermission($name)) {
// TODO: Shall this be an Auth Exception? Or a 404?
throw new IcingaException(
'Auth error, no permission for "%s"',
$name
);
}
}
public function Config($file = null)
{
if ($file === null) {
@ -110,14 +158,6 @@ class ActionController extends Zend_Controller_Action
}
}
public function Auth()
{
if ($this->auth === null) {
$this->auth = AuthManager::getInstance();
}
return $this->auth;
}
public function Window()
{
if ($this->window === null) {
@ -173,34 +213,6 @@ class ActionController extends Zend_Controller_Action
return $this->Auth()->getRestrictions($name);
}
/**
* Whether the user currently authenticated has the given permission
*
* @param string $name Permission name
* @return bool
*/
public function hasPermission($name)
{
return $this->Auth()->hasPermission($name);
}
/**
* Throws an exception if user lacks the given permission
*
* @param string $name Permission name
* @throws Exception
*/
public function assertPermission($name)
{
if (! $this->Auth()->hasPermission($name)) {
// TODO: Shall this be an Auth Exception? Or a 404?
throw new IcingaException(
'Auth error, no permission for "%s"',
$name
);
}
}
/**
* Check whether the controller requires a login. That is when the controller requires authentication and the
* user is currently not authenticated