e6429fcf7de18f57ef2b313fa2247224b75b7cc4
3 namespace app\Controller\Github
;
5 use app\Controller\Github
;
7 use app\Model\Accounts
;
12 class Callback
extends Github
19 function __construct(Web
$app, API
$github, Session
$session, Accounts
$accounts) {
20 parent
::__construct($app, $github, $session);
21 $this->accounts
= $accounts;
24 function __invoke(array $args = null) {
25 if ($this->app
->getRequest()->getQuery("error")) {
26 $this->app
->getView()->addData([
27 "error" => $this->app
->getRequest()->getQuery("error_description")
30 $this->github
->fetchToken(
31 $this->app
->getRequest()->getQuery("code"),
32 $this->app
->getRequest()->getQuery("state"),
34 $this->github
->setToken($token->access_token
);
35 $this->github
->readAuthUser($this->createUserCallback($token));
37 if (isset($this->session
->returnto
)) {
38 $returnto = $this->session
->returnto
;
39 unset($this->session
->returnto
);
40 $this->app
->redirect($returnto);
43 $this->app
->getBaseUrl()->mod("./github"));
46 $this->app
->display("github/callback");
49 function createUserCallback($oauth) {
50 return function($user) use($oauth) {
51 $tx = $this->accounts
->getConnection()->startTransaction();
53 if (($cookie = $this->app
->getRequest()->getCookie("account"))) {
54 $account = $this->accounts
->find(["account=" => $cookie])->current();
55 } elseif (!($account = $this->accounts
->byOAuth("github", $oauth->access_token
, $user->login
))) {
56 $account = $this->accounts
->createOAuthAccount("github", $oauth->access_token
, $user->login
);
58 $token = $account->updateToken("github", $oauth->access_token
, $oauth);
59 $owner = $account->updateOwner("github", $user->login
, $user);
63 $this->login($account, $token, $owner);