session profile decoupled.

This commit is contained in:
Miroslav Šedivý 2021-03-14 00:45:51 +01:00
parent 7d4f7694b9
commit 4a28307c1e
15 changed files with 38 additions and 79 deletions

View File

@ -15,7 +15,7 @@ type MemberDataPayload struct {
func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) {
members := []MemberDataPayload{} members := []MemberDataPayload{}
for _, session := range h.sessions.List() { for _, session := range h.sessions.List() {
profile := session.GetProfile() profile := session.Profile()
members = append(members, MemberDataPayload{ members = append(members, MemberDataPayload{
ID: session.ID(), ID: session.ID(),
MemberProfile: &profile, MemberProfile: &profile,
@ -73,14 +73,14 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
profile := member.GetProfile() profile := member.Profile()
utils.HttpSuccess(w, profile) utils.HttpSuccess(w, profile)
} }
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
profile := member.GetProfile() profile := member.Profile()
if !utils.HttpJsonRequest(w, r, &profile) { if !utils.HttpJsonRequest(w, r, &profile) {
return return

View File

@ -41,7 +41,7 @@ func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) {
} }
session := auth.GetSession(r) session := auth.GetSession(r)
if !session.CanHost() { if !session.Profile().CanHost {
utils.HttpBadRequest(w, "Session is not allowed to host.") utils.HttpBadRequest(w, "Session is not allowed to host.")
return return
} }
@ -58,7 +58,7 @@ func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
return return
} }
if !session.CanHost() { if !session.Profile().CanHost {
utils.HttpBadRequest(w, "Session is not allowed to host.") utils.HttpBadRequest(w, "Session is not allowed to host.")
return return
} }
@ -71,7 +71,7 @@ func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) {
session := auth.GetSession(r) session := auth.GetSession(r)
if !session.CanHost() { if !session.Profile().CanHost {
utils.HttpBadRequest(w, "Session is not allowed to host.") utils.HttpBadRequest(w, "Session is not allowed to host.")
return return
} }
@ -90,7 +90,7 @@ func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) {
return return
} }
if !target.CanHost() { if !target.Profile().CanHost {
utils.HttpBadRequest(w, "Target session is not allowed to host.") utils.HttpBadRequest(w, "Target session is not allowed to host.")
return return
} }

View File

@ -89,7 +89,7 @@ func (h *RoomHandler) Route(r chi.Router) {
func (h *RoomHandler) uploadMiddleware(next http.Handler) http.Handler { func (h *RoomHandler) uploadMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := auth.GetSession(r) session := auth.GetSession(r)
if !session.IsHost() && (!session.CanHost() || !h.sessions.ImplicitHosting()) { if !session.IsHost() && (!session.Profile().CanHost || !h.sessions.ImplicitHosting()) {
utils.HttpForbidden(w, "Without implicit hosting, only host can upload files.") utils.HttpForbidden(w, "Without implicit hosting, only host can upload files.")
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)

View File

@ -62,8 +62,8 @@ func (api *ApiManagerCtx) Login(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w, SessionDataPayload{ utils.HttpSuccess(w, SessionDataPayload{
ID: session.ID(), ID: session.ID(),
Profile: session.GetProfile(), Profile: session.Profile(),
State: session.GetState(), State: session.State(),
}) })
} }
@ -99,7 +99,7 @@ func (api *ApiManagerCtx) Whoami(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w, SessionDataPayload{ utils.HttpSuccess(w, SessionDataPayload{
ID: session.ID(), ID: session.ID(),
Profile: session.GetProfile(), Profile: session.Profile(),
State: session.GetState(), State: session.State(),
}) })
} }

View File

@ -26,7 +26,7 @@ func GetSession(r *http.Request) types.Session {
func AdminsOnly(next http.Handler) http.Handler { func AdminsOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := GetSession(r) session := GetSession(r)
if !session.IsAdmin() { if !session.Profile().IsAdmin {
utils.HttpForbidden(w) utils.HttpForbidden(w)
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
@ -48,7 +48,7 @@ func HostsOnly(next http.Handler) http.Handler {
func HostsOrAdminsOnly(next http.Handler) http.Handler { func HostsOrAdminsOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := GetSession(r) session := GetSession(r)
if !session.IsHost() && !session.IsAdmin() { if !session.IsHost() && !session.Profile().IsAdmin {
utils.HttpForbidden(w, "Only host can do this.") utils.HttpForbidden(w, "Only host can do this.")
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
@ -59,7 +59,7 @@ func HostsOrAdminsOnly(next http.Handler) http.Handler {
func CanHostOnly(next http.Handler) http.Handler { func CanHostOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := GetSession(r) session := GetSession(r)
if !session.CanHost() { if !session.Profile().CanHost {
utils.HttpForbidden(w, "Only for sessions, that can host.") utils.HttpForbidden(w, "Only for sessions, that can host.")
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
@ -70,7 +70,7 @@ func CanHostOnly(next http.Handler) http.Handler {
func CanWatchOnly(next http.Handler) http.Handler { func CanWatchOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
session := GetSession(r) session := GetSession(r)
if !session.CanWatch() { if !session.Profile().CanWatch {
utils.HttpForbidden(w, "Only for sessions, that can watch.") utils.HttpForbidden(w, "Only for sessions, that can watch.")
} else { } else {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)

View File

@ -196,7 +196,7 @@ func (manager *SessionManagerCtx) AdminBroadcast(v interface{}, exclude interfac
defer manager.sessionsMu.Unlock() defer manager.sessionsMu.Unlock()
for id, session := range manager.sessions { for id, session := range manager.sessions {
if !session.IsConnected() || !session.IsAdmin() { if !session.IsConnected() || !session.Profile().IsAdmin {
continue continue
} }

View File

@ -23,54 +23,22 @@ func (session *SessionCtx) ID() string {
return session.id return session.id
} }
// --- func (session *SessionCtx) Profile() types.MemberProfile {
// profile
// ---
func (session *SessionCtx) Name() string {
return session.profile.Name
}
func (session *SessionCtx) IsAdmin() bool {
return session.profile.IsAdmin
}
func (session *SessionCtx) CanLogin() bool {
return session.profile.CanLogin
}
func (session *SessionCtx) CanConnect() bool {
return session.profile.CanConnect
}
func (session *SessionCtx) CanWatch() bool {
return session.profile.CanWatch
}
func (session *SessionCtx) CanHost() bool {
return session.profile.CanHost
}
func (session *SessionCtx) CanAccessClipboard() bool {
return session.profile.CanAccessClipboard
}
func (session *SessionCtx) GetProfile() types.MemberProfile {
return session.profile return session.profile
} }
func (session *SessionCtx) profileChanged() { func (session *SessionCtx) profileChanged() {
if !session.CanHost() && session.IsHost() { if !session.profile.CanHost && session.IsHost() {
session.manager.ClearHost() session.manager.ClearHost()
} }
if !session.CanWatch() && session.state.IsWatching { if !session.profile.CanWatch && session.state.IsWatching {
if err := session.webrtcPeer.Destroy(); err != nil { if err := session.webrtcPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed") session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
} }
} }
if (!session.CanConnect() || !session.CanLogin()) && session.state.IsConnected { if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
if err := session.Disconnect("profile changed"); err != nil { if err := session.Disconnect("profile changed"); err != nil {
session.logger.Warn().Err(err).Msgf("websocket destroy has failed") session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
} }
@ -89,7 +57,7 @@ func (session *SessionCtx) IsConnected() bool {
return session.state.IsConnected return session.state.IsConnected
} }
func (session *SessionCtx) GetState() types.SessionState { func (session *SessionCtx) State() types.SessionState {
return session.state return session.state
} }

View File

@ -19,21 +19,12 @@ type SessionState struct {
type Session interface { type Session interface {
ID() string ID() string
Profile() MemberProfile
// profile
Name() string
IsAdmin() bool
CanLogin() bool
CanConnect() bool
CanWatch() bool
CanHost() bool
CanAccessClipboard() bool
GetProfile() MemberProfile
// state // state
IsHost() bool IsHost() bool
IsConnected() bool IsConnected() bool
GetState() SessionState State() SessionState
// websocket // websocket
SetWebSocketPeer(websocketPeer WebSocketPeer) SetWebSocketPeer(websocketPeer WebSocketPeer)

View File

@ -6,7 +6,7 @@ import (
) )
func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message.ClipboardData) error { func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message.ClipboardData) error {
if !session.CanAccessClipboard() { if !session.Profile().CanAccessClipboard {
h.logger.Debug().Str("session_id", session.ID()).Msg("cannot access clipboard") h.logger.Debug().Str("session_id", session.ID()).Msg("cannot access clipboard")
return nil return nil
} }

View File

@ -7,7 +7,7 @@ import (
) )
func (h *MessageHandlerCtx) controlRelease(session types.Session) error { func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
if !session.CanHost() { if !session.Profile().CanHost {
h.logger.Debug().Str("session_id", session.ID()).Msg("is not allowed to host") h.logger.Debug().Str("session_id", session.ID()).Msg("is not allowed to host")
return nil return nil
} }
@ -24,7 +24,7 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
} }
func (h *MessageHandlerCtx) controlRequest(session types.Session) error { func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
if !session.CanHost() { if !session.Profile().CanHost {
h.logger.Debug().Str("session_id", session.ID()).Msg("is not allowed to host") h.logger.Debug().Str("session_id", session.ID()).Msg("is not allowed to host")
return nil return nil
} }

View File

@ -7,7 +7,7 @@ import (
) )
func (h *MessageHandlerCtx) screenSet(session types.Session, payload *message.ScreenSize) error { func (h *MessageHandlerCtx) screenSet(session types.Session, payload *message.ScreenSize) error {
if !session.IsAdmin() { if !session.Profile().IsAdmin {
h.logger.Debug().Str("session_id", session.ID()).Msg("is not the admin") h.logger.Debug().Str("session_id", session.ID()).Msg("is not the admin")
return nil return nil
} }

View File

@ -11,8 +11,8 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
message.SessionData{ message.SessionData{
Event: event.SESSION_CREATED, Event: event.SESSION_CREATED,
ID: session.ID(), ID: session.ID(),
Profile: session.GetProfile(), Profile: session.Profile(),
State: session.GetState(), State: session.State(),
}, nil) }, nil)
return nil return nil
@ -33,7 +33,7 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
return err return err
} }
if session.IsAdmin() { if session.Profile().IsAdmin {
if err := h.systemAdmin(session); err != nil { if err := h.systemAdmin(session); err != nil {
return err return err
} }
@ -53,7 +53,7 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
} }
func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error { func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
profile := session.GetProfile() profile := session.Profile()
h.sessions.Broadcast( h.sessions.Broadcast(
message.MemberProfile{ message.MemberProfile{
@ -66,7 +66,7 @@ func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
} }
func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error { func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error {
state := session.GetState() state := session.State()
h.sessions.Broadcast( h.sessions.Broadcast(
message.SessionState{ message.SessionState{

View File

@ -7,7 +7,7 @@ import (
) )
func (h *MessageHandlerCtx) signalRequest(session types.Session) error { func (h *MessageHandlerCtx) signalRequest(session types.Session) error {
if !session.CanWatch() { if !session.Profile().CanWatch {
return nil return nil
} }

View File

@ -29,8 +29,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
sessionId := session.ID() sessionId := session.ID()
sessions[sessionId] = message.SessionData{ sessions[sessionId] = message.SessionData{
ID: sessionId, ID: sessionId,
Profile: session.GetProfile(), Profile: session.Profile(),
State: session.GetState(), State: session.State(),
} }
} }

View File

@ -107,7 +107,7 @@ func (manager *WebSocketManagerCtx) Start() {
manager.desktop.OnClipboardUpdated(func() { manager.desktop.OnClipboardUpdated(func() {
session := manager.sessions.GetHost() session := manager.sessions.GetHost()
if session == nil || !session.CanAccessClipboard() { if session == nil || !session.Profile().CanAccessClipboard {
return return
} }
@ -167,7 +167,7 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
return connection.Close() return connection.Close()
} }
if !session.CanConnect() { if !session.Profile().CanConnect {
// TODO: Refactor, return error code. // TODO: Refactor, return error code.
if err = connection.WriteJSON( if err = connection.WriteJSON(
message.SystemDisconnect{ message.SystemDisconnect{