Completed
Push — master ( bc825a...1aba9a )
by William
06:03
created

DevelopersController::callback()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 35
nc 4
nop 0
dl 0
loc 50
ccs 31
cts 31
cp 1
crap 5
rs 9.0488
c 0
b 0
f 0
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
25
/**
26
 * Developer controller handling developer login/logout/register.
27
 */
28
class DevelopersController extends AppController
29
{
30
    public $helpers = [
31
        'Html',
32
        'Form',
33
    ];
34
35
    public $components = [
36
        'GithubApi',
37
    ];
38
39 3
    public function beforeFilter(Event $event)
40
    {
41 3
        parent::beforeFilter($event);
42 3
        $this->GithubApi->githubConfig = Configure::read('GithubConfig');
0 ignored issues
show
Bug Best Practice introduced by
The property GithubApi does not exist on App\Controller\DevelopersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
43 3
        $this->GithubApi->githubRepo = Configure::read('GithubRepoPath');
44 3
    }
45
46 1
    public function login()
47
    {
48 1
        $url = $this->GithubApi->getRedirectUrl('user:email,public_repo');
0 ignored issues
show
Bug Best Practice introduced by
The property GithubApi does not exist on App\Controller\DevelopersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
49 1
        $this->redirect($url);
50 1
    }
51
52 1
    public function callback()
53
    {
54 1
        $code = $this->request->query('code');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::query() has been deprecated: 3.4.0 Use getQuery() or the PSR-7 getQueryParams() and withQueryParams() methods instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
        $code = /** @scrutinizer ignore-deprecated */ $this->request->query('code');

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

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

Loading history...
55 1
        $accessToken = $this->GithubApi->getAccessToken($code);
0 ignored issues
show
Bug Best Practice introduced by
The property GithubApi does not exist on App\Controller\DevelopersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
56 1
        if ($code && $accessToken) {
57 1
            list($userInfo, $status) = $this->GithubApi->getUserInfo($accessToken);
58 1
            if ($status != 200) {
59 1
                $flash_class = 'alert alert-error';
60 1
                $this->Flash->default(
61 1
                    $userInfo['message'],
62 1
                    ['params' => ['class' => $flash_class]]
63
                );
64
65 1
                $this->redirect('/');
66 1
                return;
67
            } else {
68 1
                $userInfo['has_commit_access'] = $this->GithubApi->canCommitTo(
69 1
                    $userInfo['login'],
70 1
                    $this->GithubApi->githubRepo,
71 1
                    Configure::read('GithubAccessToken')
72
                );
73
74 1
                $this->_authenticateDeveloper($userInfo, $accessToken);
75
76 1
                $flash_class = 'alert alert-success';
77 1
                $this->Flash->default(
78 1
                    'You have been logged in successfully',
79 1
                    ['params' => ['class' => $flash_class]]
80
                );
81
            }
82
        } else {
83 1
            $flash_class = 'alert alert-error';
84 1
            $this->Flash->default(
85
                'We were not able to authenticate you.'
86 1
                    . ' Please try again later',
87 1
                ['params' => ['class' => $flash_class]]
88
            );
89
90 1
            $this->redirect('/');
91 1
            return;
92
        }
93
94 1
        $last_page = $this->request->session()->read('last_page');
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::session() has been deprecated: 3.5.0 Use getSession() instead. The setter part will be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

94
        $last_page = /** @scrutinizer ignore-deprecated */ $this->request->session()->read('last_page');

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

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

Loading history...
95 1
        if (empty($last_page)) {
96
            $last_page = [
97 1
                'controller' => 'reports',
98
                'action' => 'index'
99
            ];
100
        }
101 1
        $this->redirect($last_page);
102 1
    }
103
104 1
    public function logout()
105
    {
106 1
        $this->request->session()->destroy();
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::session() has been deprecated: 3.5.0 Use getSession() instead. The setter part will be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
        /** @scrutinizer ignore-deprecated */ $this->request->session()->destroy();

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

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

Loading history...
107
108 1
        $flash_class = 'alert alert-success';
109 1
        $this->Flash->default(
110 1
            'You have been logged out successfully',
111 1
            ['params' => ['class' => $flash_class]]
112
        );
113 1
        $this->redirect('/');
114 1
    }
115
116 1
    protected function _authenticateDeveloper($userInfo, $accessToken)
117
    {
118 1
        $developers = $this->Developers->findByGithubId($userInfo['id']);
0 ignored issues
show
Bug Best Practice introduced by
The property Developers does not exist on App\Controller\DevelopersController. Since you implemented __get, consider adding a @property annotation.
Loading history...
119 1
        $developer = $developers->all()->first();
120 1
        if (! $developer) {
121 1
            $developer = $this->Developers->newEntity();
122
        } else {
123 1
            $this->Developers->id = $developer['id'];
124
        }
125 1
        $this->Developers->id = $this->Developers->saveFromGithub($userInfo, $accessToken, $developer);
126 1
        $this->request->session()->write('Developer.id', $this->Developers->id);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::session() has been deprecated: 3.5.0 Use getSession() instead. The setter part will be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

126
        /** @scrutinizer ignore-deprecated */ $this->request->session()->write('Developer.id', $this->Developers->id);

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

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

Loading history...
127 1
        $this->request->session()->write('access_token', $accessToken);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::session() has been deprecated: 3.5.0 Use getSession() instead. The setter part will be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

127
        /** @scrutinizer ignore-deprecated */ $this->request->session()->write('access_token', $accessToken);

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

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

Loading history...
128 1
        $this->request->session()->write('read_only', ! ($userInfo['has_commit_access']));
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::session() has been deprecated: 3.5.0 Use getSession() instead. The setter part will be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

128
        /** @scrutinizer ignore-deprecated */ $this->request->session()->write('read_only', ! ($userInfo['has_commit_access']));

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

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

Loading history...
129 1
    }
130
}
131