|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class SlackAuthController |
|
6
|
|
|
* |
|
7
|
|
|
*/ |
|
8
|
|
|
class SlackAuthController extends Controller |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* It seems like a lot is going on here |
|
13
|
|
|
* But in reality, it's mainly just configuration stuff |
|
14
|
|
|
* |
|
15
|
|
|
* @param SS_HTTPRequest $request |
|
16
|
|
|
*/ |
|
17
|
|
|
public function index(SS_HTTPRequest $request) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
list($code, $config, $baseURL, $url) = $this->getConfig($request); |
|
20
|
|
|
|
|
21
|
|
|
$query = $this->getQuery($config, $code); |
|
22
|
|
|
|
|
23
|
|
|
// Setup and request the code |
|
24
|
|
|
// @todo rewrite to Guzzle for SS4 |
|
25
|
|
|
$service = RestfulService::create($baseURL, 'GET', null, 0); |
|
|
|
|
|
|
26
|
|
|
$response = $service->request($url . $query); |
|
27
|
|
|
|
|
28
|
|
|
$this->saveToken($response, $config); |
|
29
|
|
|
|
|
30
|
|
|
// A successful write should go back to the admin |
|
31
|
|
|
$this->redirect('/admin/settings#Root_Slack'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $config |
|
36
|
|
|
* @param $code |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getQuery($config, $code) |
|
40
|
|
|
{ |
|
41
|
|
|
$params = [ |
|
42
|
|
|
'client_id' => $config->SlackClientID, |
|
43
|
|
|
'client_secret' => $config->SlackClientSecret, |
|
44
|
|
|
'code' => $code, |
|
45
|
|
|
'redirect_uri' => Director::absoluteURL('/SlackAuthorization/'), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
return http_build_query($params); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param RestfulService_Response $response |
|
|
|
|
|
|
53
|
|
|
* @param SiteConfig $config |
|
|
|
|
|
|
54
|
|
|
* @throws \ValidationException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function saveToken($response, $config) |
|
57
|
|
|
{ |
|
58
|
|
|
// Convert the JSON to use in our config (hidden from user view) |
|
59
|
|
|
$result = Convert::json2array($response->getBody()); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$config->SlackToken = $result['access_token']; |
|
62
|
|
|
$config->write(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param SS_HTTPRequest $request |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getConfig(SS_HTTPRequest $request) |
|
70
|
|
|
{ |
|
71
|
|
|
// Code param |
|
72
|
|
|
$code = $request->getVar('code'); |
|
73
|
|
|
$config = SiteConfig::current_site_config(); |
|
74
|
|
|
// Build the URL |
|
75
|
|
|
$baseURL = $config->SlackURL; |
|
76
|
|
|
$baseURL = (substr($baseURL, -1) === '/') ? $baseURL : $baseURL . '/'; |
|
77
|
|
|
$url = 'api/oauth.access?'; |
|
78
|
|
|
|
|
79
|
|
|
return array($code, $config, $baseURL, $url); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths