MM-14412: Enables force removals of team/channel members base on group constraints. (#10490)

* MM-14412: Enables force removals of team/channel members base on group constraints.

* Renames some existing methods and variables.

* Change return types to ChannelMembers and TeamMembers for some existing methods.

* Adds option to change LDAP_DATA to either 'qa' or 'test' with env variable.

* Adds methods to retrieve ChannelMembers and TeamMembers that, based on group constraints, should be deleted.

* Adds helper functions to create GroupTeams and GroupChannels.

* MM-14412: Switches to helper methods for GroupSyncable creation in test files.

* MM-14412: Style fix.

* MM-14412: Switches remaining GroupSyncable instances to be created with helper functions.

* MM-14412: Typo fix.

* MM-11412: Build fixes.

* MM-14412: Checks if user is team member before re-adding.

* MM-14412: Update for change of GroupConstrained fields type.
Этот коммит содержится в:
Martin Kraft
2019-04-02 09:22:50 -04:00
коммит произвёл GitHub
родитель cee65379c7
Коммит 25fd962016
23 изменённых файлов: 1212 добавлений и 607 удалений

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

@@ -668,11 +668,11 @@ func (s *SqlSupplier) GroupDeleteGroupSyncable(ctx context.Context, groupID stri
return result
}
// PendingAutoAddTeamMembers returns a slice of UserTeamIDPair that need newly created memberships
// TeamMembersToAdd returns a slice of UserTeamIDPair that need newly created memberships
// based on the groups configurations.
//
// Typically since will be the last successful group sync time.
func (s *SqlSupplier) PendingAutoAddTeamMembers(ctx context.Context, since int64, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
func (s *SqlSupplier) TeamMembersToAdd(ctx context.Context, since int64, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
result := store.NewSupplierResult()
sql := `
@@ -698,23 +698,23 @@ func (s *SqlSupplier) PendingAutoAddTeamMembers(ctx context.Context, since int64
AND (GroupMembers.CreateAt >= :Since
OR GroupTeams.UpdateAt >= :Since)`
var userTeamIDs []*model.UserTeamIDPair
var teamMembers []*model.UserTeamIDPair
_, err := s.GetMaster().Select(&userTeamIDs, sql, map[string]interface{}{"Since": since})
_, err := s.GetReplica().Select(&teamMembers, sql, map[string]interface{}{"Since": since})
if err != nil {
result.Err = model.NewAppError("SqlGroupStore.PendingAutoAddTeamMembers", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
result.Err = model.NewAppError("SqlGroupStore.TeamMembersToAdd", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
}
result.Data = userTeamIDs
result.Data = teamMembers
return result
}
// PendingAutoAddChannelMembers returns a slice of UserChannelIDPair that need newly created memberships
// ChannelMembersToAdd returns a slice of UserChannelIDPair that need newly created memberships
// based on the groups configurations.
//
// Typically since will be the last successful group sync time.
func (s *SqlSupplier) PendingAutoAddChannelMembers(ctx context.Context, since int64, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
func (s *SqlSupplier) ChannelMembersToAdd(ctx context.Context, since int64, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
result := store.NewSupplierResult()
sql := `
@@ -740,14 +740,14 @@ func (s *SqlSupplier) PendingAutoAddChannelMembers(ctx context.Context, since in
AND (GroupMembers.CreateAt >= :Since
OR GroupChannels.UpdateAt >= :Since)`
var userChannelIDs []*model.UserChannelIDPair
var channelMembers []*model.UserChannelIDPair
_, err := s.GetMaster().Select(&userChannelIDs, sql, map[string]interface{}{"Since": since})
_, err := s.GetReplica().Select(&channelMembers, sql, map[string]interface{}{"Since": since})
if err != nil {
result.Err = model.NewAppError("SqlGroupStore.PendingAutoAddChannelMembers", "store.select_error", nil, "", http.StatusInternalServerError)
result.Err = model.NewAppError("SqlGroupStore.ChannelMembersToAdd", "store.select_error", nil, "", http.StatusInternalServerError)
}
result.Data = userChannelIDs
result.Data = channelMembers
return result
}
@@ -765,3 +765,92 @@ func groupSyncableToGroupChannel(groupSyncable *model.GroupSyncable) *groupChann
ChannelId: groupSyncable.SyncableId,
}
}
// TeamMembersToRemove returns all team members that should be removed based on group constraints.
func (s *SqlSupplier) TeamMembersToRemove(ctx context.Context, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
result := store.NewSupplierResult()
sql := `
SELECT
TeamMembers.*
FROM
TeamMembers
JOIN Teams ON Teams.Id = TeamMembers.TeamId
WHERE
TeamMembers.DeleteAt = 0
AND Teams.DeleteAt = 0
AND Teams.GroupConstrained = TRUE
AND (TeamMembers.TeamId, TeamMembers.UserId)
NOT IN (
SELECT
Teams.Id AS TeamId, GroupMembers.UserId
FROM
Teams
JOIN GroupTeams ON GroupTeams.TeamId = Teams.Id
JOIN UserGroups ON UserGroups.Id = GroupTeams.GroupId
JOIN GroupMembers ON GroupMembers.GroupId = UserGroups.Id
WHERE
Teams.GroupConstrained = TRUE
AND GroupTeams.DeleteAt = 0
AND UserGroups.DeleteAt = 0
AND Teams.DeleteAt = 0
AND GroupMembers.DeleteAt = 0
GROUP BY
Teams.Id,
GroupMembers.UserId)`
var teamMembers []*model.TeamMember
_, err := s.GetReplica().Select(&teamMembers, sql)
if err != nil {
result.Err = model.NewAppError("SqlGroupStore.TeamMembersToRemove", "store.select_error", nil, "", http.StatusInternalServerError)
}
result.Data = teamMembers
return result
}
// ChannelMembersToRemove returns all channel members that should be removed based on group constraints.
func (s *SqlSupplier) ChannelMembersToRemove(ctx context.Context, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
result := store.NewSupplierResult()
sql := `
SELECT
ChannelMembers.*
FROM
ChannelMembers
JOIN Channels ON Channels.Id = ChannelMembers.ChannelId
WHERE
Channels.DeleteAt = 0
AND Channels.GroupConstrained = TRUE
AND (ChannelMembers.ChannelId, ChannelMembers.UserId)
NOT IN (
SELECT
Channels.Id AS ChannelId, GroupMembers.UserId
FROM
Channels
JOIN GroupChannels ON GroupChannels.ChannelId = Channels.Id
JOIN UserGroups ON UserGroups.Id = GroupChannels.GroupId
JOIN GroupMembers ON GroupMembers.GroupId = UserGroups.Id
WHERE
Channels.GroupConstrained = TRUE
AND GroupChannels.DeleteAt = 0
AND UserGroups.DeleteAt = 0
AND Channels.DeleteAt = 0
AND GroupMembers.DeleteAt = 0
GROUP BY
Channels.Id,
GroupMembers.UserId)`
var channelMembers []*model.ChannelMember
_, err := s.GetReplica().Select(&channelMembers, sql)
if err != nil {
result.Err = model.NewAppError("SqlGroupStore.ChannelMembersToRemove", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
}
result.Data = channelMembers
return result
}

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

@@ -640,6 +640,9 @@ func UpgradeDatabaseToVersion510(sqlStore SqlStore) {
sqlStore.CreateColumnIfNotExistsNoDefault("Channels", "GroupConstrained", "tinyint(4)", "boolean")
sqlStore.CreateColumnIfNotExistsNoDefault("Teams", "GroupConstrained", "tinyint(4)", "boolean")
sqlStore.CreateIndexIfNotExists("idx_groupteams_teamid", "GroupTeams", "TeamId")
sqlStore.CreateIndexIfNotExists("idx_groupchannels_channelid", "GroupChannels", "ChannelId")
saveSchemaVersion(sqlStore, VERSION_5_10_0)
}
}