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'); |
|
|
|
|
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'); |
|
|
|
|
49
|
1 |
|
$this->redirect($url); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
1 |
|
public function callback() |
53
|
|
|
{ |
54
|
1 |
|
$code = $this->request->query('code'); |
|
|
|
|
55
|
1 |
|
$accessToken = $this->GithubApi->getAccessToken($code); |
|
|
|
|
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'); |
|
|
|
|
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(); |
|
|
|
|
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']); |
|
|
|
|
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); |
|
|
|
|
127
|
1 |
|
$this->request->session()->write('access_token', $accessToken); |
|
|
|
|
128
|
1 |
|
$this->request->session()->write('read_only', ! ($userInfo['has_commit_access'])); |
|
|
|
|
129
|
1 |
|
} |
130
|
|
|
} |
131
|
|
|
|