From c23c7a5fa95129460dfbb96419a8e4e48ffa0b3b Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 30 Jul 2015 12:03:33 +0200 Subject: [PATCH] lib: Add Request::getIsApiRequest() If the Accept header is set to application/json it's an API request. refs #9660 --- library/Icinga/Web/Request.php | 65 ++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/library/Icinga/Web/Request.php b/library/Icinga/Web/Request.php index 90551b33c..6fb078c96 100644 --- a/library/Icinga/Web/Request.php +++ b/library/Icinga/Web/Request.php @@ -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) {