Completed
Push — master ( dcce7f...83780e )
by Michal
13s
created

DevelopersController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Developer controller handling developer login/logout/register.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Controller;
21
22
use Cake\Core\Configure;
23
use Cake\Event\Event;
24
use Cake\Network\Exception\NotFoundException;
25
use Cake\ORM\TableRegistry;
26
27
/**
28
 * Developer controller handling developer login/logout/register.
29
 */
30
class DevelopersController extends AppController
31
{
32
    public $helpers = array('Html', 'Form');
33
34
    public $components = array(
35
        'GithubApi',
36
    );
37
38
    public function beforeFilter(Event $event)
39
    {
40
        parent::beforeFilter($event);
41
        $this->GithubApi->githubConfig = Configure::read('GithubConfig');
42
        $this->GithubApi->githubRepo = Configure::read('GithubRepoPath');
43
    }
44
45
    public function login()
46
    {
47
        $url = $this->GithubApi->getRedirectUrl('user:email,public_repo');
48
        $this->redirect($url);
49
    }
50
51
    public function callback()
52
    {
53
        $code = $this->request->query('code');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Http\ServerRequest::query() has been deprecated with message: 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
        $accessToken = $this->GithubApi->getAccessToken($code);
55
        if ($accessToken) {
56
            list($userInfo, $status) = $this->GithubApi->getUserInfo($accessToken);
57
            if ($status != 200) {
58
                $this->Session->setFlash($userInfo['message'],
59
                        array('class' => 'alert alert-error'));
60
            } else {
61
                $userInfo['has_commit_access'] = $this->GithubApi->canCommitTo(
62
                    $userInfo['login'],
63
                    $this->GithubApi->githubRepo,
64
                    Configure::read('GithubAccessToken')
65
                );
66
67
                $this->_authenticateDeveloper($userInfo, $accessToken);
68
69
                $flash_class = 'alert alert-success';
70
                $this->Flash->default('You have been logged in successfully',
71
                        array('params' => array('class' => $flash_class)));
72
            }
73
        } else {
74
            $flash_class = 'alert alert-error';
75
            $this->Flash->default('We were not able to authenticate you.'
76
                    . 'Please try again later',
77
                    array('params' => array('class' => $flash_class)));
78
        }
79
        $last_page = $this->request->session()->read('last_page');
80
        if (empty($last_page)) {
81
            $last_page = array('controller' => 'reports', 'action' => 'index');
82
        }
83
        $this->redirect($last_page);
84
    }
85
86
    public function logout()
87
    {
88
        $this->request->session()->destroy();
89
90
        $flash_class = 'alert alert-success';
91
        $this->Flash->default('You have been logged out successfully',
92
                array('params' => array('class' => $flash_class)));
93
        $this->redirect('/');
94
    }
95
96
    protected function _authenticateDeveloper($userInfo, $accessToken)
97
    {
98
        $developers = $this->Developers->findByGithubId($userInfo['id']);
99
        $developer = $developers->all()->first();
100
        if (!$developer) {
101
            $developer = $this->Developers->newEntity();
102
        } else {
103
            $this->Developers->id = $developer['id'];
104
        }
105
        $this->Developers->id = $this->Developers->saveFromGithub($userInfo, $accessToken, $developer);
106
        $this->request->session()->write('Developer.id', $this->Developers->id);
107
        $this->request->session()->write('access_token', $accessToken);
108
        $this->request->session()->write('read_only', !($userInfo['has_commit_access']));
109
    }
110
}
111