fix locks.

This commit is contained in:
Miroslav Šedivý 2024-07-21 15:29:37 +02:00
parent 75ad4cefe6
commit 4adf1d7182
3 changed files with 129 additions and 17 deletions

View File

@ -23,6 +23,9 @@ type session struct {
client *http.Client client *http.Client
lastHostID string lastHostID string
lockedControls bool
lockedLogins bool
lockedFileTransfer bool
sessions map[string]*oldTypes.Member sessions map[string]*oldTypes.Member
connClient *websocket.Conn connClient *websocket.Conn
@ -60,11 +63,15 @@ func (s *session) apiReq(method, path string, request, response any) error {
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != http.StatusOK { if res.StatusCode < 200 || res.StatusCode >= 300 {
body, _ := io.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
return fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, body) return fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, body)
} }
if res.Body == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(response) return json.NewDecoder(res.Body).Decode(response)
} }

View File

@ -242,8 +242,22 @@ func (s *session) wsToBackend(msg []byte, sendMsg func([]byte) error) error {
return err return err
} }
// TODO: No WS equivalent, call HTTP API. data := map[string]any{}
return fmt.Errorf("event not implemented: %s", header.Event)
switch request.Resource {
case "login":
data["locked_logins"] = true
case "control":
data["locked_controls"] = true
case "file_transfer":
data["plugins"] = map[string]any{
"filetransfer.enabled": false,
}
default:
return fmt.Errorf("unknown resource: %s", request.Resource)
}
return s.apiReq(http.MethodPost, "/api/room/settings", data, nil)
case oldEvent.ADMIN_UNLOCK: case oldEvent.ADMIN_UNLOCK:
request := &oldMessage.AdminLock{} request := &oldMessage.AdminLock{}
@ -252,8 +266,22 @@ func (s *session) wsToBackend(msg []byte, sendMsg func([]byte) error) error {
return err return err
} }
// TODO: No WS equivalent, call HTTP API. data := map[string]any{}
return fmt.Errorf("event not implemented: %s", header.Event)
switch request.Resource {
case "login":
data["locked_logins"] = false
case "control":
data["locked_controls"] = false
case "file_transfer":
data["plugins"] = map[string]any{
"filetransfer.enabled": true,
}
default:
return fmt.Errorf("unknown resource: %s", request.Resource)
}
return s.apiReq(http.MethodPost, "/api/room/settings", data, nil)
case oldEvent.ADMIN_CONTROL: case oldEvent.ADMIN_CONTROL:
return s.apiReq(http.MethodPost, "/api/room/control/take", nil, nil) return s.apiReq(http.MethodPost, "/api/room/control/take", nil, nil)

View File

@ -196,12 +196,15 @@ func (s *session) wsToClient(msg []byte, sendMsg func([]byte) error) error {
locks := map[string]string{} locks := map[string]string{}
if request.Settings.LockedLogins { if request.Settings.LockedLogins {
locks["login"] = "" // TODO: We don't know who locked the login. locks["login"] = "" // TODO: We don't know who locked the login.
s.lockedLogins = true
} }
if request.Settings.LockedControls { if request.Settings.LockedControls {
locks["control"] = "" // TODO: We don't know who locked the control. locks["control"] = "" // TODO: We don't know who locked the control.
s.lockedControls = true
} }
if !filetransferSettings.Enabled { if !filetransferSettings.Enabled {
locks["filetransfer"] = "" // TODO: We don't know who locked the file transfer. locks["file_transfer"] = "" // TODO: We don't know who locked the file transfer.
s.lockedFileTransfer = true
} }
return send(&oldMessage.SystemInit{ return send(&oldMessage.SystemInit{
@ -540,16 +543,90 @@ func (s *session) wsToClient(msg []byte, sendMsg func([]byte) error) error {
IsActive: request.IsActive, IsActive: request.IsActive,
}) })
/*
// Admin Events // Admin Events
case: case event.SYSTEM_SETTINGS:
send(&oldMessage.AdminLock{ request := &message.SystemSettingsUpdate{}
err := json.Unmarshal(data.Payload, request)
if err != nil {
return err
}
if s.lockedControls != request.LockedControls {
s.lockedControls = request.LockedControls
if request.LockedControls {
err = send(&oldMessage.AdminLock{
Event: oldEvent.ADMIN_LOCK, Event: oldEvent.ADMIN_LOCK,
Resource: "control",
}) })
case: } else {
send(&oldMessage.AdminLock{ err = send(&oldMessage.AdminLock{
Event: oldEvent.ADMIN_UNLOCK, Event: oldEvent.ADMIN_UNLOCK,
Resource: "control",
}) })
}
if err != nil {
return err
}
}
if s.lockedLogins != request.LockedLogins {
s.lockedLogins = request.LockedLogins
if request.LockedLogins {
err = send(&oldMessage.AdminLock{
Event: oldEvent.ADMIN_LOCK,
Resource: "login",
})
} else {
err = send(&oldMessage.AdminLock{
Event: oldEvent.ADMIN_UNLOCK,
Resource: "login",
})
}
if err != nil {
return err
}
}
//
// FileTransfer
//
filetransferSettings := filetransfer.Settings{
Enabled: true, // defaults to true
}
err = request.Settings.Plugins.Unmarshal(filetransfer.PluginName, &filetransferSettings)
if err != nil && !errors.Is(err, types.ErrPluginSettingsNotFound) {
return fmt.Errorf("unable to unmarshal %s plugin settings from global settings: %w", filetransfer.PluginName, err)
}
if s.lockedFileTransfer != !filetransferSettings.Enabled {
s.lockedFileTransfer = !filetransferSettings.Enabled
if !filetransferSettings.Enabled {
err = send(&oldMessage.AdminLock{
Event: oldEvent.ADMIN_LOCK,
Resource: "file_transfer",
})
} else {
err = send(&oldMessage.AdminLock{
Event: oldEvent.ADMIN_UNLOCK,
Resource: "file_transfer",
})
}
if err != nil {
return err
}
}
return nil
/*
case: case:
send(&oldMessage.AdminTarget{ send(&oldMessage.AdminTarget{
Event: oldEvent.ADMIN_BAN, Event: oldEvent.ADMIN_BAN,