1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\Discourse\Listeners; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use Illuminate\Contracts\Config\Repository; |
8
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
9
|
|
|
use Illuminate\Support\Facades\Log; |
10
|
|
|
use Symfony\Component\HttpKernel\Log\Logger; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LogoutDiscourseUser |
14
|
|
|
* |
15
|
|
|
* Send a Logout request to Discourse for the corresponding Laravel User. |
16
|
|
|
* |
17
|
|
|
* @package Spinen\Discourse\Listeners |
18
|
|
|
*/ |
19
|
|
|
class LogoutDiscourseUser implements ShouldQueue |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Client |
23
|
|
|
*/ |
24
|
|
|
public $client; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Repository |
28
|
|
|
*/ |
29
|
|
|
public $config_repository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Logger |
33
|
|
|
*/ |
34
|
|
|
public $logger; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create the event listener. |
38
|
|
|
* |
39
|
|
|
* @param Client $client |
40
|
|
|
* @param Repository $config_repository |
41
|
|
|
* @param Logger $logger |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct(Client $client, Repository $config_repository, Logger $logger) |
46
|
|
|
{ |
47
|
5 |
|
$this->client = $client; |
48
|
5 |
|
$this->config_repository = $config_repository; |
49
|
5 |
|
$this->logger = $logger; |
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handle the event. |
54
|
|
|
* |
55
|
|
|
* @param mixed $event |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
4 |
|
public function handle($event) |
60
|
|
|
{ |
61
|
4 |
|
if (!$event->user) { |
62
|
1 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$configs = [ |
66
|
3 |
|
'base_uri' => $this->config_repository->get('services.discourse.url'), |
67
|
|
|
'headers' => [ |
68
|
3 |
|
'Api-Key' => $this->config_repository->get('services.discourse.api.key'), |
69
|
3 |
|
'Api-Username' => $this->config_repository->get('services.discourse.api.user'), |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
// Get Discourse user to match this one, and send a Logout request to Discourse and get the response |
75
|
3 |
|
$response = $this->client->get("users/by-external/{$event->user->id}.json", $configs); |
76
|
1 |
|
} catch (BadResponseException $e) { |
77
|
1 |
|
$response = $e->getResponse(); |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
if ($response->getStatusCode() !== 200) { |
81
|
1 |
|
$this->logger->error( |
82
|
1 |
|
"When getting user {$event->user->id} Discourse returned status code {$response->getStatusCode()}", |
83
|
1 |
|
['reason' => $response->getReasonPhrase()] |
84
|
|
|
); |
85
|
1 |
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$user = json_decode($response->getBody())->user; |
89
|
2 |
|
$response = $this->client->post("admin/users/{$user->id}/log_out", $configs); |
90
|
|
|
|
91
|
2 |
|
if ($response->getStatusCode() !== 200) { |
92
|
1 |
|
$this->logger->notice( |
93
|
1 |
|
"When logging out user {$event->user->id} Discourse returned status code {$response->getStatusCode()}:", |
94
|
1 |
|
['reason' => $response->getReasonPhrase()] |
95
|
|
|
); |
96
|
|
|
} |
97
|
2 |
|
} |
98
|
|
|
} |
99
|
|
|
|