1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace denis660\Centrifuge; |
6
|
|
|
|
7
|
|
|
use denis660\Centrifuge\Contracts\CentrifugeInterface; |
8
|
|
|
use Exception; |
9
|
|
|
use Illuminate\Broadcasting\Broadcasters\Broadcaster; |
10
|
|
|
use Illuminate\Broadcasting\BroadcastException; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
12
|
|
|
|
13
|
|
|
class CentrifugeBroadcaster extends Broadcaster |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The Centrifuge SDK instance. |
17
|
|
|
* |
18
|
|
|
* @var \denis660\Centrifuge\Contracts\CentrifugeInterface |
19
|
|
|
*/ |
20
|
|
|
protected $Centrifuge; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new broadcaster instance. |
24
|
|
|
* |
25
|
|
|
* @param \denis660\Centrifuge\Contracts\CentrifugeInterface $Centrifuge |
26
|
|
|
*/ |
27
|
|
|
public function __construct(CentrifugeInterface $Centrifuge) |
28
|
|
|
{ |
29
|
|
|
$this->Centrifuge = $Centrifuge; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Authenticate the incoming request for a given channel. |
34
|
|
|
* |
35
|
|
|
* @param \Illuminate\Http\Request $request |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function auth($request) |
39
|
|
|
{ |
40
|
|
|
if ($request->user()) { |
41
|
|
|
$client = $this->getClientFromRequest($request); |
42
|
|
|
$channels = $this->getChannelsFromRequest($request); |
43
|
|
|
|
44
|
|
|
$response = []; |
45
|
|
|
foreach ($channels as $channel) { |
46
|
|
|
$channelName = $this->getChannelName($channel); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$is_access_granted = $this->verifyUserCanAccessChannel($request, $channelName); |
50
|
|
|
} catch (HttpException $e) { |
51
|
|
|
$is_access_granted = false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$response[$channel] = $this->makeResponseForClient($is_access_granted, $client); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return response()->json($response); |
|
|
|
|
58
|
|
|
} else { |
59
|
|
|
throw new HttpException(401); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return the valid authentication response. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Http\Request $request |
67
|
|
|
* @param mixed $result |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function validAuthenticationResponse($request, $result) |
71
|
|
|
{ |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Broadcast the given event. |
77
|
|
|
* |
78
|
|
|
* @param array $channels |
79
|
|
|
* @param string $event |
80
|
|
|
* @param array $payload |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function broadcast(array $channels, $event, array $payload = []) |
84
|
|
|
{ |
85
|
|
|
$payload['event'] = $event; |
86
|
|
|
|
87
|
|
|
$response = $this->Centrifuge->broadcast($this->formatChannels($channels), $payload); |
88
|
|
|
|
89
|
|
|
if (is_array($response) && ! isset($response['error'])) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new BroadcastException( |
94
|
|
|
$response['error'] instanceof Exception ? $response['error']->getMessage() : $response['error'] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get client from request. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Http\Request $request |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function getClientFromRequest($request) |
105
|
|
|
{ |
106
|
|
|
return $request->get('client', ''); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get channels from request. |
111
|
|
|
* |
112
|
|
|
* @param \Illuminate\Http\Request $request |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private function getChannelsFromRequest($request) |
116
|
|
|
{ |
117
|
|
|
$channels = $request->get('channels', []); |
118
|
|
|
|
119
|
|
|
return is_array($channels) ? $channels : [$channels]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get channel name without $ symbol (if present). |
124
|
|
|
* |
125
|
|
|
* @param string $channel |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
private function getChannelName(string $channel) |
129
|
|
|
{ |
130
|
|
|
return (substr($channel, 0, 1) === '$') ? substr($channel, 1) : $channel; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Make response for client, based on access rights. |
135
|
|
|
* |
136
|
|
|
* @param bool $access_granted |
137
|
|
|
* @param string $client |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function makeResponseForClient(bool $access_granted, string $client) |
141
|
|
|
{ |
142
|
|
|
$info = []; |
143
|
|
|
|
144
|
|
|
return $access_granted ? [ |
145
|
|
|
'sign' => $this->Centrifuge->generateConnectionToken($client, 0, $info), |
146
|
|
|
'info' => $info, |
147
|
|
|
] : [ |
148
|
|
|
'status' => 403, |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: