Refactor broadcasting configuration and enhance chat authorization: update middleware in BroadcastServiceProvider, add support user check in ChatSupport, and improve chat.js for dynamic auth endpoint handling. Update channels.php to restrict access for support conversation.
This commit is contained in:
@@ -5,8 +5,6 @@ namespace App\Providers;
|
||||
use App\Http\Middleware\EnsureChatOperator;
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Nova\Http\Middleware\Authenticate as NovaAuthenticate;
|
||||
use Laravel\Nova\Http\Middleware\Authorize as NovaAuthorize;
|
||||
|
||||
class BroadcastServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -24,7 +22,7 @@ class BroadcastServiceProvider extends ServiceProvider
|
||||
|
||||
Broadcast::routes([
|
||||
'prefix' => 'panel',
|
||||
'middleware' => ['web', NovaAuthenticate::class, NovaAuthorize::class, EnsureChatOperator::class],
|
||||
'middleware' => ['web', 'auth', EnsureChatOperator::class],
|
||||
]);
|
||||
|
||||
require base_path('routes/channels.php');
|
||||
|
||||
@@ -47,6 +47,10 @@ class ChatSupport
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::isSupportUser($user)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $user->isMe() || $user->isAdmin() || $user->isDirektor() || $user->isManager();
|
||||
}
|
||||
|
||||
|
||||
@@ -328,13 +328,42 @@
|
||||
enabledTransports: ["ws", "wss"],
|
||||
disableStats: true,
|
||||
cluster: "mt1",
|
||||
authEndpoint: config.broadcastAuth,
|
||||
csrfToken: config.csrf,
|
||||
authEndpoint: config.broadcastAuthPath || config.broadcastAuth || "/panel/broadcasting/auth",
|
||||
auth: {
|
||||
headers: {
|
||||
"X-CSRF-TOKEN": config.csrf,
|
||||
Accept: "application/json",
|
||||
"X-Requested-With": "XMLHttpRequest",
|
||||
},
|
||||
},
|
||||
authorizer: (channel) => ({
|
||||
authorize: (socketId, callback) => {
|
||||
fetch(config.broadcastAuthPath || config.broadcastAuth || "/panel/broadcasting/auth", {
|
||||
method: "POST",
|
||||
credentials: "same-origin",
|
||||
headers: headers(true),
|
||||
body: JSON.stringify({
|
||||
socket_id: socketId,
|
||||
channel_name: channel.name,
|
||||
}),
|
||||
})
|
||||
.then(async (response) => {
|
||||
let data = {};
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (error) {
|
||||
data = {};
|
||||
}
|
||||
if (!response.ok) {
|
||||
callback(true, data);
|
||||
return;
|
||||
}
|
||||
callback(false, data);
|
||||
})
|
||||
.catch((error) => callback(true, error));
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
state.echo.private(config.inboxChannel || "chat.support").listen(".message.sent", upsertIncoming);
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
messagesUrl: @json(url('/panel/chat/conversations')),
|
||||
csrf: @json(csrf_token()),
|
||||
broadcastAuth: @json(url('/panel/broadcasting/auth')),
|
||||
broadcastAuthPath: '/panel/broadcasting/auth',
|
||||
pusher: @json($pusher),
|
||||
inboxChannel: @json($inboxChannel),
|
||||
strings: {
|
||||
|
||||
@@ -29,6 +29,10 @@ Broadcast::channel('chat.inbox.{userId}', function ($user, $userId) {
|
||||
});
|
||||
|
||||
Broadcast::channel('chat.{conversationId}', function ($user, $conversationId) {
|
||||
if ($conversationId === 'support') {
|
||||
return ChatSupport::isOperator($user);
|
||||
}
|
||||
|
||||
if (! ctype_digit((string) $conversationId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user