Files
mattermost/store/layered_store.go
Christopher Speller 06b579d18a MM-12393 Server side of bot accounts. (#10378)
* bots model, store and api (#9903)

* bots model, store and api

Fixes: MM-13100, MM-13101, MM-13103, MM-13105, MMM-13119

* uncomment tests incorrectly commented, and fix merge issues

* add etags support

* add missing licenses

* remove unused sqlbuilder.go (for now...)

* rejig permissions

* split out READ_BOTS into READ_BOTS and READ_OTHERS_BOTS, the latter
implicitly allowing the former
* make MANAGE_OTHERS_BOTS imply MANAGE_BOTS

* conform to general rest api pattern

* eliminate redundant http.StatusOK

* Update api4/bot.go

Co-Authored-By: lieut-data <jesse.hallam@gmail.com>

* s/model.UserFromBotModel/model.UserFromBot/g

* Update model/bot.go

Co-Authored-By: lieut-data <jesse.hallam@gmail.com>

* Update model/client4.go

Co-Authored-By: lieut-data <jesse.hallam@gmail.com>

* move sessionHasPermissionToManageBot to app/authorization.go

* use api.ApiSessionRequired for createBot

* introduce BOT_DESCRIPTION_MAX_RUNES constant

* MM-13512 Prevent getting a user by email based on privacy settings (#10021)

* MM-13512 Prevent getting a user by email based on privacy settings

* Add additional config settings to tests

* upgrade db to 5.7 (#10019)

* MM-13526 Add validation when setting a user's Locale field (#10022)

* Fix typos (#10024)

* Fixing first user being created with system admin privilages without being explicity specified. (#10014)

* Revert "Support for Embeded chat (#9129)" (#10017)

This reverts commit 3fcecd521a.

* s/DisableBot/UpdateBotActive

* add permissions on upgrade

* Update NOTICE.txt (#10054)

- add new dependency (text)
- handle switch to forked dependency (go-gomail -> go-mail)
- misc copyright owner updates

* avoid leaking bot knowledge without permission

* [GH-6798] added a new api endpoint to get the bulk reactions for posts (#10049)

* 6798 added a new api to get the bulk reactions for posts

* 6798 added the permsission check before getting the reactions

* GH-6798 added a new app function for the new endpoint

* 6798 added a store method to get reactions for multiple posts

* 6798 connected the app function with the new store function

* 6798 fixed the review comments

* MM-13559 Update model.post.is_valid.file_ids.app_error text per report (#10055)

Ticket: https://mattermost.atlassian.net/browse/MM-13559
Report: https://github.com/mattermost/mattermost-server/issues/10023

* Trigger Login Hooks with OAuth (#10061)

* make BotStore.GetAll deterministic even on duplicate CreateAt

* fix spurious TestMuteCommandSpecificChannel test failure

See
https://community-daily.mattermost.com/core/pl/px9p8s3dzbg1pf3ddrm5cr36uw

* fix race in TestExportUserChannels

* TestExportUserChannels: remove SaveMember call, as it is redundant and used to be silently failing anyway

* MM-13117: bot tokens (#10111)

* eliminate redundant Client/AdminClient declarations

* harden TestUpdateChannelScheme to API failures

* eliminate unnecessary config restoration

* minor cleanup

* make TestGenerateMfaSecret config dependency explicit

* TestCreateUserAccessToken for bots

* TestGetUserAccessToken* for bots

* leverage SessionHasPermissionToUserOrBot for user token APIs

* Test(Revoke|Disable|Enable)UserAccessToken

* make EnableUserAccessTokens explicit, so as to not rely on local config.json

* uncomment TestResetPassword, but still skip

* mark assert(Invalid)Token as helper

* fix whitespace issues

* fix mangled comments

* MM-13116: bot plugin api (#10113)

* MM-13117: expose bot API to plugins

This also changes the `CreatorId` column definition to allow for plugin
ids, as the default unless the plugin overrides is to use the plugin id
here. This branch hasn't hit master yet, so no migration needed.

* gofmt issues

* expunge use of BotList in plugin/client API

* introduce model.BotGetOptions

* use botUserId term for clarity

* MM-13129 Adding functionality to deal with orphaned bots (#10238)

* Add way to list orphaned bots.

* Add /assign route to modify ownership of bot accounts.

* Apply suggestions from code review

Co-Authored-By: crspeller <crspeller@gmail.com>

* MM-13120: add IsBot field to returned user objects (#10103)

* MM-13104: forbid bot login (#10251)

* MM-13104: disallow bot login

* fix shadowing

* MM-13136 Disable user bots when user is disabled. (#10293)

* Disable user bots when user is disabled.

* Grammer.

Co-Authored-By: crspeller <crspeller@gmail.com>

* Fixing bot branch for test changes.

* Don't use external dependancies in bot plugin tests.

* Rename bot CreatorId to OwnerId

* Adding ability to re-enable bots

* Fixing IsBot to not attempt to be saved to DB.

* Adding diagnostics and licencing counting for bot accounts.

* Modifying gorp to allow reading of '-' fields.

* Removing unnessisary nil values from UserCountOptions.

* Changing comment to GoDoc format

* Improving user count SQL

* Some improvments from feedback.

* Omit empty on User.IsBot
2019-03-05 07:06:45 -08:00

462 lines
14 KiB
Go

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package store
import (
"context"
"github.com/mattermost/mattermost-server/einterfaces"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
)
const (
ENABLE_EXPERIMENTAL_REDIS = false
)
type LayeredStoreDatabaseLayer interface {
LayeredStoreSupplier
Store
}
type LayeredStore struct {
TmpContext context.Context
ReactionStore ReactionStore
RoleStore RoleStore
SchemeStore SchemeStore
DatabaseLayer LayeredStoreDatabaseLayer
LocalCacheLayer *LocalCacheSupplier
RedisLayer *RedisSupplier
LayerChainHead LayeredStoreSupplier
GroupStore GroupStore
}
func NewLayeredStore(db LayeredStoreDatabaseLayer, metrics einterfaces.MetricsInterface, cluster einterfaces.ClusterInterface) Store {
store := &LayeredStore{
TmpContext: context.TODO(),
DatabaseLayer: db,
LocalCacheLayer: NewLocalCacheSupplier(metrics, cluster),
}
store.ReactionStore = &LayeredReactionStore{store}
store.RoleStore = &LayeredRoleStore{store}
store.SchemeStore = &LayeredSchemeStore{store}
store.GroupStore = &LayeredGroupStore{store}
// Setup the chain
if ENABLE_EXPERIMENTAL_REDIS {
mlog.Debug("Experimental redis enabled.")
store.RedisLayer = NewRedisSupplier()
store.RedisLayer.SetChainNext(store.DatabaseLayer)
store.LayerChainHead = store.RedisLayer
} else {
store.LocalCacheLayer.SetChainNext(store.DatabaseLayer)
store.LayerChainHead = store.LocalCacheLayer
}
return store
}
type QueryFunction func(LayeredStoreSupplier) *LayeredStoreSupplierResult
func (s *LayeredStore) RunQuery(queryFunction QueryFunction) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
result := queryFunction(s.LayerChainHead)
storeChannel <- result.StoreResult
}()
return storeChannel
}
func (s *LayeredStore) Team() TeamStore {
return s.DatabaseLayer.Team()
}
func (s *LayeredStore) Channel() ChannelStore {
return s.DatabaseLayer.Channel()
}
func (s *LayeredStore) Post() PostStore {
return s.DatabaseLayer.Post()
}
func (s *LayeredStore) User() UserStore {
return s.DatabaseLayer.User()
}
func (s *LayeredStore) Bot() BotStore {
return s.DatabaseLayer.Bot()
}
func (s *LayeredStore) Audit() AuditStore {
return s.DatabaseLayer.Audit()
}
func (s *LayeredStore) ClusterDiscovery() ClusterDiscoveryStore {
return s.DatabaseLayer.ClusterDiscovery()
}
func (s *LayeredStore) Compliance() ComplianceStore {
return s.DatabaseLayer.Compliance()
}
func (s *LayeredStore) Session() SessionStore {
return s.DatabaseLayer.Session()
}
func (s *LayeredStore) OAuth() OAuthStore {
return s.DatabaseLayer.OAuth()
}
func (s *LayeredStore) System() SystemStore {
return s.DatabaseLayer.System()
}
func (s *LayeredStore) Webhook() WebhookStore {
return s.DatabaseLayer.Webhook()
}
func (s *LayeredStore) Command() CommandStore {
return s.DatabaseLayer.Command()
}
func (s *LayeredStore) CommandWebhook() CommandWebhookStore {
return s.DatabaseLayer.CommandWebhook()
}
func (s *LayeredStore) Preference() PreferenceStore {
return s.DatabaseLayer.Preference()
}
func (s *LayeredStore) License() LicenseStore {
return s.DatabaseLayer.License()
}
func (s *LayeredStore) Token() TokenStore {
return s.DatabaseLayer.Token()
}
func (s *LayeredStore) Emoji() EmojiStore {
return s.DatabaseLayer.Emoji()
}
func (s *LayeredStore) Status() StatusStore {
return s.DatabaseLayer.Status()
}
func (s *LayeredStore) FileInfo() FileInfoStore {
return s.DatabaseLayer.FileInfo()
}
func (s *LayeredStore) Reaction() ReactionStore {
return s.ReactionStore
}
func (s *LayeredStore) Job() JobStore {
return s.DatabaseLayer.Job()
}
func (s *LayeredStore) UserAccessToken() UserAccessTokenStore {
return s.DatabaseLayer.UserAccessToken()
}
func (s *LayeredStore) ChannelMemberHistory() ChannelMemberHistoryStore {
return s.DatabaseLayer.ChannelMemberHistory()
}
func (s *LayeredStore) Plugin() PluginStore {
return s.DatabaseLayer.Plugin()
}
func (s *LayeredStore) Role() RoleStore {
return s.RoleStore
}
func (s *LayeredStore) TermsOfService() TermsOfServiceStore {
return s.DatabaseLayer.TermsOfService()
}
func (s *LayeredStore) UserTermsOfService() UserTermsOfServiceStore {
return s.DatabaseLayer.UserTermsOfService()
}
func (s *LayeredStore) Scheme() SchemeStore {
return s.SchemeStore
}
func (s *LayeredStore) Group() GroupStore {
return s.GroupStore
}
func (s *LayeredStore) LinkMetadata() LinkMetadataStore {
return s.DatabaseLayer.LinkMetadata()
}
func (s *LayeredStore) MarkSystemRanUnitTests() {
s.DatabaseLayer.MarkSystemRanUnitTests()
}
func (s *LayeredStore) Close() {
s.DatabaseLayer.Close()
}
func (s *LayeredStore) LockToMaster() {
s.DatabaseLayer.LockToMaster()
}
func (s *LayeredStore) UnlockFromMaster() {
s.DatabaseLayer.UnlockFromMaster()
}
func (s *LayeredStore) DropAllTables() {
defer s.LocalCacheLayer.Invalidate()
s.DatabaseLayer.DropAllTables()
}
func (s *LayeredStore) TotalMasterDbConnections() int {
return s.DatabaseLayer.TotalMasterDbConnections()
}
func (s *LayeredStore) TotalReadDbConnections() int {
return s.DatabaseLayer.TotalReadDbConnections()
}
func (s *LayeredStore) TotalSearchDbConnections() int {
return s.DatabaseLayer.TotalSearchDbConnections()
}
type LayeredReactionStore struct {
*LayeredStore
}
func (s *LayeredReactionStore) Save(reaction *model.Reaction) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.ReactionSave(s.TmpContext, reaction)
})
}
func (s *LayeredReactionStore) Delete(reaction *model.Reaction) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.ReactionDelete(s.TmpContext, reaction)
})
}
func (s *LayeredReactionStore) GetForPost(postId string, allowFromCache bool) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.ReactionGetForPost(s.TmpContext, postId)
})
}
func (s *LayeredReactionStore) BulkGetForPosts(postIds []string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.ReactionsBulkGetForPosts(s.TmpContext, postIds)
})
}
func (s *LayeredReactionStore) DeleteAllWithEmojiName(emojiName string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.ReactionDeleteAllWithEmojiName(s.TmpContext, emojiName)
})
}
func (s *LayeredReactionStore) PermanentDeleteBatch(endTime int64, limit int64) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.ReactionPermanentDeleteBatch(s.TmpContext, endTime, limit)
})
}
type LayeredRoleStore struct {
*LayeredStore
}
func (s *LayeredRoleStore) Save(role *model.Role) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleSave(s.TmpContext, role)
})
}
func (s *LayeredRoleStore) Get(roleId string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleGet(s.TmpContext, roleId)
})
}
func (s *LayeredRoleStore) GetByName(name string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleGetByName(s.TmpContext, name)
})
}
func (s *LayeredRoleStore) GetByNames(names []string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleGetByNames(s.TmpContext, names)
})
}
func (s *LayeredRoleStore) Delete(roldId string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleDelete(s.TmpContext, roldId)
})
}
func (s *LayeredRoleStore) PermanentDeleteAll() StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RolePermanentDeleteAll(s.TmpContext)
})
}
type LayeredSchemeStore struct {
*LayeredStore
}
func (s *LayeredSchemeStore) Save(scheme *model.Scheme) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.SchemeSave(s.TmpContext, scheme)
})
}
func (s *LayeredSchemeStore) Get(schemeId string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.SchemeGet(s.TmpContext, schemeId)
})
}
func (s *LayeredSchemeStore) GetByName(schemeName string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.SchemeGetByName(s.TmpContext, schemeName)
})
}
func (s *LayeredSchemeStore) Delete(schemeId string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.SchemeDelete(s.TmpContext, schemeId)
})
}
func (s *LayeredSchemeStore) GetAllPage(scope string, offset int, limit int) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.SchemeGetAllPage(s.TmpContext, scope, offset, limit)
})
}
func (s *LayeredSchemeStore) PermanentDeleteAll() StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.SchemePermanentDeleteAll(s.TmpContext)
})
}
type LayeredGroupStore struct {
*LayeredStore
}
func (s *LayeredGroupStore) Create(group *model.Group) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupCreate(s.TmpContext, group)
})
}
func (s *LayeredGroupStore) Get(groupID string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGet(s.TmpContext, groupID)
})
}
func (s *LayeredGroupStore) GetByRemoteID(remoteID string, groupSource model.GroupSource) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetByRemoteID(s.TmpContext, remoteID, groupSource)
})
}
func (s *LayeredGroupStore) GetAllBySource(groupSource model.GroupSource) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetAllBySource(s.TmpContext, groupSource)
})
}
func (s *LayeredGroupStore) Update(group *model.Group) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupUpdate(s.TmpContext, group)
})
}
func (s *LayeredGroupStore) Delete(groupID string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupDelete(s.TmpContext, groupID)
})
}
func (s *LayeredGroupStore) GetMemberUsers(groupID string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetMemberUsers(s.TmpContext, groupID)
})
}
func (s *LayeredGroupStore) GetMemberUsersPage(groupID string, offset int, limit int) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetMemberUsersPage(s.TmpContext, groupID, offset, limit)
})
}
func (s *LayeredGroupStore) GetMemberCount(groupID string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetMemberCount(s.TmpContext, groupID)
})
}
func (s *LayeredGroupStore) CreateOrRestoreMember(groupID string, userID string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupCreateOrRestoreMember(s.TmpContext, groupID, userID)
})
}
func (s *LayeredGroupStore) DeleteMember(groupID string, userID string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupDeleteMember(s.TmpContext, groupID, userID)
})
}
func (s *LayeredGroupStore) CreateGroupSyncable(groupSyncable *model.GroupSyncable) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupCreateGroupSyncable(s.TmpContext, groupSyncable)
})
}
func (s *LayeredGroupStore) GetGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetGroupSyncable(s.TmpContext, groupID, syncableID, syncableType)
})
}
func (s *LayeredGroupStore) GetAllGroupSyncablesByGroupId(groupID string, syncableType model.GroupSyncableType) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupGetAllGroupSyncablesByGroup(s.TmpContext, groupID, syncableType)
})
}
func (s *LayeredGroupStore) UpdateGroupSyncable(groupSyncable *model.GroupSyncable) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupUpdateGroupSyncable(s.TmpContext, groupSyncable)
})
}
func (s *LayeredGroupStore) DeleteGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.GroupDeleteGroupSyncable(s.TmpContext, groupID, syncableID, syncableType)
})
}
func (s *LayeredGroupStore) PendingAutoAddTeamMembers(minGroupMembersCreateAt int64) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.PendingAutoAddTeamMembers(s.TmpContext, minGroupMembersCreateAt)
})
}
func (s *LayeredGroupStore) PendingAutoAddChannelMembers(minGroupMembersCreateAt int64) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.PendingAutoAddChannelMembers(s.TmpContext, minGroupMembersCreateAt)
})
}