2021-01-14 01:56:01 +01:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-09-08 06:52:03 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-01-14 01:56:01 +01:00
|
|
|
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-07-29 11:56:57 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-01-14 01:56:01 +01:00
|
|
|
|
2021-08-25 22:08:01 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
|
|
|
|
2021-01-14 01:56:01 +01:00
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
2021-09-08 06:52:03 +02:00
|
|
|
var (
|
2021-09-13 14:12:32 +02:00
|
|
|
errUnsupportedOperation = errors.New("unsupported operation")
|
2021-09-08 06:52:03 +02:00
|
|
|
)
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) upsertWorkspaceSignupToken(db sq.BaseRunner, workspace model.Workspace) error {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2021-01-14 01:56:01 +01:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(db).
|
2021-04-17 09:06:57 +02:00
|
|
|
Insert(s.tablePrefix+"workspaces").
|
2021-01-14 01:56:01 +01:00
|
|
|
Columns(
|
|
|
|
"id",
|
|
|
|
"signup_token",
|
|
|
|
"modified_by",
|
|
|
|
"update_at",
|
|
|
|
).
|
|
|
|
Values(
|
|
|
|
workspace.ID,
|
|
|
|
workspace.SignupToken,
|
|
|
|
workspace.ModifiedBy,
|
|
|
|
now,
|
2021-04-22 22:53:01 +02:00
|
|
|
)
|
|
|
|
if s.dbType == mysqlDBType {
|
2021-06-21 11:21:42 +02:00
|
|
|
query = query.Suffix("ON DUPLICATE KEY UPDATE signup_token = ?, modified_by = ?, update_at = ?",
|
|
|
|
workspace.SignupToken, workspace.ModifiedBy, now)
|
2021-04-22 22:53:01 +02:00
|
|
|
} else {
|
2021-06-21 11:21:42 +02:00
|
|
|
query = query.Suffix(
|
2021-09-08 06:52:03 +02:00
|
|
|
`ON CONFLICT (id)
|
2021-06-21 11:21:42 +02:00
|
|
|
DO UPDATE SET signup_token = EXCLUDED.signup_token, modified_by = EXCLUDED.modified_by, update_at = EXCLUDED.update_at`,
|
|
|
|
)
|
2021-04-22 22:53:01 +02:00
|
|
|
}
|
2021-01-14 01:56:01 +01:00
|
|
|
|
|
|
|
_, err := query.Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) upsertWorkspaceSettings(db sq.BaseRunner, workspace model.Workspace) error {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2021-10-05 15:52:59 +02:00
|
|
|
signupToken := utils.NewID(utils.IDTypeToken)
|
2021-01-14 01:56:01 +01:00
|
|
|
|
|
|
|
settingsJSON, err := json.Marshal(workspace.Settings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(db).
|
2021-04-17 09:06:57 +02:00
|
|
|
Insert(s.tablePrefix+"workspaces").
|
2021-01-14 01:56:01 +01:00
|
|
|
Columns(
|
|
|
|
"id",
|
2021-07-29 11:56:57 +02:00
|
|
|
"signup_token",
|
2021-01-14 01:56:01 +01:00
|
|
|
"settings",
|
|
|
|
"modified_by",
|
|
|
|
"update_at",
|
|
|
|
).
|
|
|
|
Values(
|
|
|
|
workspace.ID,
|
2021-07-29 11:56:57 +02:00
|
|
|
signupToken,
|
2021-01-14 01:56:01 +01:00
|
|
|
settingsJSON,
|
|
|
|
workspace.ModifiedBy,
|
|
|
|
now,
|
2021-04-22 22:53:01 +02:00
|
|
|
)
|
|
|
|
if s.dbType == mysqlDBType {
|
|
|
|
query = query.Suffix("ON DUPLICATE KEY UPDATE settings = ?, modified_by = ?, update_at = ?", settingsJSON, workspace.ModifiedBy, now)
|
|
|
|
} else {
|
2021-06-21 11:21:42 +02:00
|
|
|
query = query.Suffix(
|
2021-09-08 06:52:03 +02:00
|
|
|
`ON CONFLICT (id)
|
2021-06-21 11:21:42 +02:00
|
|
|
DO UPDATE SET settings = EXCLUDED.settings, modified_by = EXCLUDED.modified_by, update_at = EXCLUDED.update_at`,
|
|
|
|
)
|
2021-04-22 22:53:01 +02:00
|
|
|
}
|
2021-01-14 01:56:01 +01:00
|
|
|
|
|
|
|
_, err = query.Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) getWorkspace(db sq.BaseRunner, id string) (*model.Workspace, error) {
|
2021-01-14 01:56:01 +01:00
|
|
|
var settingsJSON string
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(db).
|
2021-01-14 01:56:01 +01:00
|
|
|
Select(
|
|
|
|
"id",
|
|
|
|
"signup_token",
|
2021-04-22 22:53:01 +02:00
|
|
|
"COALESCE(settings, '{}')",
|
2021-01-14 01:56:01 +01:00
|
|
|
"modified_by",
|
|
|
|
"update_at",
|
|
|
|
).
|
2021-04-17 09:06:57 +02:00
|
|
|
From(s.tablePrefix + "workspaces").
|
2021-06-21 11:21:42 +02:00
|
|
|
Where(sq.Eq{"id": id})
|
2021-01-14 01:56:01 +01:00
|
|
|
row := query.QueryRow()
|
|
|
|
workspace := model.Workspace{}
|
|
|
|
|
|
|
|
err := row.Scan(
|
|
|
|
&workspace.ID,
|
|
|
|
&workspace.SignupToken,
|
|
|
|
&settingsJSON,
|
|
|
|
&workspace.ModifiedBy,
|
|
|
|
&workspace.UpdateAt,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(settingsJSON), &workspace.Settings)
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
s.logger.Error(`ERROR GetWorkspace settings json.Unmarshal`, mlog.Err(err))
|
2021-01-14 01:56:01 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &workspace, nil
|
|
|
|
}
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) hasWorkspaceAccess(db sq.BaseRunner, userID string, workspaceID string) (bool, error) {
|
2021-05-24 19:06:11 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
2021-06-04 16:38:49 +02:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) getWorkspaceCount(db sq.BaseRunner) (int64, error) {
|
|
|
|
query := s.getQueryBuilder(db).
|
2021-06-04 16:38:49 +02:00
|
|
|
Select(
|
|
|
|
"COUNT(*) AS count",
|
|
|
|
).
|
|
|
|
From(s.tablePrefix + "workspaces")
|
|
|
|
|
|
|
|
rows, err := query.Query()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("ERROR GetWorkspaceCount", mlog.Err(err))
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-07-06 22:44:11 +02:00
|
|
|
defer s.CloseRows(rows)
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
rows.Next()
|
|
|
|
err = rows.Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Failed to fetch workspace count", mlog.Err(err))
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
2021-09-08 06:52:03 +02:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) getUserWorkspaces(_ sq.BaseRunner, _ string) ([]model.UserWorkspace, error) {
|
2021-09-13 14:12:32 +02:00
|
|
|
return nil, fmt.Errorf("GetUserWorkspaces %w", errUnsupportedOperation)
|
2021-09-08 06:52:03 +02:00
|
|
|
}
|