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) {
members := []MemberDataPayload{}
for _, session := range h.sessions.List() {
profile := session.GetProfile()
profile := session.Profile()
members = append(members, MemberDataPayload{
ID: session.ID(),
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) {
member := GetMember(r)
profile := member.GetProfile()
profile := member.Profile()
utils.HttpSuccess(w, profile)
}
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
profile := member.GetProfile()
profile := member.Profile()
if !utils.HttpJsonRequest(w, r, &profile) {
return

View File

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

View File

@ -89,7 +89,7 @@ func (h *RoomHandler) Route(r chi.Router) {
func (h *RoomHandler) uploadMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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.")
} else {
next.ServeHTTP(w, r)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,7 @@ import (
)
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")
return nil
}

View File

@ -7,7 +7,7 @@ import (
)
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")
return nil
}
@ -24,7 +24,7 @@ func (h *MessageHandlerCtx) controlRelease(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")
return nil
}

View File

@ -7,7 +7,7 @@ import (
)
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")
return nil
}

View File

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

View File

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

View File

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

View File

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