[MM-55028] Added OAuthOutgoingConnection store (#25221)
* OAuthOutgoingConnection model * added store * make generated * add missing license headers * fix receiver name * i18n * i18n sorting * update migrations from master * make migrations-extract * update retrylayer tests * replaced sql query with id pagination * fixed flaky tests * missing columns * missing columns on save/update * typo * improved tests * remove enum from mysql colum * add password credentials to store * renamed migrations * model change suggestions * refactor test functionsn * migration typo * refactor store table names * updated sanitize test * oauthoutgoingconnection -> outgoingoauthconnection * signature change * i18n update * granttype typo * uppercase typo * lowercase store name
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
8158c0e614
Коммит
b40366dbdf
@@ -0,0 +1,97 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type SqlOutgoingOAuthConnectionStore struct {
|
||||
*SqlStore
|
||||
}
|
||||
|
||||
func newSqlOutgoingOAuthConnectionStore(sqlStore *SqlStore) store.OutgoingOAuthConnectionStore {
|
||||
return &SqlOutgoingOAuthConnectionStore{sqlStore}
|
||||
}
|
||||
|
||||
func (s *SqlOutgoingOAuthConnectionStore) SaveConnection(c request.CTX, conn *model.OutgoingOAuthConnection) (*model.OutgoingOAuthConnection, error) {
|
||||
if conn.Id != "" {
|
||||
return nil, store.NewErrInvalidInput("OutgoingOAuthConnection", "Id", conn.Id)
|
||||
}
|
||||
|
||||
conn.PreSave()
|
||||
if err := conn.IsValid(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := s.GetMasterX().NamedExec(`INSERT INTO OutgoingOAuthConnections
|
||||
(Id, Name, ClientId, ClientSecret, CreateAt, UpdateAt, CreatorId, OAuthTokenURL, GrantType, Audiences)
|
||||
VALUES
|
||||
(:Id, :Name, :ClientId, :ClientSecret, :CreateAt, :UpdateAt, :CreatorId, :OAuthTokenURL, :GrantType, :Audiences)`, conn); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to save OutgoingOAuthConnection")
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (s *SqlOutgoingOAuthConnectionStore) UpdateConnection(c request.CTX, conn *model.OutgoingOAuthConnection) (*model.OutgoingOAuthConnection, error) {
|
||||
if conn.Id == "" {
|
||||
return nil, store.NewErrInvalidInput("OutgoingOAuthConnection", "Id", conn.Id)
|
||||
}
|
||||
|
||||
conn.PreUpdate()
|
||||
if err := conn.IsValid(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := s.GetMasterX().NamedExec(`UPDATE OutgoingOAuthConnections SET
|
||||
Name=:Name, ClientId=:ClientId, ClientSecret=:ClientSecret, UpdateAt=:UpdateAt, OAuthTokenURL=:OAuthTokenURL, GrantType=:GrantType, Audiences=:Audiences
|
||||
WHERE Id=:Id`, conn); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to update OutgoingOAuthConnection")
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (s *SqlOutgoingOAuthConnectionStore) GetConnection(c request.CTX, id string) (*model.OutgoingOAuthConnection, error) {
|
||||
conn := &model.OutgoingOAuthConnection{}
|
||||
if err := s.GetReplicaX().Get(conn, `SELECT * FROM OutgoingOAuthConnections WHERE Id=?`, id); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, store.NewErrNotFound("OutgoingOAuthConnection", id)
|
||||
}
|
||||
return nil, errors.Wrap(err, "failed to get OutgoingOAuthConnection")
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (s *SqlOutgoingOAuthConnectionStore) GetConnections(c request.CTX, filters model.OutgoingOAuthConnectionGetConnectionsFilter) ([]*model.OutgoingOAuthConnection, error) {
|
||||
filters.SetDefaults()
|
||||
|
||||
conns := []*model.OutgoingOAuthConnection{}
|
||||
query := s.getQueryBuilder().
|
||||
Select("*").
|
||||
From("OutgoingOAuthConnections").
|
||||
OrderBy("Id").
|
||||
Limit(uint64(filters.Limit))
|
||||
|
||||
if filters.OffsetId != "" {
|
||||
query = query.Where("Id > ?", filters.OffsetId)
|
||||
}
|
||||
|
||||
if err := s.GetReplicaX().SelectBuilder(&conns, query); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get OutgoingOAuthConnections")
|
||||
}
|
||||
|
||||
return conns, nil
|
||||
}
|
||||
|
||||
func (s *SqlOutgoingOAuthConnectionStore) DeleteConnection(c request.CTX, id string) error {
|
||||
if _, err := s.GetMasterX().Exec(`DELETE FROM OutgoingOAuthConnections WHERE Id=?`, id); err != nil {
|
||||
return errors.Wrap(err, "failed to delete OutgoingOAuthConnection")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store/storetest"
|
||||
)
|
||||
|
||||
func TestOutgoingOAuthConnectionStore(t *testing.T) {
|
||||
StoreTest(t, storetest.TestOutgoingOAuthConnectionStore)
|
||||
}
|
||||
@@ -78,6 +78,7 @@ type SqlStoreStores struct {
|
||||
compliance store.ComplianceStore
|
||||
session store.SessionStore
|
||||
oauth store.OAuthStore
|
||||
outgoingOAuthConnection store.OutgoingOAuthConnectionStore
|
||||
system store.SystemStore
|
||||
webhook store.WebhookStore
|
||||
command store.CommandStore
|
||||
@@ -199,6 +200,7 @@ func New(settings model.SqlSettings, metrics einterfaces.MetricsInterface) (*Sql
|
||||
store.stores.compliance = newSqlComplianceStore(store)
|
||||
store.stores.session = newSqlSessionStore(store)
|
||||
store.stores.oauth = newSqlOAuthStore(store)
|
||||
store.stores.outgoingOAuthConnection = newSqlOutgoingOAuthConnectionStore(store)
|
||||
store.stores.system = newSqlSystemStore(store)
|
||||
store.stores.webhook = newSqlWebhookStore(store, metrics)
|
||||
store.stores.command = newSqlCommandStore(store)
|
||||
@@ -948,6 +950,10 @@ func (ss *SqlStore) OAuth() store.OAuthStore {
|
||||
return ss.stores.oauth
|
||||
}
|
||||
|
||||
func (ss *SqlStore) OutgoingOAuthConnection() store.OutgoingOAuthConnectionStore {
|
||||
return ss.stores.outgoingOAuthConnection
|
||||
}
|
||||
|
||||
func (ss *SqlStore) System() store.SystemStore {
|
||||
return ss.stores.system
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user