MM-10007 Send an admin and regular WS events when a user is updated (#8588)

* Add user.DeepCopy() function

* Add omit admins/non-admins to WS broadcast and use for updating users

* Updates per feedback and adding unit test for ShouldSendEvent
Этот коммит содержится в:
Joram Wilander
2018-04-20 08:44:18 -04:00
коммит произвёл GitHub
родитель 7987c95fcd
Коммит 283f34b9c6
9 изменённых файлов: 197 добавлений и 5 удалений

Просмотреть файл

@@ -96,6 +96,23 @@ type UserAuth struct {
AuthService string `json:"auth_service,omitempty"`
}
func (u *User) DeepCopy() *User {
copyUser := *u
if u.AuthData != nil {
copyUser.AuthData = NewString(*u.AuthData)
}
if u.Props != nil {
copyUser.Props = CopyStringMap(u.Props)
}
if u.NotifyProps != nil {
copyUser.NotifyProps = CopyStringMap(u.NotifyProps)
}
if u.Timezone != nil {
copyUser.Timezone = CopyStringMap(u.Timezone)
}
return &copyUser
}
// IsValid validates the user and returns an error if it isn't configured
// correctly.
func (u *User) IsValid() *AppError {

Просмотреть файл

@@ -8,6 +8,8 @@ import (
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPasswordHash(t *testing.T) {
@@ -22,6 +24,36 @@ func TestPasswordHash(t *testing.T) {
}
}
func TestUserDeepCopy(t *testing.T) {
id := NewId()
authData := "authdata"
mapKey := "key"
mapValue := "key"
user := &User{Id: id, AuthData: NewString(authData), Props: map[string]string{}, NotifyProps: map[string]string{}, Timezone: map[string]string{}}
user.Props[mapKey] = mapValue
user.NotifyProps[mapKey] = mapValue
user.Timezone[mapKey] = mapValue
copyUser := user.DeepCopy()
copyUser.Id = "someid"
*copyUser.AuthData = "changed"
copyUser.Props[mapKey] = "changed"
copyUser.NotifyProps[mapKey] = "changed"
copyUser.Timezone[mapKey] = "changed"
assert.Equal(t, id, user.Id)
assert.Equal(t, authData, *user.AuthData)
assert.Equal(t, mapValue, user.Props[mapKey])
assert.Equal(t, mapValue, user.NotifyProps[mapKey])
assert.Equal(t, mapValue, user.Timezone[mapKey])
user = &User{Id: id}
copyUser = user.DeepCopy()
assert.Equal(t, id, user.Id)
}
func TestUserJson(t *testing.T) {
user := User{Id: NewId(), Username: NewId()}
json := user.ToJson()

Просмотреть файл

@@ -148,6 +148,14 @@ func GetMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func CopyStringMap(originalMap map[string]string) map[string]string {
copyMap := make(map[string]string)
for k, v := range originalMap {
copyMap[k] = v
}
return copyMap
}
// MapToJson converts a map to a json string
func MapToJson(objmap map[string]string) string {
b, _ := json.Marshal(objmap)

Просмотреть файл

@@ -8,6 +8,7 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -47,6 +48,17 @@ func TestAppErrorJunk(t *testing.T) {
}
}
func TestCopyStringMap(t *testing.T) {
itemKey := "item1"
originalMap := make(map[string]string)
originalMap[itemKey] = "val1"
copyMap := CopyStringMap(originalMap)
copyMap[itemKey] = "changed"
assert.Equal(t, "val1", originalMap[itemKey])
}
func TestMapJson(t *testing.T) {
m := make(map[string]string)

Просмотреть файл

@@ -57,10 +57,12 @@ type WebSocketMessage interface {
}
type WebsocketBroadcast struct {
OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
ContainsSanitizedData bool `json:"-"`
ContainsSensitiveData bool `json:"-"`
}
type precomputedWebSocketEventJSON struct {