Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

DevelopersController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Developer controller handling developer login/logout/register.
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Controller;
20
21
use Cake\Core\Configure;
22
use Cake\Http\Response;
23
use App\Controller\Component\GithubApiComponent;
24
25
/**
26
 * Developer controller handling developer login/logout/register.
27
 *
28
 * @property GithubApiComponent $GithubApi
29
 */
30
class DevelopersController extends AppController
31
{
32
    /**
33
     * Initialization hook method.
34
     *
35
     * Use this method to add common initialization code like loading components.
36
     *
37
     * @return void Nothing
38
     */
39 21
    public function initialize(): void
40
    {
41 21
        parent::initialize();
42 21
        $this->loadComponent('GithubApi');
43 21
        $this->viewBuilder()->setHelpers([
44 21
            'Html',
45
            'Form',
46
        ]);
47 21
    }
48
49 7
    public function login(): void
50
    {
51 7
        $url = $this->GithubApi->getRedirectUrl('user:email,public_repo');
52 7
        $this->redirect($url);
53 7
    }
54
55 7
    public function callback(): ?Response
56
    {
57 7
        $code = $this->request->getQuery('code');
58 7
        $accessToken = $this->GithubApi->getAccessToken($code);
59 7
        if (empty($code) || empty($accessToken)) {
60 7
            $flash_class = 'alert alert-error';
61 7
            $this->Flash->set(
62
                'We were not able to authenticate you.'
63 7
                    . ' Please try again later',
64 7
                ['params' => ['class' => $flash_class]]
65
            );
66
67 7
            return $this->redirect('/');
68
        }
69
70 7
        [$userInfo, $status] = $this->GithubApi->getUserInfo($accessToken);
71 7
        if ($status !== 200) {
72 7
            $flash_class = 'alert alert-error';
73 7
            $this->Flash->set(
74 7
                $userInfo['message'],
75 7
                ['params' => ['class' => $flash_class]]
76
            );
77
78 7
            return $this->redirect('/');
79
        }
80
81 7
        $userInfo['has_commit_access'] = $this->GithubApi->canCommitTo(
82 7
            $userInfo['login'],
83 7
            Configure::read('GithubRepoPath'),
84 7
            Configure::read('GithubAccessToken')
85
        );
86
87 7
        $this->authenticateDeveloper($userInfo, $accessToken);
88
89 7
        $flash_class = 'alert alert-success';
90 7
        $this->Flash->set(
91 7
            'You have been logged in successfully',
92 7
            ['params' => ['class' => $flash_class]]
93
        );
94
95 7
        $last_page = $this->request->getSession()->read('last_page');
96 7
        if (empty($last_page)) {
97 2
            $last_page = [
98 5
                'controller' => 'reports',
99
                'action' => 'index',
100
            ];
101
        }
102
103 7
        return $this->redirect($last_page);
104
    }
105
106 7
    public function logout(): void
107
    {
108 7
        $this->request->getSession()->destroy();
109
110 7
        $flash_class = 'alert alert-success';
111 7
        $this->Flash->set(
112 7
            'You have been logged out successfully',
113 7
            ['params' => ['class' => $flash_class]]
114
        );
115 7
        $this->redirect('/');
116 7
    }
117
118 7
    protected function authenticateDeveloper(array $userInfo, string $accessToken): void
119
    {
120 7
        $developers = $this->Developers->findByGithubId($userInfo['id']);
121 7
        $developer = $developers->all()->first();
122 7
        if (! $developer) {
123 7
            $developer = $this->Developers->newEmptyEntity();
124
        } else {
125 7
            $this->Developers->id = $developer['id'];
126
        }
127 7
        $this->Developers->id = $this->Developers->saveFromGithub($userInfo, $accessToken, $developer);
128 7
        $this->request->getSession()->write('Developer.id', $this->Developers->id);
129 7
        $this->request->getSession()->write('access_token', $accessToken);
130 7
        $this->request->getSession()->write('read_only', ! $userInfo['has_commit_access']);
131 7
    }
132
}
133