controllers: fix base url to omit scheme for when we connect through a https gateway
[pharext/pharext.org] / app / Controller / Github.php
index 49b14885be7eb986f88e88e73a884ac0a67ee961..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\Cookie;
+use http\Header;
 use http\QueryString;
-use http\Url;
 
 abstract class Github implements Controller
 {
@@ -18,20 +21,46 @@ abstract class Github implements Controller
        protected $app;
 
        /**
-        * @var \app\Github\API
+        * @var API
         */
        protected $github;
 
        /**
-        * @var \app\Session
+        * @var Session
         */
        protected $session;
-
-       function __construct(Web $app, API $github) {
+       
+       function __construct(Web $app, API $github, Session $session) {
                $this->app = $app;
                $this->github = $github;
-               $this->app->getView()->addData(["location" => "github"]);
-               $this->app->getView()->registerFunction("check", [$this, "checkRepoHook"]);
+               $this->session = $session;
+               $this->app->getView()->addData(compact("session") + [
+                       "location" => "github", 
+                       "title" => "Github"
+               ]);
+               $this->app->getView()->registerFunction("check", [$github, "checkRepoHook"]);
+               
+               if (($header = $this->app->getRequest()->getHeader("Cache-Control", Header::class))) {
+                       $params = $header->getParams();
+                       if (!empty($params["no-cache"])) {
+                               $this->github->setMaxAge(0);
+                       } elseif (!empty($params["max-age"])) {
+                               $this->github->setMaxAge($params["max-age"]["value"]);
+                       }
+               }
+       }
+
+       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() {
@@ -39,40 +68,10 @@ abstract class Github implements Controller
                        return true;
                }
                $this->app->redirect($this->app->getBaseUrl()->mod([
+                       "scheme" => null,
                        "path" => "github/signin",
                        "query" => new QueryString(["returnto" => $this->session->current])
                ]));
                return false;
        }
-
-       function setSession(Session $session) {
-               $this->session = $session;
-       }
-
-       function checkRepoHook($repo) {
-               if ($repo->hooks) {
-                       foreach ($repo->hooks as $hook) {
-                               if ($hook->name === "web" && $hook->config->url === $this->github->getConfig()->hook->url) {
-                                       return true;
-                               }
-                       }
-               }
-               return false;
-       }
-
-       function createLinkGenerator($links) {
-               return function($which) use($links) {
-                       if (!isset($links[$which])) {
-                               if ($which !== "next" || !isset($links["last"])) {
-                                       return null;
-                               } else {
-                                       $which = "last";
-                               }
-                       }
-                       $url = new Url($links[$which], null, 0);
-                       $qry = new QueryString($url->query);
-                       return $qry->getInt("page", 1);
-               };
-       }
-
 }