X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=app%2FController%2FGithub%2FCallback.php;h=646d782f20d52c59676efdbd23a72a39ddd3c5ba;hb=36cfa28cf2dcee3422f0231f91c6692eb28e7824;hp=ac2ce7b481d2bca63e680769e55833708c8b04e8;hpb=ebc0d017c8a24bd16ca1f4347b39b07ba4349135;p=pharext%2Fpharext.org diff --git a/app/Controller/Github/Callback.php b/app/Controller/Github/Callback.php index ac2ce7b..646d782 100644 --- a/app/Controller/Github/Callback.php +++ b/app/Controller/Github/Callback.php @@ -3,32 +3,62 @@ namespace app\Controller\Github; use app\Controller\Github; +use app\Github\API; +use app\Model\Accounts; +use app\Session; +use app\Web; class Callback extends Github { + /** + * @var Accounts + */ + private $accounts; + + function __construct(Web $app, API $github, Session $session, Accounts $accounts) { + parent::__construct($app, $github, $session); + $this->accounts = $accounts; + } + function __invoke(array $args = null) { if ($this->app->getRequest()->getQuery("error")) { $this->app->getView()->addData([ "error" => $this->app->getRequest()->getQuery("error_description") ]); } else { - try { - $this->github->fetchToken( - $this->app->getRequest()->getQuery("code"), - $this->app->getRequest()->getQuery("state"), - function($json) { - $this->github->setToken($json->access_token); - })->send(); - if (isset($this->session->returnto)) { - $this->app->redirect($this->session->returnto); - } else { - $this->app->redirect( - $this->app->getBaseUrl()->mod("./github")); - } - } catch (\app\Github\Exception $exception) { - $this->app->getView()->addData(compact("exception")); + $this->github->fetchToken( + $this->app->getRequest()->getQuery("code"), + $this->app->getRequest()->getQuery("state"), + function($token) { + $this->github->setToken($token->access_token); + $this->github->readAuthUser($this->createUserCallback($token)); + })->send(); + if (isset($this->session->returnto)) { + $returnto = $this->session->returnto; + unset($this->session->returnto); + $this->app->redirect($returnto); + } else { + $this->app->redirect( + $this->app->getBaseUrl()->mod("./github")); } } $this->app->display("github/callback"); } + + function createUserCallback($token) { + return function($user) use($token) { + $tx = $this->accounts->getConnection()->startTransaction(); + + if (!($account = $this->accounts->byOAuth("github", $token->access_token, $user->login))) { + $account = $this->accounts->createOAuthAccount("github", $token->access_token, $user->login); + } + $account->updateToken("github", $token->access_token, $token); + $owner = $account->updateOwner("github", $user->login, $user); + + $tx->commit(); + + $this->session->account = $account->account->get(); + $this->session->github = (object) $owner->export(); + }; + } }