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