Restrict the creation of dms/gms to user that you can see (#11695)
* Restrict the creation of dms/gms to user that you can see * Adding tests * Adding tests for CreateGroupChannel api endpoint
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
0ec0616d89
Коммит
ee007962f4
@@ -347,6 +347,22 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
otherUserId := userIds[0]
|
||||||
|
if c.App.Session.UserId == otherUserId {
|
||||||
|
otherUserId = userIds[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
canSee, err := c.App.UserCanSeeOtherUser(c.App.Session.UserId, otherUserId)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !canSee {
|
||||||
|
c.SetPermissionError(model.PERMISSION_VIEW_MEMBERS)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
sc, err := c.App.GetOrCreateDirectChannel(userIds[0], userIds[1])
|
sc, err := c.App.GetOrCreateDirectChannel(userIds[0], userIds[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
@@ -401,6 +417,25 @@ func createGroupChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canSeeAll := true
|
||||||
|
for _, id := range userIds {
|
||||||
|
if c.App.Session.UserId != id {
|
||||||
|
canSee, err := c.App.UserCanSeeOtherUser(c.App.Session.UserId, id)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !canSee {
|
||||||
|
canSeeAll = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !canSeeAll {
|
||||||
|
c.SetPermissionError(model.PERMISSION_VIEW_MEMBERS)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
groupChannel, err := c.App.CreateGroupChannel(userIds, c.App.Session.UserId)
|
groupChannel, err := c.App.CreateGroupChannel(userIds, c.App.Session.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Err = err
|
c.Err = err
|
||||||
|
|||||||
@@ -407,6 +407,51 @@ func TestCreateDirectChannel(t *testing.T) {
|
|||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateDirectChannelAsGuest(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer th.TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
user1 := th.BasicUser
|
||||||
|
|
||||||
|
enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable
|
||||||
|
defer func() {
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts })
|
||||||
|
th.App.RemoveLicense()
|
||||||
|
}()
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
|
||||||
|
th.App.SetLicense(model.NewTestLicense())
|
||||||
|
|
||||||
|
id := model.NewId()
|
||||||
|
guest := &model.User{
|
||||||
|
Email: "success+" + id + "@simulator.amazonses.com",
|
||||||
|
Username: "un_" + id,
|
||||||
|
Nickname: "nn_" + id,
|
||||||
|
Password: "Password1",
|
||||||
|
EmailVerified: true,
|
||||||
|
}
|
||||||
|
guest, err := th.App.CreateGuest(guest)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
_, resp := Client.Login(guest.Username, "Password1")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
|
t.Run("Try to created DM with not visible user", func(t *testing.T) {
|
||||||
|
_, resp := Client.CreateDirectChannel(guest.Id, user1.Id)
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = Client.CreateDirectChannel(user1.Id, guest.Id)
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Creating DM with visible user", func(t *testing.T) {
|
||||||
|
th.LinkUserToTeam(guest, th.BasicTeam)
|
||||||
|
th.AddUserToChannel(guest, th.BasicChannel)
|
||||||
|
|
||||||
|
_, resp := Client.CreateDirectChannel(guest.Id, user1.Id)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestDeleteDirectChannel(t *testing.T) {
|
func TestDeleteDirectChannel(t *testing.T) {
|
||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
@@ -496,6 +541,67 @@ func TestCreateGroupChannel(t *testing.T) {
|
|||||||
CheckNoError(t, resp)
|
CheckNoError(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateGroupChannelAsGuest(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer th.TearDown()
|
||||||
|
Client := th.Client
|
||||||
|
user1 := th.BasicUser
|
||||||
|
user2 := th.BasicUser2
|
||||||
|
user3 := th.CreateUser()
|
||||||
|
user4 := th.CreateUser()
|
||||||
|
user5 := th.CreateUser()
|
||||||
|
th.LinkUserToTeam(user2, th.BasicTeam)
|
||||||
|
th.AddUserToChannel(user2, th.BasicChannel)
|
||||||
|
th.LinkUserToTeam(user3, th.BasicTeam)
|
||||||
|
th.AddUserToChannel(user3, th.BasicChannel)
|
||||||
|
|
||||||
|
enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable
|
||||||
|
defer func() {
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts })
|
||||||
|
th.App.RemoveLicense()
|
||||||
|
}()
|
||||||
|
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
|
||||||
|
th.App.SetLicense(model.NewTestLicense())
|
||||||
|
|
||||||
|
id := model.NewId()
|
||||||
|
guest := &model.User{
|
||||||
|
Email: "success+" + id + "@simulator.amazonses.com",
|
||||||
|
Username: "un_" + id,
|
||||||
|
Nickname: "nn_" + id,
|
||||||
|
Password: "Password1",
|
||||||
|
EmailVerified: true,
|
||||||
|
}
|
||||||
|
guest, err := th.App.CreateGuest(guest)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
_, resp := Client.Login(guest.Username, "Password1")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
|
t.Run("Try to created GM with not visible users", func(t *testing.T) {
|
||||||
|
_, resp := Client.CreateGroupChannel([]string{guest.Id, user1.Id, user2.Id, user3.Id})
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = Client.CreateGroupChannel([]string{user1.Id, user2.Id, guest.Id, user3.Id})
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Try to created GM with visible and not visible users", func(t *testing.T) {
|
||||||
|
th.LinkUserToTeam(guest, th.BasicTeam)
|
||||||
|
th.AddUserToChannel(guest, th.BasicChannel)
|
||||||
|
|
||||||
|
_, resp := Client.CreateGroupChannel([]string{guest.Id, user1.Id, user3.Id, user4.Id, user5.Id})
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
|
||||||
|
_, resp = Client.CreateGroupChannel([]string{user1.Id, user2.Id, guest.Id, user4.Id, user5.Id})
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Creating GM with visible users", func(t *testing.T) {
|
||||||
|
_, resp := Client.CreateGroupChannel([]string{guest.Id, user1.Id, user2.Id, user3.Id})
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestDeleteGroupChannel(t *testing.T) {
|
func TestDeleteGroupChannel(t *testing.T) {
|
||||||
th := Setup().InitBasic()
|
th := Setup().InitBasic()
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user