MM 56929 Dont allow guests to be set via channel API (#26385)

* don't allow guest to be set only on channels.

* fix bad merge, add jira ticket link

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Scott Bishel
2024-03-07 08:44:43 -07:00
коммит произвёл GitHub
родитель 8e99bca82b
Коммит ab029105fd
4 изменённых файлов: 51 добавлений и 15 удалений

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

@@ -2894,6 +2894,31 @@ func TestUpdateChannelRoles(t *testing.T) {
func TestUpdateChannelMemberSchemeRoles(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
enableGuestAccounts := *th.App.Config().GuestAccountsSettings.Enable
defer func() {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = enableGuestAccounts })
th.App.Srv().RemoveLicense()
}()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GuestAccountsSettings.Enable = true })
th.App.Srv().SetLicense(model.NewTestLicense())
id := model.NewId()
guest := &model.User{
Email: th.GenerateTestEmail(),
Nickname: "nn_" + id,
FirstName: "f_" + id,
LastName: "l_" + id,
Password: "Pa$$word11",
EmailVerified: true,
}
guest, appError := th.App.CreateGuest(th.Context, guest)
require.Nil(t, appError)
_, _, appError = th.App.AddUserToTeam(th.Context, th.BasicTeam.Id, guest.Id, "")
th.AddUserToChannel(guest, th.BasicChannel)
require.Nil(t, appError)
SystemAdminClient := th.SystemAdminClient
WebSocketClient, err := th.CreateWebSocketClient()
WebSocketClient.Listen()
@@ -2944,6 +2969,11 @@ func TestUpdateChannelMemberSchemeRoles(t *testing.T) {
assert.Equal(t, true, tm2.SchemeUser)
assert.Equal(t, false, tm2.SchemeAdmin)
//cannot set Guest to User for single channel
resp, err := SystemAdminClient.UpdateChannelMemberSchemeRoles(context.Background(), th.BasicChannel.Id, guest.Id, s2)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
s3 := &model.SchemeRoles{
SchemeAdmin: true,
SchemeUser: false,
@@ -2977,21 +3007,17 @@ func TestUpdateChannelMemberSchemeRoles(t *testing.T) {
SchemeUser: false,
SchemeGuest: true,
}
_, err = SystemAdminClient.UpdateChannelMemberSchemeRoles(context.Background(), th.BasicChannel.Id, th.BasicUser.Id, s5)
require.NoError(t, err)
tm5, _, err := SystemAdminClient.GetChannelMember(context.Background(), th.BasicChannel.Id, th.BasicUser.Id, "")
require.NoError(t, err)
assert.Equal(t, true, tm5.SchemeGuest)
assert.Equal(t, false, tm5.SchemeUser)
assert.Equal(t, false, tm5.SchemeAdmin)
// cannot set user to guest for a single channel
resp, err = SystemAdminClient.UpdateChannelMemberSchemeRoles(context.Background(), th.BasicChannel.Id, th.BasicUser.Id, s5)
require.Error(t, err)
CheckBadRequestStatus(t, resp)
s6 := &model.SchemeRoles{
SchemeAdmin: false,
SchemeUser: true,
SchemeGuest: true,
}
resp, err := SystemAdminClient.UpdateChannelMemberSchemeRoles(context.Background(), th.BasicChannel.Id, th.BasicUser.Id, s6)
resp, err = SystemAdminClient.UpdateChannelMemberSchemeRoles(context.Background(), th.BasicChannel.Id, th.BasicUser.Id, s6)
require.Error(t, err)
CheckBadRequestStatus(t, resp)

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

@@ -1207,14 +1207,18 @@ func (a *App) UpdateChannelMemberSchemeRoles(c request.CTX, channelID string, us
return nil, err
}
if member.SchemeGuest {
return nil, model.NewAppError("UpdateChannelMemberSchemeRoles", "api.channel.update_channel_member_roles.guest.app_error", nil, "", http.StatusBadRequest)
}
if isSchemeGuest {
return nil, model.NewAppError("UpdateChannelMemberSchemeRoles", "api.channel.update_channel_member_roles.user_and_guest.app_error", nil, "", http.StatusBadRequest)
}
member.SchemeAdmin = isSchemeAdmin
member.SchemeUser = isSchemeUser
member.SchemeGuest = isSchemeGuest
if member.SchemeUser && member.SchemeGuest {
return nil, model.NewAppError("UpdateChannelMemberSchemeRoles", "api.channel.update_channel_member_roles.guest_and_user.app_error", nil, "", http.StatusBadRequest)
}
// If the migration is not completed, we also need to check the default channel_admin/channel_user roles are not present in the roles field.
if err = a.IsPhase2MigrationCompleted(); err != nil {
member.ExplicitRoles = RemoveRoles([]string{model.ChannelGuestRoleId, model.ChannelUserRoleId, model.ChannelAdminRoleId}, member.ExplicitRoles)

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

@@ -1512,7 +1512,6 @@ func TestImportImportUser(t *testing.T) {
// see https://mattermost.atlassian.net/browse/MM-56986
// Test importing deleted guest with a valid team & valid channel name in apply mode.
// mlog.Debug("TESTING GUEST")
// username = model.NewId()
// deleteAt = model.GetMillis()
// deletedGuestData := &imports.UserImportData{
@@ -1537,7 +1536,6 @@ func TestImportImportUser(t *testing.T) {
// user, appErr = th.App.GetUserByUsername(*deletedGuestData.Username)
// require.Nil(t, appErr, "Failed to get user from database.")
// mlog.Debug(user.Roles)
// teamMember, appErr = th.App.GetTeamMember(th.Context, team.Id, user.Id)
// require.Nil(t, appErr, "Failed to get the team member")

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

@@ -451,6 +451,10 @@
"id": "api.channel.update_channel_member_roles.changing_guest_role.app_error",
"translation": "Invalid channel member update: You can't add or remove the guest role manually."
},
{
"id": "api.channel.update_channel_member_roles.guest.app_error",
"translation": "Invalid channel member update: A guest cannot be made team member or team admin, please promote as a user first."
},
{
"id": "api.channel.update_channel_member_roles.guest_and_user.app_error",
"translation": "Invalid channel member update: A user must be a guest or a user but not both."
@@ -459,6 +463,10 @@
"id": "api.channel.update_channel_member_roles.scheme_role.app_error",
"translation": "The provided role is managed by a Scheme and therefore cannot be applied directly to a Channel Member."
},
{
"id": "api.channel.update_channel_member_roles.user_and_guest.app_error",
"translation": "Invalid channel member update: A guest cannot be set for a single channel, a System Admin must promote or demote users to/from guests."
},
{
"id": "api.channel.update_channel_privacy.default_channel_error",
"translation": "The default channel cannot be made private."