controllers: fix base url to omit scheme for when we connect through a https gateway
[pharext/pharext.org] / app / Controller / Github.php
index 364f0e584424fa2a1438605f746f14ecf655c5b6..7ec309461ef60fd6ac20e59f5a9f7fb43bdaec32 100644 (file)
@@ -4,11 +4,14 @@ namespace app\Controller;
 
 use app\Controller;
 use app\Github\API;
+use app\Model\Account;
+use app\Model\Owner;
+use app\Model\Token;
 use app\Session;
 use app\Web;
-
-use http\QueryString;
+use http\Cookie;
 use http\Header;
+use http\QueryString;
 
 abstract class Github implements Controller
 {
@@ -18,12 +21,12 @@ abstract class Github implements Controller
        protected $app;
 
        /**
-        * @var \app\Github\API
+        * @var API
         */
        protected $github;
 
        /**
-        * @var \app\Session
+        * @var Session
         */
        protected $session;
        
@@ -35,7 +38,7 @@ abstract class Github implements Controller
                        "location" => "github", 
                        "title" => "Github"
                ]);
-               $this->app->getView()->registerFunction("check", [$this, "checkRepoHook"]);
+               $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]);
                
                if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) {
                        $params = $header->getParams();
@@ -47,30 +50,28 @@ abstract class Github implements Controller
                }
        }
 
+       protected function login(Account $account, Token $token, Owner $owner) {
+               $auth = new Cookie;
+               $auth->setCookie("account", $account->account->get());
+               $auth->setFlags(Cookie::SECURE | Cookie::HTTPONLY);
+               $auth->setPath($this->app->getBaseUrl()->path);
+               $auth->setMaxAge(60*60*24);
+               $this->app->getResponse()->setCookie($auth);
+
+               $this->github->setToken($token->token->get());
+               $this->session->account = $account->account->get();
+               $this->session->github = (object) $owner->export();
+       }
+
        protected function checkToken() {
                if ($this->github->hasToken()) {
                        return true;
                }
                $this->app->redirect($this->app->getBaseUrl()->mod([
+                       "scheme" => null,
                        "path" => "github/signin",
                        "query" => new QueryString(["returnto" => $this->session->current])
                ]));
                return false;
        }
-
-       /**
-        * Check if the pharext webhook is set for the repo and return it
-        * @param object $repo
-        * @return int hook id
-        */
-       function checkRepoHook($repo) {
-               if ($repo->hooks) {
-                       foreach ($repo->hooks as $hook) {
-                               if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) {
-                                       return $hook;
-                               }
-                       }
-               }
-               return null;
-       }
 }