[MM-7968] restrict creation of direct channels to team members (#17222)

* restrict creation of direct channels to team members

* run make i18n-extract

* add suggestions from hahmadia

* place common-team-check logic in app layer

* use flat SQL query

* show more specific error message to user

* MM-7968: Fmt file.

* MM-7968: Fix for moved session field.

Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Martin Kraft <martinkraft@gmail.com>
Co-authored-by: Martin Kraft <martin@upspin.org>
Этот коммит содержится в:
Max Erenberg
2021-05-19 08:45:03 -04:00
коммит произвёл GitHub
родитель 3681cd3688
Коммит 9ef41a55e2
13 изменённых файлов: 175 добавлений и 4 удалений

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

@@ -601,6 +601,7 @@ type AppIface interface {
GetClusterId() string
GetClusterStatus() []*model.ClusterInfo
GetCommand(commandID string) (*model.Command, *model.AppError)
GetCommonTeamIDsForTwoUsers(userID, otherUserID string) ([]string, *model.AppError)
GetComplianceFile(job *model.Compliance) ([]byte, *model.AppError)
GetComplianceReport(reportId string) (*model.Compliance, *model.AppError)
GetComplianceReports(page, perPage int) (model.Compliances, *model.AppError)

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

@@ -332,6 +332,17 @@ func (a *App) GetOrCreateDirectChannel(c *request.Context, userID, otherUserID s
return channel, nil
}
if *a.Config().TeamSettings.RestrictDirectMessage == model.DIRECT_MESSAGE_TEAM &&
!a.SessionHasPermissionTo(*c.Session(), model.PERMISSION_MANAGE_SYSTEM) {
commonTeamIDs, err := a.GetCommonTeamIDsForTwoUsers(userID, otherUserID)
if err != nil {
return nil, err
}
if len(commonTeamIDs) == 0 {
return nil, model.NewAppError("createDirectChannel", "api.channel.create_channel.direct_channel.team_restricted_error", nil, "", http.StatusForbidden)
}
}
channel, err := a.createDirectChannel(userID, otherUserID, channelOptions...)
if err != nil {
if err.Id == store.ChannelExistsError {

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

@@ -5313,6 +5313,28 @@ func (a *OpenTracingAppLayer) GetCommand(commandID string) (*model.Command, *mod
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetCommonTeamIDsForTwoUsers(userID string, otherUserID string) ([]string, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetCommonTeamIDsForTwoUsers")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetCommonTeamIDsForTwoUsers(userID, otherUserID)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetComplianceFile(job *model.Compliance) ([]byte, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetComplianceFile")

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

@@ -85,7 +85,7 @@ func (*msgProvider) DoCommand(a *app.App, c *request.Context, args *model.Comman
var directChannel *model.Channel
if directChannel, err = a.GetOrCreateDirectChannel(c, args.UserId, userProfile.Id); err != nil {
mlog.Error(err.Error())
return &model.CommandResponse{Text: args.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
return &model.CommandResponse{Text: args.T(err.Id), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
targetChannelId = directChannel.Id
} else {

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

@@ -1016,6 +1016,14 @@ func (a *App) GetTeamMembersByIds(teamID string, userIDs []string, restrictions
return teamMembers, nil
}
func (a *App) GetCommonTeamIDsForTwoUsers(userID, otherUserID string) ([]string, *model.AppError) {
teamIDs, err := a.Srv().Store.Team().GetCommonTeamIDsForTwoUsers(userID, otherUserID)
if err != nil {
return nil, model.NewAppError("GetCommonTeamIDsForUsers", "app.team.get_common_team_ids_for_users.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return teamIDs, nil
}
func (a *App) AddTeamMember(c *request.Context, teamID, userID string) (*model.TeamMember, *model.AppError) {
_, teamMember, err := a.AddUserToTeam(c, teamID, userID, "")
if err != nil {