lib: Add Request::getIsApiRequest()

If the Accept header is set to application/json it's an API request.

refs #9660
This commit is contained in:
Eric Lippmann 2015-07-30 12:03:33 +02:00
parent 3aae37aff3
commit c23c7a5fa9
1 changed files with 47 additions and 18 deletions

View File

@ -7,24 +7,46 @@ use Zend_Controller_Request_Http;
use Icinga\User;
/**
* Request to handle special attributes
* A request
*/
class Request extends Zend_Controller_Request_Http
{
/**
* User object
* User if authenticated
*
* @var User
* @var User|null
*/
private $user;
protected $user;
/**
* Unique identifier
*
* @var string
*/
private $uniqueId;
protected $uniqueId;
private $url;
/**
* Request URL
*
* @var Url
*/
protected $url;
/**
* Get whether the request seems to be an API request
*
* @return bool
*/
public function getIsApiRequest()
{
return $this->getHeader('Accept') === 'application/json';
}
/**
* Get the request URL
*
* @return Url
*/
public function getUrl()
{
if ($this->url === null) {
@ -34,31 +56,38 @@ class Request extends Zend_Controller_Request_Http
}
/**
* Setter for user
* Get the user if authenticated
*
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* Getter for user
*
* @return User
* @return User|null
*/
public function getUser()
{
return $this->user;
}
/**
* Set the authenticated user
*
* @param User $user
*
* @return $this
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Makes an ID unique to this request, to prevent id collisions in different containers
*
* Call this whenever an ID might show up multiple times in different containers. This function is useful
* for ensuring unique ids on sites, even if we combine the HTML of different requests into one site,
* while still being able to reference elements uniquely in the same request.
*
* @param string $id
*
* @return string The id suffixed w/ an identifier unique to this request
*/
public function protectId($id)
{