filetransfer: use enabeld in settings and profile.

This commit is contained in:
Miroslav Šedivý 2024-06-16 17:27:42 +02:00
parent 5a04066c55
commit 43467dcac1
2 changed files with 64 additions and 48 deletions

View File

@ -1,6 +1,7 @@
package filetransfer package filetransfer
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -43,30 +44,25 @@ type Manager struct {
fileList []Item fileList []Item
} }
func (m *Manager) isEnabledForSession(session types.Session) bool { func (m *Manager) isEnabledForSession(session types.Session) (bool, error) {
canTransfer := true settings := Settings{
Enabled: true, // defaults to true
profile, ok := session.Profile().Plugins["filetransfer"] }
// by default, allow file transfer if the plugin config is not present err := m.sessions.Settings().Plugins.Unmarshal(PluginName, &settings)
if ok { if err != nil && !errors.Is(err, types.ErrPluginSettingsNotFound) {
canTransfer, ok = profile.(bool) return false, fmt.Errorf("unable to unmarshal %s plugin settings from global settings: %w", PluginName, err)
// if the plugin is present but not a boolean, allow file transfer
if !ok {
canTransfer = true
}
} }
settings, ok := m.sessions.Settings().Plugins["filetransfer"] profile := Settings{
// by default, allow file transfer if the plugin config is not present Enabled: true, // defaults to true
if ok && canTransfer && session.Profile().IsAdmin {
canTransfer, ok = settings.(bool)
// if the plugin is present but not a boolean, allow file transfer
if !ok {
canTransfer = true
}
} }
return m.config.Enabled && canTransfer err = session.Profile().Plugins.Unmarshal(PluginName, &profile)
if err != nil && !errors.Is(err, types.ErrPluginSettingsNotFound) {
return false, fmt.Errorf("unable to unmarshal %s plugin settings from profile: %w", PluginName, err)
}
return m.config.Enabled && (settings.Enabled || session.Profile().IsAdmin) && profile.Enabled, nil
} }
func (m *Manager) refresh() (error, bool) { func (m *Manager) refresh() (error, bool) {
@ -102,31 +98,32 @@ func (m *Manager) refresh() (error, bool) {
func (m *Manager) broadcastUpdate() { func (m *Manager) broadcastUpdate() {
m.mu.RLock() m.mu.RLock()
defer m.mu.RUnlock() fileList := m.fileList
m.mu.RUnlock()
m.sessions.Broadcast(FILETRANSFER_UPDATE, Message{ m.sessions.Broadcast(FILETRANSFER_UPDATE, Message{
Enabled: m.config.Enabled, Enabled: m.config.Enabled,
RootDir: m.config.RootDir, RootDir: m.config.RootDir,
Files: m.fileList, Files: fileList,
})
}
func (m *Manager) sendUpdate(session types.Session) {
m.mu.RLock()
fileList := m.fileList
m.mu.RUnlock()
session.Send(FILETRANSFER_UPDATE, Message{
Enabled: m.config.Enabled,
RootDir: m.config.RootDir,
Files: fileList,
}) })
} }
func (m *Manager) Start() error { func (m *Manager) Start() error {
// send init message once a user connects // send init message once a user connects
m.sessions.OnConnected(func(session types.Session) { m.sessions.OnConnected(func(session types.Session) {
isEnabled := m.isEnabledForSession(session) m.sendUpdate(session)
// get file list
m.mu.RLock()
fileList := m.fileList
m.mu.RUnlock()
// send init message
session.Send(FILETRANSFER_UPDATE, Message{
Enabled: isEnabled,
RootDir: m.config.RootDir,
Files: fileList,
})
}) })
// if file transfer is disabled, return immediately without starting the watcher // if file transfer is disabled, return immediately without starting the watcher
@ -192,6 +189,15 @@ func (m *Manager) Start() error {
return fmt.Errorf("unable to watch file transfer dir: %w", err) return fmt.Errorf("unable to watch file transfer dir: %w", err)
} }
// initial refresh
err, changed := m.refresh()
if err != nil {
return fmt.Errorf("unable to refresh file transfer list: %w", err)
}
if changed {
m.broadcastUpdate()
}
return nil return nil
} }
@ -214,22 +220,16 @@ func (m *Manager) WebSocketHandler(session types.Session, msg types.WebSocketMes
} }
if changed { if changed {
// broadcast update message to all clients
m.broadcastUpdate() m.broadcastUpdate()
} else { } else {
// get file list
m.mu.RLock()
fileList := m.fileList
m.mu.RUnlock()
// send update message to this client only // send update message to this client only
session.Send(FILETRANSFER_UPDATE, Message{ m.sendUpdate(session)
Enabled: m.config.Enabled,
RootDir: m.config.RootDir,
Files: fileList,
})
} }
return true return true
} }
// not handled by this plugin
return false return false
} }
@ -239,7 +239,13 @@ func (m *Manager) downloadFileHandler(w http.ResponseWriter, r *http.Request) er
return utils.HttpUnauthorized("session not found") return utils.HttpUnauthorized("session not found")
} }
enabled := m.isEnabledForSession(session) enabled, err := m.isEnabledForSession(session)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
Msg("error checking file transfer permissions")
}
if !enabled { if !enabled {
return utils.HttpForbidden("file transfer is disabled") return utils.HttpForbidden("file transfer is disabled")
} }
@ -267,12 +273,18 @@ func (m *Manager) uploadFileHandler(w http.ResponseWriter, r *http.Request) erro
return utils.HttpUnauthorized("session not found") return utils.HttpUnauthorized("session not found")
} }
enabled := m.isEnabledForSession(session) enabled, err := m.isEnabledForSession(session)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
Msg("error checking file transfer permissions")
}
if !enabled { if !enabled {
return utils.HttpForbidden("file transfer is disabled") return utils.HttpForbidden("file transfer is disabled")
} }
err := r.ParseMultipartForm(MULTIPART_FORM_MAX_MEMORY) err = r.ParseMultipartForm(MULTIPART_FORM_MAX_MEMORY)
if err != nil || r.MultipartForm == nil { if err != nil || r.MultipartForm == nil {
return utils.HttpBadRequest(). return utils.HttpBadRequest().
WithInternalErr(err). WithInternalErr(err).

View File

@ -2,6 +2,10 @@ package filetransfer
const PluginName = "filetransfer" const PluginName = "filetransfer"
type Settings struct {
Enabled bool `json:"enabled" mapstructure:"enabled"`
}
const ( const (
FILETRANSFER_UPDATE = "filetransfer/update" FILETRANSFER_UPDATE = "filetransfer/update"
) )