Cleaning up some old code from the permissions system change (#4090)
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
7fcc004beb
Коммит
667db6e10c
@@ -27,8 +27,8 @@ type WebConn struct {
|
|||||||
UserId string
|
UserId string
|
||||||
T goi18n.TranslateFunc
|
T goi18n.TranslateFunc
|
||||||
Locale string
|
Locale string
|
||||||
hasPermissionsToChannel map[string]bool
|
isMemberOfChannel map[string]bool
|
||||||
hasPermissionsToTeam map[string]bool
|
isMemberOfTeam map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebConn(c *Context, ws *websocket.Conn) *WebConn {
|
func NewWebConn(c *Context, ws *websocket.Conn) *WebConn {
|
||||||
@@ -41,8 +41,8 @@ func NewWebConn(c *Context, ws *websocket.Conn) *WebConn {
|
|||||||
SessionToken: c.Session.Token,
|
SessionToken: c.Session.Token,
|
||||||
T: c.T,
|
T: c.T,
|
||||||
Locale: c.Locale,
|
Locale: c.Locale,
|
||||||
hasPermissionsToChannel: make(map[string]bool),
|
isMemberOfChannel: make(map[string]bool),
|
||||||
hasPermissionsToTeam: make(map[string]bool),
|
isMemberOfTeam: make(map[string]bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,56 +101,49 @@ func (c *WebConn) writePump() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebConn) InvalidateCache() {
|
func (c *WebConn) InvalidateCache() {
|
||||||
c.hasPermissionsToChannel = make(map[string]bool)
|
c.isMemberOfTeam = make(map[string]bool)
|
||||||
c.hasPermissionsToTeam = make(map[string]bool)
|
c.isMemberOfChannel = make(map[string]bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebConn) InvalidateCacheForChannel(channelId string) {
|
func (c *WebConn) InvalidateCacheForChannel(channelId string) {
|
||||||
delete(c.hasPermissionsToChannel, channelId)
|
delete(c.isMemberOfChannel, channelId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebConn) HasPermissionsToTeam(teamId string) bool {
|
func (c *WebConn) IsMemberOfTeam(teamId string) bool {
|
||||||
perm, ok := c.hasPermissionsToTeam[teamId]
|
isMember, ok := c.isMemberOfTeam[teamId]
|
||||||
if !ok {
|
if !ok {
|
||||||
session := GetSession(c.SessionToken)
|
session := GetSession(c.SessionToken)
|
||||||
if session == nil {
|
if session == nil {
|
||||||
perm = false
|
isMember = false
|
||||||
c.hasPermissionsToTeam[teamId] = perm
|
c.isMemberOfTeam[teamId] = isMember
|
||||||
} else {
|
} else {
|
||||||
member := session.GetTeamByTeamId(teamId)
|
member := session.GetTeamByTeamId(teamId)
|
||||||
|
|
||||||
if member != nil {
|
if member != nil {
|
||||||
perm = true
|
isMember = true
|
||||||
c.hasPermissionsToTeam[teamId] = perm
|
c.isMemberOfTeam[teamId] = isMember
|
||||||
} else {
|
} else {
|
||||||
perm = true
|
isMember = true
|
||||||
c.hasPermissionsToTeam[teamId] = perm
|
c.isMemberOfTeam[teamId] = isMember
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return perm
|
return isMember
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *WebConn) HasPermissionsToChannel(channelId string) bool {
|
func (c *WebConn) IsMemberOfChannel(channelId string) bool {
|
||||||
perm, ok := c.hasPermissionsToChannel[channelId]
|
isMember, ok := c.isMemberOfChannel[channelId]
|
||||||
if !ok {
|
if !ok {
|
||||||
if cresult := <-Srv.Store.Channel().CheckPermissionsToNoTeam(channelId, c.UserId); cresult.Err != nil {
|
if cresult := <-Srv.Store.Channel().GetMember(channelId, c.UserId); cresult.Err != nil {
|
||||||
perm = false
|
isMember = false
|
||||||
c.hasPermissionsToChannel[channelId] = perm
|
c.isMemberOfChannel[channelId] = isMember
|
||||||
} else {
|
} else {
|
||||||
count := cresult.Data.(int64)
|
isMember = true
|
||||||
|
c.isMemberOfChannel[channelId] = isMember
|
||||||
if count == 1 {
|
|
||||||
perm = true
|
|
||||||
c.hasPermissionsToChannel[channelId] = perm
|
|
||||||
} else {
|
|
||||||
perm = false
|
|
||||||
c.hasPermissionsToChannel[channelId] = perm
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return perm
|
return isMember
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
|
||||||
"github.com/mattermost/platform/einterfaces"
|
"github.com/mattermost/platform/einterfaces"
|
||||||
@@ -154,7 +155,7 @@ func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool {
|
|||||||
// We have to make sure the user is in the channel. Otherwise system messages that
|
// We have to make sure the user is in the channel. Otherwise system messages that
|
||||||
// post about users in channels they are not in trigger warnings.
|
// post about users in channels they are not in trigger warnings.
|
||||||
if len(msg.ChannelId) > 0 {
|
if len(msg.ChannelId) > 0 {
|
||||||
allowed := webCon.HasPermissionsToChannel(msg.ChannelId)
|
allowed := webCon.IsMemberOfChannel(msg.ChannelId)
|
||||||
|
|
||||||
if !allowed {
|
if !allowed {
|
||||||
return false
|
return false
|
||||||
@@ -176,7 +177,7 @@ func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool {
|
|||||||
|
|
||||||
// Only report events to users who are in the team for the event
|
// Only report events to users who are in the team for the event
|
||||||
if len(msg.TeamId) > 0 {
|
if len(msg.TeamId) > 0 {
|
||||||
allowed := webCon.HasPermissionsToTeam(msg.TeamId)
|
allowed := webCon.IsMemberOfTeam(msg.TeamId)
|
||||||
|
|
||||||
if !allowed {
|
if !allowed {
|
||||||
return false
|
return false
|
||||||
@@ -185,7 +186,7 @@ func shouldSendEvent(webCon *WebConn, msg *model.WebSocketEvent) bool {
|
|||||||
|
|
||||||
// Only report events to users who are in the channel for the event execept deleted events
|
// Only report events to users who are in the channel for the event execept deleted events
|
||||||
if len(msg.ChannelId) > 0 && msg.Event != model.WEBSOCKET_EVENT_CHANNEL_DELETED {
|
if len(msg.ChannelId) > 0 && msg.Event != model.WEBSOCKET_EVENT_CHANNEL_DELETED {
|
||||||
allowed := webCon.HasPermissionsToChannel(msg.ChannelId)
|
allowed := webCon.IsMemberOfChannel(msg.ChannelId)
|
||||||
|
|
||||||
if !allowed {
|
if !allowed {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -55,23 +55,6 @@ func TeamMembersFromJson(data io.Reader) []*TeamMember {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsInTeamRole(teamRoles string, inRole string) bool {
|
|
||||||
roles := strings.Split(teamRoles, " ")
|
|
||||||
|
|
||||||
for _, r := range roles {
|
|
||||||
if r == inRole {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *TeamMember) IsTeamAdmin() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *TeamMember) IsValid() *AppError {
|
func (o *TeamMember) IsValid() *AppError {
|
||||||
|
|
||||||
if len(o.TeamId) != 26 {
|
if len(o.TeamId) != 26 {
|
||||||
@@ -82,12 +65,6 @@ func (o *TeamMember) IsValid() *AppError {
|
|||||||
return NewLocAppError("TeamMember.IsValid", "model.team_member.is_valid.user_id.app_error", nil, "")
|
return NewLocAppError("TeamMember.IsValid", "model.team_member.is_valid.user_id.app_error", nil, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
/*for _, role := range strings.Split(o.Roles, " ") {
|
|
||||||
if !(role == "" || role == ROLE_TEAM_ADMIN.Id) {
|
|
||||||
return NewLocAppError("TeamMember.IsValid", "model.team_member.is_valid.role.app_error", nil, "role="+role)
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -730,130 +730,6 @@ func (s SqlChannelStore) PermanentDeleteMembersByUser(userId string) StoreChanne
|
|||||||
return storeChannel
|
return storeChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) CheckPermissionsToNoTeam(channelId string, userId string) StoreChannel {
|
|
||||||
storeChannel := make(StoreChannel, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
result := StoreResult{}
|
|
||||||
|
|
||||||
count, err := s.GetReplica().SelectInt(
|
|
||||||
`SELECT
|
|
||||||
COUNT(0)
|
|
||||||
FROM
|
|
||||||
Channels,
|
|
||||||
ChannelMembers
|
|
||||||
WHERE
|
|
||||||
Channels.Id = ChannelMembers.ChannelId
|
|
||||||
AND Channels.DeleteAt = 0
|
|
||||||
AND ChannelMembers.ChannelId = :ChannelId
|
|
||||||
AND ChannelMembers.UserId = :UserId`,
|
|
||||||
map[string]interface{}{"ChannelId": channelId, "UserId": userId})
|
|
||||||
if err != nil {
|
|
||||||
result.Err = model.NewLocAppError("SqlChannelStore.CheckPermissionsTo", "store.sql_channel.check_permissions.app_error", nil, "channel_id="+channelId+", user_id="+userId+", "+err.Error())
|
|
||||||
} else {
|
|
||||||
result.Data = count
|
|
||||||
}
|
|
||||||
|
|
||||||
storeChannel <- result
|
|
||||||
close(storeChannel)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return storeChannel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SqlChannelStore) CheckPermissionsTo(teamId string, channelId string, userId string) StoreChannel {
|
|
||||||
storeChannel := make(StoreChannel, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
result := StoreResult{}
|
|
||||||
|
|
||||||
count, err := s.GetReplica().SelectInt(
|
|
||||||
`SELECT
|
|
||||||
COUNT(0)
|
|
||||||
FROM
|
|
||||||
Channels,
|
|
||||||
ChannelMembers
|
|
||||||
WHERE
|
|
||||||
Channels.Id = ChannelMembers.ChannelId
|
|
||||||
AND (Channels.TeamId = :TeamId OR Channels.TeamId = '')
|
|
||||||
AND Channels.DeleteAt = 0
|
|
||||||
AND ChannelMembers.ChannelId = :ChannelId
|
|
||||||
AND ChannelMembers.UserId = :UserId`,
|
|
||||||
map[string]interface{}{"TeamId": teamId, "ChannelId": channelId, "UserId": userId})
|
|
||||||
if err != nil {
|
|
||||||
result.Err = model.NewLocAppError("SqlChannelStore.CheckPermissionsTo", "store.sql_channel.check_permissions.app_error", nil, "channel_id="+channelId+", user_id="+userId+", "+err.Error())
|
|
||||||
} else {
|
|
||||||
result.Data = count
|
|
||||||
}
|
|
||||||
|
|
||||||
storeChannel <- result
|
|
||||||
close(storeChannel)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return storeChannel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SqlChannelStore) CheckPermissionsToByName(teamId string, channelName string, userId string) StoreChannel {
|
|
||||||
storeChannel := make(StoreChannel, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
result := StoreResult{}
|
|
||||||
|
|
||||||
channelId, err := s.GetReplica().SelectStr(
|
|
||||||
`SELECT
|
|
||||||
Channels.Id
|
|
||||||
FROM
|
|
||||||
Channels,
|
|
||||||
ChannelMembers
|
|
||||||
WHERE
|
|
||||||
Channels.Id = ChannelMembers.ChannelId
|
|
||||||
AND (Channels.TeamId = :TeamId OR Channels.TeamId = '')
|
|
||||||
AND Channels.Name = :Name
|
|
||||||
AND Channels.DeleteAt = 0
|
|
||||||
AND ChannelMembers.UserId = :UserId`,
|
|
||||||
map[string]interface{}{"TeamId": teamId, "Name": channelName, "UserId": userId})
|
|
||||||
if err != nil {
|
|
||||||
result.Err = model.NewLocAppError("SqlChannelStore.CheckPermissionsToByName", "store.sql_channel.check_permissions_by_name.app_error", nil, "channel_id="+channelName+", user_id="+userId+", "+err.Error())
|
|
||||||
} else {
|
|
||||||
result.Data = channelId
|
|
||||||
}
|
|
||||||
|
|
||||||
storeChannel <- result
|
|
||||||
close(storeChannel)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return storeChannel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SqlChannelStore) CheckOpenChannelPermissions(teamId string, channelId string) StoreChannel {
|
|
||||||
storeChannel := make(StoreChannel, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
result := StoreResult{}
|
|
||||||
|
|
||||||
count, err := s.GetReplica().SelectInt(
|
|
||||||
`SELECT
|
|
||||||
COUNT(0)
|
|
||||||
FROM
|
|
||||||
Channels
|
|
||||||
WHERE
|
|
||||||
Channels.Id = :ChannelId
|
|
||||||
AND Channels.TeamId = :TeamId
|
|
||||||
AND Channels.Type = :ChannelType`,
|
|
||||||
map[string]interface{}{"ChannelId": channelId, "TeamId": teamId, "ChannelType": model.CHANNEL_OPEN})
|
|
||||||
if err != nil {
|
|
||||||
result.Err = model.NewLocAppError("SqlChannelStore.CheckOpenChannelPermissions", "store.sql_channel.check_open_channel_permissions.app_error", nil, "channel_id="+channelId+", "+err.Error())
|
|
||||||
} else {
|
|
||||||
result.Data = count
|
|
||||||
}
|
|
||||||
|
|
||||||
storeChannel <- result
|
|
||||||
close(storeChannel)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return storeChannel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SqlChannelStore) SetLastViewedAt(channelId string, userId string, newLastViewedAt int64) StoreChannel {
|
func (s SqlChannelStore) SetLastViewedAt(channelId string, userId string, newLastViewedAt int64) StoreChannel {
|
||||||
storeChannel := make(StoreChannel, 1)
|
storeChannel := make(StoreChannel, 1)
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,10 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mattermost/platform/model"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestChannelStoreSave(t *testing.T) {
|
func TestChannelStoreSave(t *testing.T) {
|
||||||
@@ -442,94 +443,6 @@ func TestChannelDeleteMemberStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChannelStorePermissionsTo(t *testing.T) {
|
|
||||||
Setup()
|
|
||||||
|
|
||||||
o1 := model.Channel{}
|
|
||||||
o1.TeamId = model.NewId()
|
|
||||||
o1.DisplayName = "Channel1"
|
|
||||||
o1.Name = "a" + model.NewId() + "b"
|
|
||||||
o1.Type = model.CHANNEL_OPEN
|
|
||||||
Must(store.Channel().Save(&o1))
|
|
||||||
|
|
||||||
m1 := model.ChannelMember{}
|
|
||||||
m1.ChannelId = o1.Id
|
|
||||||
m1.UserId = model.NewId()
|
|
||||||
m1.NotifyProps = model.GetDefaultChannelNotifyProps()
|
|
||||||
Must(store.Channel().SaveMember(&m1))
|
|
||||||
|
|
||||||
count := (<-store.Channel().CheckPermissionsTo(o1.TeamId, o1.Id, m1.UserId)).Data.(int64)
|
|
||||||
if count != 1 {
|
|
||||||
t.Fatal("should have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckPermissionsToNoTeam(o1.Id, m1.UserId)).Data.(int64)
|
|
||||||
if count != 1 {
|
|
||||||
t.Fatal("should have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckPermissionsTo("junk", o1.Id, m1.UserId)).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckPermissionsTo(o1.TeamId, "junk", m1.UserId)).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckPermissionsToNoTeam("junk", m1.UserId)).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckPermissionsTo(o1.TeamId, o1.Id, "junk")).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckPermissionsToNoTeam(o1.Id, "junk")).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
channelId := (<-store.Channel().CheckPermissionsToByName(o1.TeamId, o1.Name, m1.UserId)).Data.(string)
|
|
||||||
if channelId != o1.Id {
|
|
||||||
t.Fatal("should have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
channelId = (<-store.Channel().CheckPermissionsToByName(o1.TeamId, "missing", m1.UserId)).Data.(string)
|
|
||||||
if channelId != "" {
|
|
||||||
t.Fatal("should not have permissions")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannelStoreOpenChannelPermissionsTo(t *testing.T) {
|
|
||||||
Setup()
|
|
||||||
|
|
||||||
o1 := model.Channel{}
|
|
||||||
o1.TeamId = model.NewId()
|
|
||||||
o1.DisplayName = "Channel1"
|
|
||||||
o1.Name = "a" + model.NewId() + "b"
|
|
||||||
o1.Type = model.CHANNEL_OPEN
|
|
||||||
Must(store.Channel().Save(&o1))
|
|
||||||
|
|
||||||
count := (<-store.Channel().CheckOpenChannelPermissions(o1.TeamId, o1.Id)).Data.(int64)
|
|
||||||
if count != 1 {
|
|
||||||
t.Fatal("should have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckOpenChannelPermissions("junk", o1.Id)).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
|
|
||||||
count = (<-store.Channel().CheckOpenChannelPermissions(o1.TeamId, "junk")).Data.(int64)
|
|
||||||
if count != 0 {
|
|
||||||
t.Fatal("shouldn't have permissions")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestChannelStoreGetChannels(t *testing.T) {
|
func TestChannelStoreGetChannels(t *testing.T) {
|
||||||
Setup()
|
Setup()
|
||||||
|
|
||||||
|
|||||||
@@ -95,10 +95,6 @@ type ChannelStore interface {
|
|||||||
RemoveMember(channelId string, userId string) StoreChannel
|
RemoveMember(channelId string, userId string) StoreChannel
|
||||||
PermanentDeleteMembersByUser(userId string) StoreChannel
|
PermanentDeleteMembersByUser(userId string) StoreChannel
|
||||||
GetExtraMembers(channelId string, limit int) StoreChannel
|
GetExtraMembers(channelId string, limit int) StoreChannel
|
||||||
CheckPermissionsTo(teamId string, channelId string, userId string) StoreChannel
|
|
||||||
CheckPermissionsToNoTeam(channelId string, userId string) StoreChannel
|
|
||||||
CheckOpenChannelPermissions(teamId string, channelId string) StoreChannel
|
|
||||||
CheckPermissionsToByName(teamId string, channelName string, userId string) StoreChannel
|
|
||||||
UpdateLastViewedAt(channelId string, userId string) StoreChannel
|
UpdateLastViewedAt(channelId string, userId string) StoreChannel
|
||||||
SetLastViewedAt(channelId string, userId string, newLastViewedAt int64) StoreChannel
|
SetLastViewedAt(channelId string, userId string, newLastViewedAt int64) StoreChannel
|
||||||
IncrementMentionCount(channelId string, userId string) StoreChannel
|
IncrementMentionCount(channelId string, userId string) StoreChannel
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user