IsAdmin + IsConnected.

This commit is contained in:
Miroslav Šedivý 2020-12-02 16:49:51 +01:00
parent 25fec63455
commit 123fbd8317
8 changed files with 16 additions and 16 deletions

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.Admin() { if !session.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.Admin() { if !session.IsHost() && !session.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)

View File

@ -87,7 +87,7 @@ func (manager *SessionManagerCtx) Delete(id string) error {
delete(manager.members, id) delete(manager.members, id)
if session.Connected() { if session.IsConnected() {
return session.Disconnect("member deleted") return session.Disconnect("member deleted")
} }
@ -137,7 +137,7 @@ func (manager *SessionManagerCtx) Admins() []types.Session {
var sessions []types.Session var sessions []types.Session
for _, session := range manager.members { for _, session := range manager.members {
if !session.Connected() || !session.Admin() { if !session.IsConnected() || !session.IsAdmin() {
continue continue
} }
@ -153,7 +153,7 @@ func (manager *SessionManagerCtx) Members() []types.Session {
var sessions []types.Session var sessions []types.Session
for _, session := range manager.members { for _, session := range manager.members {
if !session.Connected() { if !session.IsConnected() {
continue continue
} }
@ -168,7 +168,7 @@ func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{})
defer manager.membersMu.Unlock() defer manager.membersMu.Unlock()
for id, session := range manager.members { for id, session := range manager.members {
if !session.Connected() { if !session.IsConnected() {
continue continue
} }
@ -189,7 +189,7 @@ func (manager *SessionManagerCtx) AdminBroadcast(v interface{}, exclude interfac
defer manager.membersMu.Unlock() defer manager.membersMu.Unlock()
for id, session := range manager.members { for id, session := range manager.members {
if !session.Connected() || !session.Admin() { if !session.IsConnected() || !session.IsAdmin() {
continue continue
} }

View File

@ -27,7 +27,7 @@ func (session *SessionCtx) Name() string {
return session.profile.Name return session.profile.Name
} }
func (session *SessionCtx) Admin() bool { func (session *SessionCtx) IsAdmin() bool {
return session.profile.IsAdmin return session.profile.IsAdmin
} }
@ -39,7 +39,7 @@ func (session *SessionCtx) VerifySecret(secret string) bool {
return session.profile.Secret == secret return session.profile.Secret == secret
} }
func (session *SessionCtx) Connected() bool { func (session *SessionCtx) IsConnected() bool {
// TODO: Refactor. // TODO: Refactor.
return session.websocket_connected// && session.webrtc_connected return session.websocket_connected// && session.webrtc_connected
} }

View File

@ -16,9 +16,9 @@ type MemberProfile struct {
type Session interface { type Session interface {
ID() string ID() string
Name() string Name() string
Admin() bool IsAdmin() bool
IsHost() bool IsHost() bool
Connected() bool IsConnected() bool
VerifySecret(secret string) bool VerifySecret(secret string) bool
SetWebSocketPeer(websocket_peer WebSocketPeer) SetWebSocketPeer(websocket_peer WebSocketPeer)
SetWebSocketConnected(connected bool) SetWebSocketConnected(connected bool)

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.Admin() { if !session.IsAdmin() {
h.logger.Debug().Msg("user not admin") h.logger.Debug().Msg("user not admin")
return nil return nil
} }

View File

@ -11,7 +11,7 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
return err return err
} }
if session.Admin() { if session.IsAdmin() {
if err := h.systemAdmin(session); err != nil { if err := h.systemAdmin(session); err != nil {
return err return err
} }
@ -23,7 +23,7 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
Event: event.MEMBER_CONNECTED, Event: event.MEMBER_CONNECTED,
ID: session.ID(), ID: session.ID(),
Name: session.Name(), Name: session.Name(),
IsAdmin: session.Admin(), IsAdmin: session.IsAdmin(),
}, nil) }, nil)
return nil return nil

View File

@ -28,7 +28,7 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
members[session.ID()] = message.MemberData{ members[session.ID()] = message.MemberData{
ID: session.ID(), ID: session.ID(),
Name: session.Name(), Name: session.Name(),
IsAdmin: session.Admin(), IsAdmin: session.IsAdmin(),
} }
} }

View File

@ -135,7 +135,7 @@ func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) e
return connection.Close() return connection.Close()
} }
if session.Connected() { if session.IsConnected() {
// TODO: Refactor // TODO: Refactor
if err = connection.WriteJSON( if err = connection.WriteJSON(
message.SystemDisconnect{ message.SystemDisconnect{