[icinga-checkins] icinga.org: icingaweb2/bugfix/ username-and-password-not-being-passed-in-navigation-item-urls-12133: Url: Add support for username and password

git at icinga.org git at icinga.org
Thu Oct 13 11:04:39 CEST 2016


Module: icingaweb2
Branch: bugfix/username-and-password-not-being-passed-in-navigation-item-urls-12133
Commit: f557b89aea1a41cec89ddbdf67d8fa1e77115b8e
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=f557b89aea1a41cec89ddbdf67d8fa1e77115b8e

Author: Noah Hilverling <noah.hilverling at netways.de>
Date:   Thu Oct 13 11:01:39 2016 +0200

Url: Add support for username and password

fixes #12133

---

 library/Icinga/Web/Url.php |   93 +++++++++++++++++++++++++++++++-------------
 1 file changed, 65 insertions(+), 28 deletions(-)

diff --git a/library/Icinga/Web/Url.php b/library/Icinga/Web/Url.php
index 3fec8e3..eee57d8 100644
--- a/library/Icinga/Web/Url.php
+++ b/library/Icinga/Web/Url.php
@@ -183,33 +183,16 @@ class Url
             || (isset($urlParts['host']) && $urlParts['host'] !== $request->getServer('SERVER_NAME'))
             || (isset($urlParts['port']) && $urlParts['port'] != $request->getServer('SERVER_PORT')))
         ) {
-            $baseUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . (isset($urlParts['port'])
-                ? (':' . $urlParts['port'])
-                : '');
             $urlObject->setIsExternal();
-        } else {
-            $baseUrl = '';
         }
 
         if (isset($urlParts['path'])) {
             $urlPath = $urlParts['path'];
             if ($urlPath && $urlPath[0] === '/') {
-                if ($baseUrl) {
-                    $urlPath = substr($urlPath, 1);
-                } else {
-                    $requestBaseUrl = $request->getBaseUrl();
-                    if ($requestBaseUrl && $requestBaseUrl !== '/' && strpos($urlPath, $requestBaseUrl) === 0) {
-                        $urlPath = substr($urlPath, strlen($requestBaseUrl) + 1);
-                        $baseUrl = $requestBaseUrl;
-                    }
-                }
-            } elseif (! $baseUrl) {
-                $baseUrl = $request->getBaseUrl();
+                $urlPath = substr($urlPath, 1);
             }
 
             $urlObject->setPath($urlPath);
-        } elseif (! $baseUrl) {
-            $baseUrl = $request->getBaseUrl();
         }
 
         // TODO: This has been used by former filter implementation, remove it:
@@ -221,7 +204,26 @@ class Url
             $urlObject->setAnchor($urlParts['fragment']);
         }
 
-        $urlObject->setBaseUrl($baseUrl);
+        if (isset($urlParts['host'])) {
+            $urlObject->setHost($urlParts['host']);
+        }
+
+        if (isset($urlParts['port'])) {
+            $urlObject->setPort($urlParts['port']);
+        }
+
+        if (isset($urlParts['scheme'])) {
+            $urlObject->setScheme($urlParts['scheme']);
+        }
+
+        if (isset($urlParts['user'])) {
+            $urlObject->setUsername($urlParts['user']);
+        }
+
+        if (isset($urlParts['pass'])) {
+            $urlObject->setPassword($urlParts['pass']);
+        }
+
         $urlObject->setParams($params);
         return $urlObject;
     }
@@ -336,24 +338,46 @@ class Url
     /**
      * Overwrite the baseUrl
      *
-     * @param   string  $baseUrl    The url path to use as the Url Base
+     * @deprecated  Please create a new url from scratch instead
      *
-     * @return  $this
+     * @param       string  $baseUrl    The url path to use as the Url Base
+     *
+     * @return      $this
      */
     public function setBaseUrl($baseUrl)
     {
-        $this->baseUrl = rtrim($baseUrl, '/ ');
+        $urlParts = parse_url($baseUrl);
+        if (isset($urlParts["host"])) {
+            $this->setHost($urlParts["host"]);
+        }
+        if (isset($urlParts["port"])) {
+            $this->setPort($urlParts["port"]);
+        }
+        if (isset($urlParts["scheme"])) {
+            $this->setScheme($urlParts["scheme"]);
+        }
+        if (isset($urlParts["user"])) {
+            $this->setUsername($urlParts["user"]);
+        }
+        if (isset($urlParts["pass"])) {
+            $this->setPassword($urlParts["pass"]);
+        }
         return $this;
     }
 
     /**
-     * Return the baseUrl set for this url
+     * Return the baseUrl for this url
+     *
+     * @deprecated
      *
      * @return  string
      */
     public function getBaseUrl()
     {
-        return $this->baseUrl;
+        if (!$this->isExternal()) {
+            return '';
+        }
+        return $this->getScheme() . '://' . $this->getHost() . ($this->getPort() ? (':' . $this->getPort()) : '');
     }
 
     /**
@@ -508,12 +532,25 @@ class Url
             return $path;
         }
 
-        $baseUrl = $this->getBaseUrl();
-        if (! $baseUrl) {
-            $baseUrl = '/';
+        if (!$this->isExternal()) {
+            return '/icingaweb2/' . $path;
+        }
+
+        $urlString = '';
+        if ($this->getScheme()) {
+            $urlString = $urlString . $this->getScheme() . '://';
+        }
+        if ($this->getUsername() && $this->getPassword()) {
+            $urlString = $urlString . $this->getUsername() . ':' . $this->getPassword() . "@";
+        }
+        if ($this->getHost()) {
+            $urlString = $urlString . $this->getHost();
+        }
+        if ($this->getPort()) {
+            $urlString = $urlString . $this->getPort();
         }
 
-        return $baseUrl . ($baseUrl !== '/' && $path ? '/' : '') . $path;
+        return $urlString . '/' . $path;
     }
 
     /**



More information about the icinga-checkins mailing list