summaryrefslogtreecommitdiffstats
path: root/api/web_conn.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/web_conn.go')
-rw-r--r--api/web_conn.go78
1 files changed, 59 insertions, 19 deletions
diff --git a/api/web_conn.go b/api/web_conn.go
index 515a8ab31..397af0696 100644
--- a/api/web_conn.go
+++ b/api/web_conn.go
@@ -7,7 +7,6 @@ import (
l4g "github.com/alecthomas/log4go"
"github.com/gorilla/websocket"
"github.com/mattermost/platform/model"
- "github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"time"
)
@@ -21,14 +20,15 @@ const (
)
type WebConn struct {
- WebSocket *websocket.Conn
- Send chan *model.Message
- TeamId string
- UserId string
- ChannelAccessCache map[string]bool
+ WebSocket *websocket.Conn
+ Send chan *model.Message
+ SessionId string
+ UserId string
+ hasPermissionsToChannel map[string]bool
+ hasPermissionsToTeam map[string]bool
}
-func NewWebConn(ws *websocket.Conn, teamId string, userId string, sessionId string) *WebConn {
+func NewWebConn(ws *websocket.Conn, userId string, sessionId string) *WebConn {
go func() {
achan := Srv.Store.User().UpdateUserAndSessionActivity(userId, sessionId, model.GetMillis())
pchan := Srv.Store.User().UpdateLastPingAt(userId, model.GetMillis())
@@ -42,7 +42,14 @@ func NewWebConn(ws *websocket.Conn, teamId string, userId string, sessionId stri
}
}()
- return &WebConn{Send: make(chan *model.Message, 64), WebSocket: ws, UserId: userId, TeamId: teamId, ChannelAccessCache: make(map[string]bool)}
+ return &WebConn{
+ Send: make(chan *model.Message, 64),
+ WebSocket: ws,
+ UserId: userId,
+ SessionId: sessionId,
+ hasPermissionsToChannel: make(map[string]bool),
+ hasPermissionsToTeam: make(map[string]bool),
+ }
}
func (c *WebConn) readPump() {
@@ -69,7 +76,6 @@ func (c *WebConn) readPump() {
if err := c.WebSocket.ReadJSON(&msg); err != nil {
return
} else {
- msg.TeamId = c.TeamId
msg.UserId = c.UserId
PublishAndForget(&msg)
}
@@ -107,19 +113,53 @@ func (c *WebConn) writePump() {
}
}
-func (c *WebConn) updateChannelAccessCache(channelId string) bool {
- allowed := hasPermissionsToChannel(Srv.Store.Channel().CheckPermissionsTo(c.TeamId, channelId, c.UserId))
- c.ChannelAccessCache[channelId] = allowed
+func (c *WebConn) InvalidateCache() {
+ c.hasPermissionsToChannel = make(map[string]bool)
+ c.hasPermissionsToTeam = make(map[string]bool)
+}
+
+func (c *WebConn) HasPermissionsToTeam(teamId string) bool {
+ perm, ok := c.hasPermissionsToTeam[teamId]
+ if !ok {
+ session := GetSession(c.SessionId)
+ if session == nil {
+ perm = false
+ c.hasPermissionsToTeam[teamId] = perm
+ } else {
+ member := session.GetTeamByTeamId(teamId)
+
+ if member != nil {
+ perm = true
+ c.hasPermissionsToTeam[teamId] = perm
+ } else {
+ perm = true
+ c.hasPermissionsToTeam[teamId] = perm
+ }
- return allowed
+ }
+ }
+
+ return perm
}
-func hasPermissionsToChannel(sc store.StoreChannel) bool {
- if cresult := <-sc; cresult.Err != nil {
- return false
- } else if cresult.Data.(int64) != 1 {
- return false
+func (c *WebConn) HasPermissionsToChannel(channelId string) bool {
+ perm, ok := c.hasPermissionsToChannel[channelId]
+ if !ok {
+ if cresult := <-Srv.Store.Channel().CheckPermissionsToNoTeam(channelId, c.UserId); cresult.Err != nil {
+ perm = false
+ c.hasPermissionsToChannel[channelId] = perm
+ } else {
+ count := cresult.Data.(int64)
+
+ if count == 1 {
+ perm = true
+ c.hasPermissionsToChannel[channelId] = perm
+ } else {
+ perm = false
+ c.hasPermissionsToChannel[channelId] = perm
+ }
+ }
}
- return true
+ return perm
}