implement control protection.

This commit is contained in:
Miroslav Šedivý
2024-04-21 20:10:16 +02:00
parent 3ee6078256
commit 0f45aa3f19
4 changed files with 91 additions and 1 deletions

View File

@ -37,6 +37,16 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
if err := h.systemAdmin(session); err != nil {
return err
}
// update settings in atomic way
h.sessions.UpdateSettingsFunc(func(settings *types.Settings) bool {
// if control protection & locked controls: unlock controls
if settings.LockedControls && settings.ControlProtection {
settings.LockedControls = false
return true // update settings
}
return false // do not update settings
})
}
return h.SessionStateChanged(session)
@ -49,6 +59,27 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
h.sessions.ClearHost()
}
if session.Profile().IsAdmin {
hasAdmin := false
h.sessions.Range(func(s types.Session) bool {
if s.Profile().IsAdmin && s.ID() != session.ID() && s.State().IsConnected {
hasAdmin = true
return false
}
return true
})
// update settings in atomic way
h.sessions.UpdateSettingsFunc(func(settings *types.Settings) bool {
// if control protection & not locked controls & no admin: lock controls
if !settings.LockedControls && settings.ControlProtection && !hasAdmin {
settings.LockedControls = true
return true // update settings
}
return false // do not update settings
})
}
return h.SessionStateChanged(session)
}