d753687a3b7928ed739bc4b0bc66dbc54dbbad8b
[pharext/pharext.org] / Signin.php
1 <?php
2
3 namespace app\Controller\Github;
4
5 use app\Controller\Github;
6 use app\Github\API;
7 use app\Model\Accounts;
8 use app\Session;
9 use app\Web;
10
11 class Signin extends Github
12 {
13 /**
14 * @var Accounts
15 */
16 private $accounts;
17
18 function __construct(Web $app, API $github, Session $session, Accounts $accounts) {
19 parent::__construct($app, $github, $session);
20 $this->accounts = $accounts;
21 }
22
23 function __invoke(array $args = null) {
24 if (($cookie = $this->app->getRequest()->getCookie("account"))) {
25 $accounts = $this->accounts->find(["account=" => $cookie]);
26 if (count($accounts)) {
27 $account = $accounts->current();
28 $tokens = $account->allOf("tokens")->filter(function($token) {
29 return $token->authority == "github";
30 });
31 if (count($tokens)) {
32 $token = $tokens->current();
33 $this->login($account, $token,
34 $account->allOf("owners")->filter(function($owner) {
35 return $owner->authority == "github";
36 })->current()
37 );
38 if (($returnto = $this->app->getRequest()->getQuery("returnto"))) {
39 $this->app->redirect($returnto);
40 } else {
41 $this->app->redirect($this->app->getBaseUrl()->mod("./github"));
42 }
43 return;
44 }
45 }
46 }
47 $callback = $this->app->getBaseUrl()->mod("./github/callback");
48 $location = $this->github->getAuthUrl($callback);
49 $this->app->redirect($location);
50 if (($returnto = $this->app->getRequest()->getQuery("returnto"))) {
51 $this->session->returnto = $returnto;
52 }
53 }
54 }