From cf4e0666fea4cedaea5df9cca1ce4c133fa2b129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 14 May 2023 22:41:32 +0200 Subject: [PATCH] add locked controls to settings. --- internal/api/room/control.go | 4 ++++ internal/session/manager.go | 10 ++++++++++ internal/websocket/handler/control.go | 4 ++++ pkg/types/session.go | 1 + 4 files changed, 19 insertions(+) diff --git a/internal/api/room/control.go b/internal/api/room/control.go index ad6d27b3..ddf04346 100644 --- a/internal/api/room/control.go +++ b/internal/api/room/control.go @@ -39,6 +39,10 @@ func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) err } session, _ := auth.GetSession(r) + if h.sessions.Settings().LockedControls && !session.Profile().IsAdmin { + return utils.HttpForbidden("controls are locked") + } + h.sessions.SetHost(session) return utils.HttpSuccess(w) diff --git a/internal/session/manager.go b/internal/session/manager.go index 2da614c4..828cbffd 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -20,6 +20,7 @@ func New(config *config.Session) *SessionManagerCtx { config: config, settings: types.Settings{ PrivateMode: false, // By default disabled. + LockedControls: false, // By default disabled. ImplicitHosting: config.ImplicitHosting, InactiveCursors: config.InactiveCursors, MercifulReconnect: config.MercifulReconnect, @@ -387,6 +388,15 @@ func (manager *SessionManagerCtx) UpdateSettings(new types.Settings) { } } + // if contols have been locked + if old.LockedControls != new.LockedControls && new.LockedControls { + // if the host is not admin, it must release controls + host, hasHost := manager.GetHost() + if hasHost && !host.Profile().IsAdmin { + manager.ClearHost() + } + } + manager.emmiter.Emit("settings_changed", new, old) } diff --git a/internal/websocket/handler/control.go b/internal/websocket/handler/control.go index 9a21cd1d..2eec4a3b 100644 --- a/internal/websocket/handler/control.go +++ b/internal/websocket/handler/control.go @@ -40,6 +40,10 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error { return ErrIsAlreadyTheHost } + if h.sessions.Settings().LockedControls && !session.Profile().IsAdmin { + return ErrIsNotAllowedToHost + } + if !h.sessions.Settings().ImplicitHosting { // tell session if there is a host if host, hasHost := h.sessions.GetHost(); hasHost { diff --git a/pkg/types/session.go b/pkg/types/session.go index b1c2057d..a872587e 100644 --- a/pkg/types/session.go +++ b/pkg/types/session.go @@ -30,6 +30,7 @@ type SessionState struct { type Settings struct { PrivateMode bool `json:"private_mode"` + LockedControls bool `json:"locked_controls"` ImplicitHosting bool `json:"implicit_hosting"` InactiveCursors bool `json:"inactive_cursors"` MercifulReconnect bool `json:"merciful_reconnect"`