From 99d7b81d6c85cd2a06319c325e18313f56847773 Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Mon, 1 Jul 2024 17:07:55 +0530 Subject: [PATCH 1/7] refactor: removed the mattermost auth check --- server/boards/boardsapp.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/server/boards/boardsapp.go b/server/boards/boardsapp.go index c1a733e4..5e08fdfa 100644 --- a/server/boards/boardsapp.go +++ b/server/boards/boardsapp.go @@ -91,13 +91,11 @@ func NewBoardsApp(api model.ServicesAPI) (*BoardsApp, error) { if err != nil { return nil, fmt.Errorf("error initializing the DB: %w", err) } - if cfg.AuthMode == server.MattermostAuthMod { - layeredStore, err2 := mattermostauthlayer.New(cfg.DBType, sqlDB, db, logger, api, storeParams.TablePrefix) - if err2 != nil { - return nil, fmt.Errorf("error initializing the DB: %w", err2) - } - db = layeredStore + layeredStore, err2 := mattermostauthlayer.New(cfg.DBType, sqlDB, db, logger, api, storeParams.TablePrefix) + if err2 != nil { + return nil, fmt.Errorf("error initializing the DB: %w", err2) } + db = layeredStore permissionsService := mmpermissions.New(db, api, logger) From c18a26c5f24aaa0feb1d821b70d786d697c40a1b Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Tue, 2 Jul 2024 13:06:21 +0530 Subject: [PATCH 2/7] refactor: removed standalone sql part of code --- server/app/helper_test.go | 3 +- server/server/initHandlers.go | 3 +- server/server/server.go | 19 +- server/services/store/sqlstore/board.go | 136 ------ server/services/store/sqlstore/file.go | 92 ---- .../services/store/sqlstore/public_methods.go | 69 +-- server/services/store/sqlstore/session.go | 111 ----- server/services/store/sqlstore/sqlstore.go | 13 - server/services/store/sqlstore/team.go | 40 -- server/services/store/sqlstore/user.go | 406 ------------------ server/ws/server.go | 4 +- server/ws/server_test.go | 6 +- 12 files changed, 43 insertions(+), 859 deletions(-) delete mode 100644 server/services/store/sqlstore/file.go delete mode 100644 server/services/store/sqlstore/session.go diff --git a/server/app/helper_test.go b/server/app/helper_test.go index 43b46e9e..828e948a 100644 --- a/server/app/helper_test.go +++ b/server/app/helper_test.go @@ -36,8 +36,7 @@ func SetupTestHelper(t *testing.T) (*TestHelper, func()) { filesBackend := &mocks.FileBackend{} auth := auth.New(&cfg, store, nil) logger, _ := mlog.NewLogger() - sessionToken := "TESTTOKEN" - wsserver := ws.NewServer(auth, sessionToken, false, logger, store) + wsserver := ws.NewServer(auth, logger, store) webhook := webhook.NewClient(&cfg, logger) metricsService := metrics.NewMetrics(metrics.InstanceInfo{}) diff --git a/server/server/initHandlers.go b/server/server/initHandlers.go index e891032a..6f71a462 100644 --- a/server/server/initHandlers.go +++ b/server/server/initHandlers.go @@ -1,6 +1,5 @@ package server func (s *Server) initHandlers() { - cfg := s.config - s.api.MattermostAuth = cfg.AuthMode == MattermostAuthMod + s.api.MattermostAuth = true } diff --git a/server/server/server.go b/server/server/server.go index b5838bf2..2139b0f8 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -40,10 +40,6 @@ import ( const ( cleanupSessionTaskFrequency = 10 * time.Minute updateMetricsTaskFrequency = 15 * time.Minute - - minSessionExpiryTime = int64(60 * 60 * 24 * 31) // 31 days - - MattermostAuthMod = "mattermost" ) type Server struct { @@ -78,7 +74,7 @@ func New(params Params) (*Server, error) { // if no ws adapter is provided, we spin up a websocket server wsAdapter := params.WSAdapter if wsAdapter == nil { - wsAdapter = ws.NewServer(authenticator, params.SingleUserToken, params.Cfg.AuthMode == MattermostAuthMod, params.Logger, params.DBStore) + wsAdapter = ws.NewServer(authenticator, params.Logger, params.DBStore) } filesBackendSettings := filestore.FileBackendSettings{} @@ -253,19 +249,6 @@ func (s *Server) Start() error { } } - if s.config.AuthMode != MattermostAuthMod { - s.cleanUpSessionsTask = scheduler.CreateRecurringTask("cleanUpSessions", func() { - secondsAgo := minSessionExpiryTime - if secondsAgo < s.config.SessionExpireTime { - secondsAgo = s.config.SessionExpireTime - } - - if err := s.store.CleanUpSessions(secondsAgo); err != nil { - s.logger.Error("Unable to clean up the sessions", mlog.Err(err)) - } - }, cleanupSessionTaskFrequency) - } - metricsUpdater := func() { blockCounts, err := s.store.GetBlockCountsByType() if err != nil { diff --git a/server/services/store/sqlstore/board.go b/server/services/store/sqlstore/board.go index 48998c68..42896baa 100644 --- a/server/services/store/sqlstore/board.go +++ b/server/services/store/sqlstore/board.go @@ -234,36 +234,6 @@ func (s *SQLStore) getBoard(db sq.BaseRunner, boardID string) (*model.Board, err return s.getBoardByCondition(db, sq.Eq{"id": boardID}) } -func (s *SQLStore) getBoardsForUserAndTeam(db sq.BaseRunner, userID, teamID string, includePublicBoards bool) ([]*model.Board, error) { - query := s.getQueryBuilder(db). - Select(boardFields("b.")...). - Distinct(). - From(s.tablePrefix + "boards as b"). - LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id"). - Where(sq.Eq{"b.team_id": teamID}). - Where(sq.Eq{"b.is_template": false}) - - if includePublicBoards { - query = query.Where(sq.Or{ - sq.Eq{"b.type": model.BoardTypeOpen}, - sq.Eq{"bm.user_id": userID}, - }) - } else { - query = query.Where(sq.Or{ - sq.Eq{"bm.user_id": userID}, - }) - } - - rows, err := query.Query() - if err != nil { - s.logger.Error(`getBoardsForUserAndTeam ERROR`, mlog.Err(err)) - return nil, err - } - defer s.CloseRows(rows) - - return s.boardsFromRows(rows) -} - func (s *SQLStore) getBoardsInTeamByIds(db sq.BaseRunner, boardIDs []string, teamID string) ([]*model.Board, error) { query := s.getQueryBuilder(db). Select(boardFields("b.")...). @@ -646,112 +616,6 @@ func (s *SQLStore) getMembersForBoard(db sq.BaseRunner, boardID string) ([]*mode return s.boardMembersFromRows(rows) } -// searchBoardsForUser returns all boards that match with the -// term that are either private and which the user is a member of, or -// they're open, regardless of the user membership. -// Search is case-insensitive. -func (s *SQLStore) searchBoardsForUser(db sq.BaseRunner, term string, searchField model.BoardSearchField, userID string, includePublicBoards bool) ([]*model.Board, error) { - query := s.getQueryBuilder(db). - Select(boardFields("b.")...). - Distinct(). - From(s.tablePrefix + "boards as b"). - LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id"). - Where(sq.Eq{"b.is_template": false}) - - if includePublicBoards { - query = query.Where(sq.Or{ - sq.Eq{"b.type": model.BoardTypeOpen}, - sq.Eq{"bm.user_id": userID}, - }) - } else { - query = query.Where(sq.Or{ - sq.Eq{"bm.user_id": userID}, - }) - } - - if term != "" { - if searchField == model.BoardSearchFieldPropertyName { - switch s.dbType { - case model.PostgresDBType: - where := "b.properties->? is not null" - query = query.Where(where, term) - case model.MysqlDBType, model.SqliteDBType: - where := "JSON_EXTRACT(b.properties, ?) IS NOT NULL" - query = query.Where(where, "$."+term) - default: - where := "b.properties LIKE ?" - query = query.Where(where, "%\""+term+"\"%") - } - } else { // model.BoardSearchFieldTitle - // break search query into space separated words - // and search for all words. - // This should later be upgraded to industrial-strength - // word tokenizer, that uses much more than space - // to break words. - conditions := sq.And{} - for _, word := range strings.Split(strings.TrimSpace(term), " ") { - conditions = append(conditions, sq.Like{"lower(b.title)": "%" + strings.ToLower(word) + "%"}) - } - query = query.Where(conditions) - } - } - - rows, err := query.Query() - if err != nil { - s.logger.Error(`searchBoardsForUser ERROR`, mlog.Err(err)) - return nil, err - } - defer s.CloseRows(rows) - - return s.boardsFromRows(rows) -} - -// searchBoardsForUserInTeam returns all boards that match with the -// term that are either private and which the user is a member of, or -// they're open, regardless of the user membership. -// Search is case-insensitive. -func (s *SQLStore) searchBoardsForUserInTeam(db sq.BaseRunner, teamID, term, userID string) ([]*model.Board, error) { - query := s.getQueryBuilder(db). - Select(boardFields("b.")...). - Distinct(). - From(s.tablePrefix + "boards as b"). - LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id"). - Where(sq.Eq{"b.is_template": false}). - Where(sq.Eq{"b.team_id": teamID}). - Where(sq.Or{ - sq.Eq{"b.type": model.BoardTypeOpen}, - sq.And{ - sq.Eq{"b.type": model.BoardTypePrivate}, - sq.Eq{"bm.user_id": userID}, - }, - }) - - if term != "" { - // break search query into space separated words - // and search for all words. - // This should later be upgraded to industrial-strength - // word tokenizer, that uses much more than space - // to break words. - - conditions := sq.And{} - - for _, word := range strings.Split(strings.TrimSpace(term), " ") { - conditions = append(conditions, sq.Like{"lower(b.title)": "%" + strings.ToLower(word) + "%"}) - } - - query = query.Where(conditions) - } - - rows, err := query.Query() - if err != nil { - s.logger.Error(`searchBoardsForUser ERROR`, mlog.Err(err)) - return nil, err - } - defer s.CloseRows(rows) - - return s.boardsFromRows(rows) -} - func (s *SQLStore) getBoardHistory(db sq.BaseRunner, boardID string, opts model.QueryBoardHistoryOptions) ([]*model.Board, error) { var order string if opts.Descending { diff --git a/server/services/store/sqlstore/file.go b/server/services/store/sqlstore/file.go deleted file mode 100644 index 72645690..00000000 --- a/server/services/store/sqlstore/file.go +++ /dev/null @@ -1,92 +0,0 @@ -package sqlstore - -import ( - "database/sql" - "errors" - - sq "github.com/Masterminds/squirrel" - - "github.com/mattermost/mattermost-plugin-boards/server/model" - - mmModel "github.com/mattermost/mattermost/server/public/model" - "github.com/mattermost/mattermost/server/public/shared/mlog" -) - -func (s *SQLStore) saveFileInfo(db sq.BaseRunner, fileInfo *mmModel.FileInfo) error { - query := s.getQueryBuilder(db). - Insert(s.tablePrefix+"file_info"). - Columns( - "id", - "create_at", - "name", - "extension", - "size", - "delete_at", - "path", - "archived", - ). - Values( - fileInfo.Id, - fileInfo.CreateAt, - fileInfo.Name, - fileInfo.Extension, - fileInfo.Size, - fileInfo.DeleteAt, - fileInfo.Path, - false, - ) - - if _, err := query.Exec(); err != nil { - s.logger.Error( - "failed to save fileinfo", - mlog.String("file_name", fileInfo.Name), - mlog.Int("size", fileInfo.Size), - mlog.Err(err), - ) - return err - } - - return nil -} - -func (s *SQLStore) getFileInfo(db sq.BaseRunner, id string) (*mmModel.FileInfo, error) { - query := s.getQueryBuilder(db). - Select( - "id", - "create_at", - "delete_at", - "name", - "extension", - "size", - "archived", - "path", - ). - From(s.tablePrefix + "file_info"). - Where(sq.Eq{"Id": id}) - - row := query.QueryRow() - - fileInfo := mmModel.FileInfo{} - - err := row.Scan( - &fileInfo.Id, - &fileInfo.CreateAt, - &fileInfo.DeleteAt, - &fileInfo.Name, - &fileInfo.Extension, - &fileInfo.Size, - &fileInfo.Archived, - &fileInfo.Path, - ) - - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil, model.NewErrNotFound("file info ID=" + id) - } - - s.logger.Error("error scanning fileinfo row", mlog.String("id", id), mlog.Err(err)) - return nil, err - } - - return &fileInfo, nil -} diff --git a/server/services/store/sqlstore/public_methods.go b/server/services/store/sqlstore/public_methods.go index 45e1b53c..98229f18 100644 --- a/server/services/store/sqlstore/public_methods.go +++ b/server/services/store/sqlstore/public_methods.go @@ -14,6 +14,7 @@ package sqlstore import ( "context" + "errors" "time" "github.com/mattermost/mattermost-plugin-boards/server/model" @@ -47,12 +48,12 @@ func (s *SQLStore) AddUpdateCategoryBoard(userID string, categoryID string, boar } func (s *SQLStore) CanSeeUser(seerID string, seenID string) (bool, error) { - return s.canSeeUser(s.db, seerID, seenID) + return false, errors.New("can see user not supported in focalboard, will fetch from mattermost") } func (s *SQLStore) CleanUpSessions(expireTime int64) error { - return s.cleanUpSessions(s.db, expireTime) + return errors.New("no clean up sessions allowed from focalboard, update it using mattermost") } @@ -129,7 +130,7 @@ func (s *SQLStore) CreateCategory(category model.Category) error { } func (s *SQLStore) CreateSession(session *model.Session) error { - return s.createSession(s.db, session) + return errors.New("no update allowed from focalboard, update it using mattermost") } @@ -139,7 +140,7 @@ func (s *SQLStore) CreateSubscription(sub *model.Subscription) (*model.Subscript } func (s *SQLStore) CreateUser(user *model.User) (*model.User, error) { - return s.createUser(s.db, user) + return nil, errors.New("no user creation allowed from focalboard, create it using mattermost") } @@ -241,7 +242,7 @@ func (s *SQLStore) DeleteNotificationHint(blockID string) error { } func (s *SQLStore) DeleteSession(sessionID string) error { - return s.deleteSession(s.db, sessionID) + return errors.New("no delete session allowed from focalboard, update it using mattermost") } @@ -299,7 +300,7 @@ func (s *SQLStore) DuplicateBoard(boardID string, userID string, toTeam string, } func (s *SQLStore) GetActiveUserCount(updatedSecondsAgo int64) (int, error) { - return s.getActiveUserCount(s.db, updatedSecondsAgo) + return 0, errors.New("active user count not supported in focalboard when using plugin mode, will fetch from mattermost") } @@ -409,7 +410,7 @@ func (s *SQLStore) GetBoardsForCompliance(opts model.QueryBoardsForComplianceOpt } func (s *SQLStore) GetBoardsForUserAndTeam(userID string, teamID string, includePublicBoards bool) ([]*model.Board, error) { - return s.getBoardsForUserAndTeam(s.db, userID, teamID, includePublicBoards) + return nil, errors.New("boards for user and team will be fetch from mattermost not from focalboard while using in plugin mode") } @@ -429,17 +430,17 @@ func (s *SQLStore) GetCategory(id string) (*model.Category, error) { } func (s *SQLStore) GetChannel(teamID string, channelID string) (*mmModel.Channel, error) { - return s.getChannel(s.db, teamID, channelID) + return nil, errors.New("channel will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetFileInfo(id string) (*mmModel.FileInfo, error) { - return s.getFileInfo(s.db, id) + return nil, errors.New("file info will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetLicense() *mmModel.License { - return s.getLicense(s.db) + return nil } @@ -469,12 +470,12 @@ func (s *SQLStore) GetNotificationHint(blockID string) (*model.NotificationHint, } func (s *SQLStore) GetRegisteredUserCount() (int, error) { - return s.getRegisteredUserCount(s.db) + return 0, errors.New("registered user count not supported in focalboard, will fetch from mattermost") } func (s *SQLStore) GetSession(token string, expireTime int64) (*model.Session, error) { - return s.getSession(s.db, token, expireTime) + return nil, errors.New("sessions not used when using mattermost") } @@ -519,7 +520,7 @@ func (s *SQLStore) GetSystemSettings() (map[string]string, error) { } func (s *SQLStore) GetTeam(ID string) (*model.Team, error) { - return s.getTeam(s.db, ID) + return nil, errors.New("team will be fetch from mattermost not from focalboard while using in plugin mode") } @@ -529,7 +530,7 @@ func (s *SQLStore) GetTeamCount() (int64, error) { } func (s *SQLStore) GetTeamsForUser(userID string) ([]*model.Team, error) { - return s.getTeamsForUser(s.db, userID) + return nil, errors.New("teams for user will be fetch from mattermost not from focalboard while using in plugin mode") } @@ -544,17 +545,17 @@ func (s *SQLStore) GetUsedCardsCount() (int, error) { } func (s *SQLStore) GetUserByEmail(email string) (*model.User, error) { - return s.getUserByEmail(s.db, email) + return nil, errors.New("user email will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetUserByID(userID string) (*model.User, error) { - return s.getUserByID(s.db, userID) + return nil, errors.New("user id will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetUserByUsername(username string) (*model.User, error) { - return s.getUserByUsername(s.db, username) + return nil, errors.New("user username will be fetch from mattermost not from focalboard while using in plugin mode") } @@ -569,22 +570,22 @@ func (s *SQLStore) GetUserCategoryBoards(userID string, teamID string) ([]model. } func (s *SQLStore) GetUserPreferences(userID string) (mmModel.Preferences, error) { - return s.getUserPreferences(s.db, userID) + return nil, errors.New("user preferences will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetUserTimezone(userID string) (string, error) { - return s.getUserTimezone(s.db, userID) + return "", errors.New("user timezone will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetUsersByTeam(teamID string, asGuestID string, showEmail bool, showName bool) ([]*model.User, error) { - return s.getUsersByTeam(s.db, teamID, asGuestID, showEmail, showName) + return nil, errors.New("users by team will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) GetUsersList(userIDs []string, showEmail bool, showName bool) ([]*model.User, error) { - return s.getUsersList(s.db, userIDs, showEmail, showName) + return nil, errors.New("users list will be fetch from mattermost not from focalboard while using in plugin mode") } @@ -762,17 +763,17 @@ func (s *SQLStore) PatchBoardsAndBlocks(pbab *model.PatchBoardsAndBlocks, userID } func (s *SQLStore) PatchUserPreferences(userID string, patch model.UserPreferencesPatch) (mmModel.Preferences, error) { - return s.patchUserPreferences(s.db, userID, patch) + return nil, errors.New("no patch allowed from focalboard, patch it using mattermost") } func (s *SQLStore) PostMessage(message string, postType string, channelID string) error { - return s.postMessage(s.db, message, postType, channelID) + return errors.New("no post message allowed from focalboard, post it using mattermost") } func (s *SQLStore) RefreshSession(session *model.Session) error { - return s.refreshSession(s.db, session) + return errors.New("no update allowed from focalboard, update it using mattermost") } @@ -816,7 +817,7 @@ func (s *SQLStore) RunDataRetention(globalRetentionDate int64, batchSize int64) } func (s *SQLStore) SaveFileInfo(fileInfo *mmModel.FileInfo) error { - return s.saveFileInfo(s.db, fileInfo) + return errors.New("no save file info allowed from focalboard, save it using mattermost") } @@ -826,27 +827,27 @@ func (s *SQLStore) SaveMember(bm *model.BoardMember) (*model.BoardMember, error) } func (s *SQLStore) SearchBoardsForUser(term string, searchField model.BoardSearchField, userID string, includePublicBoards bool) ([]*model.Board, error) { - return s.searchBoardsForUser(s.db, term, searchField, userID, includePublicBoards) + return nil, errors.New("searching boards for user will be using from mattermost not from focalboard in plugin mode") } func (s *SQLStore) SearchBoardsForUserInTeam(teamID string, term string, userID string) ([]*model.Board, error) { - return s.searchBoardsForUserInTeam(s.db, teamID, term, userID) + return nil, errors.New("searching boards for user in team will be using from mattermost not from focalboard in plugin mode") } func (s *SQLStore) SearchUserChannels(teamID string, userID string, query string) ([]*mmModel.Channel, error) { - return s.searchUserChannels(s.db, teamID, userID, query) + return nil, errors.New("searching user channels will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) SearchUsersByTeam(teamID string, searchQuery string, asGuestID string, excludeBots bool, showEmail bool, showName bool) ([]*model.User, error) { - return s.searchUsersByTeam(s.db, teamID, searchQuery, asGuestID, excludeBots, showEmail, showName) + return nil, errors.New("searching users by team will be fetch from mattermost not from focalboard while using in plugin mode") } func (s *SQLStore) SendMessage(message string, postType string, receipts []string) error { - return s.sendMessage(s.db, message, postType, receipts) + return errors.New("no send message allowed from focalboard, send it using mattermost") } @@ -919,7 +920,7 @@ func (s *SQLStore) UpdateCategory(category model.Category) error { } func (s *SQLStore) UpdateSession(session *model.Session) error { - return s.updateSession(s.db, session) + return errors.New("no update allowed from focalboard, update it using mattermost") } @@ -929,17 +930,17 @@ func (s *SQLStore) UpdateSubscribersNotifiedAt(blockID string, notifiedAt int64) } func (s *SQLStore) UpdateUser(user *model.User) (*model.User, error) { - return s.updateUser(s.db, user) + return nil, errors.New("no update allowed from focalboard, update it using mattermost") } func (s *SQLStore) UpdateUserPassword(username string, password string) error { - return s.updateUserPassword(s.db, username, password) + return errors.New("no update allowed from focalboard, update it using mattermost") } func (s *SQLStore) UpdateUserPasswordByID(userID string, password string) error { - return s.updateUserPasswordByID(s.db, userID, password) + return errors.New("no update allowed from focalboard, update it using mattermost") } diff --git a/server/services/store/sqlstore/session.go b/server/services/store/sqlstore/session.go deleted file mode 100644 index f87b68eb..00000000 --- a/server/services/store/sqlstore/session.go +++ /dev/null @@ -1,111 +0,0 @@ -package sqlstore - -import ( - "encoding/json" - - sq "github.com/Masterminds/squirrel" - "github.com/mattermost/mattermost-plugin-boards/server/model" - "github.com/mattermost/mattermost-plugin-boards/server/utils" -) - -// GetActiveUserCount returns the number of users with active sessions within N seconds ago. -func (s *SQLStore) getActiveUserCount(db sq.BaseRunner, updatedSecondsAgo int64) (int, error) { - query := s.getQueryBuilder(db). - Select("count(distinct user_id)"). - From(s.tablePrefix + "sessions"). - Where(sq.Gt{"update_at": utils.GetMillis() - utils.SecondsToMillis(updatedSecondsAgo)}) - - row := query.QueryRow() - - var count int - err := row.Scan(&count) - if err != nil { - return 0, err - } - - return count, nil -} - -func (s *SQLStore) getSession(db sq.BaseRunner, token string, expireTimeSeconds int64) (*model.Session, error) { - query := s.getQueryBuilder(db). - Select("id", "token", "user_id", "auth_service", "props"). - From(s.tablePrefix + "sessions"). - Where(sq.Eq{"token": token}). - Where(sq.Gt{"update_at": utils.GetMillis() - utils.SecondsToMillis(expireTimeSeconds)}) - - row := query.QueryRow() - session := model.Session{} - - var propsBytes []byte - err := row.Scan(&session.ID, &session.Token, &session.UserID, &session.AuthService, &propsBytes) - if err != nil { - return nil, err - } - - err = json.Unmarshal(propsBytes, &session.Props) - if err != nil { - return nil, err - } - - return &session, nil -} - -func (s *SQLStore) createSession(db sq.BaseRunner, session *model.Session) error { - now := utils.GetMillis() - - propsBytes, err := json.Marshal(session.Props) - if err != nil { - return err - } - - query := s.getQueryBuilder(db).Insert(s.tablePrefix+"sessions"). - Columns("id", "token", "user_id", "auth_service", "props", "create_at", "update_at"). - Values(session.ID, session.Token, session.UserID, session.AuthService, propsBytes, now, now) - - _, err = query.Exec() - return err -} - -func (s *SQLStore) refreshSession(db sq.BaseRunner, session *model.Session) error { - now := utils.GetMillis() - - query := s.getQueryBuilder(db).Update(s.tablePrefix+"sessions"). - Where(sq.Eq{"token": session.Token}). - Set("update_at", now) - - _, err := query.Exec() - return err -} - -func (s *SQLStore) updateSession(db sq.BaseRunner, session *model.Session) error { - now := utils.GetMillis() - - propsBytes, err := json.Marshal(session.Props) - if err != nil { - return err - } - - query := s.getQueryBuilder(db).Update(s.tablePrefix+"sessions"). - Where(sq.Eq{"token": session.Token}). - Set("update_at", now). - Set("props", propsBytes) - - _, err = query.Exec() - return err -} - -func (s *SQLStore) deleteSession(db sq.BaseRunner, sessionID string) error { - query := s.getQueryBuilder(db).Delete(s.tablePrefix + "sessions"). - Where(sq.Eq{"id": sessionID}) - - _, err := query.Exec() - return err -} - -func (s *SQLStore) cleanUpSessions(db sq.BaseRunner, expireTimeSeconds int64) error { - query := s.getQueryBuilder(db).Delete(s.tablePrefix + "sessions"). - Where(sq.Lt{"update_at": utils.GetMillis() - utils.SecondsToMillis(expireTimeSeconds)}) - - _, err := query.Exec() - return err -} diff --git a/server/services/store/sqlstore/sqlstore.go b/server/services/store/sqlstore/sqlstore.go index d9b43a35..a0148c2c 100644 --- a/server/services/store/sqlstore/sqlstore.go +++ b/server/services/store/sqlstore/sqlstore.go @@ -9,7 +9,6 @@ import ( sq "github.com/Masterminds/squirrel" "github.com/mattermost/mattermost-plugin-boards/server/model" - "github.com/mattermost/mattermost-plugin-boards/server/services/store" "github.com/mattermost/mattermost/server/public/pluginapi/cluster" mmModel "github.com/mattermost/mattermost/server/public/model" @@ -171,18 +170,6 @@ func (s *SQLStore) elementInColumn(column string) string { return "" } -func (s *SQLStore) getLicense(db sq.BaseRunner) *mmModel.License { - return nil -} - -func (s *SQLStore) searchUserChannels(db sq.BaseRunner, teamID, userID, query string) ([]*mmModel.Channel, error) { - return nil, store.NewNotSupportedError("search user channels not supported on standalone mode") -} - -func (s *SQLStore) getChannel(db sq.BaseRunner, teamID, channel string) (*mmModel.Channel, error) { - return nil, store.NewNotSupportedError("get channel not supported on standalone mode") -} - func (s *SQLStore) DBVersion() string { var version string var row *sql.Row diff --git a/server/services/store/sqlstore/team.go b/server/services/store/sqlstore/team.go index 3702d04c..7a6c8d59 100644 --- a/server/services/store/sqlstore/team.go +++ b/server/services/store/sqlstore/team.go @@ -91,46 +91,6 @@ func (s *SQLStore) upsertTeamSettings(db sq.BaseRunner, team model.Team) error { return err } -func (s *SQLStore) getTeam(db sq.BaseRunner, id string) (*model.Team, error) { - var settingsJSON string - - query := s.getQueryBuilder(db). - Select( - "id", - "signup_token", - "COALESCE(settings, '{}')", - "modified_by", - "update_at", - ). - From(s.tablePrefix + "teams"). - Where(sq.Eq{"id": id}) - row := query.QueryRow() - team := model.Team{} - - err := row.Scan( - &team.ID, - &team.SignupToken, - &settingsJSON, - &team.ModifiedBy, - &team.UpdateAt, - ) - if err != nil { - return nil, err - } - - err = json.Unmarshal([]byte(settingsJSON), &team.Settings) - if err != nil { - s.logger.Error(`ERROR GetTeam settings json.Unmarshal`, mlog.Err(err)) - return nil, err - } - - return &team, nil -} - -func (s *SQLStore) getTeamsForUser(db sq.BaseRunner, _ string) ([]*model.Team, error) { - return s.getAllTeams(db) -} - func (s *SQLStore) getTeamCount(db sq.BaseRunner) (int64, error) { query := s.getQueryBuilder(db). Select( diff --git a/server/services/store/sqlstore/user.go b/server/services/store/sqlstore/user.go index 849d8191..ef5e747f 100644 --- a/server/services/store/sqlstore/user.go +++ b/server/services/store/sqlstore/user.go @@ -1,23 +1,7 @@ package sqlstore import ( - "database/sql" - "errors" "fmt" - - mmModel "github.com/mattermost/mattermost/server/public/model" - "github.com/mattermost/mattermost/server/v8/channels/store" - - sq "github.com/Masterminds/squirrel" - - "github.com/mattermost/mattermost-plugin-boards/server/model" - "github.com/mattermost/mattermost-plugin-boards/server/utils" - - "github.com/mattermost/mattermost/server/public/shared/mlog" -) - -var ( - errUnsupportedOperation = errors.New("unsupported operation") ) type UserNotFoundError struct { @@ -27,393 +11,3 @@ type UserNotFoundError struct { func (unf UserNotFoundError) Error() string { return fmt.Sprintf("user not found (%s)", unf.id) } - -func (s *SQLStore) getRegisteredUserCount(db sq.BaseRunner) (int, error) { - query := s.getQueryBuilder(db). - Select("count(*)"). - From(s.tablePrefix + "users"). - Where(sq.Eq{"delete_at": 0}) - row := query.QueryRow() - - var count int - err := row.Scan(&count) - if err != nil { - return 0, err - } - - return count, nil -} - -func (s *SQLStore) getUserByCondition(db sq.BaseRunner, condition sq.Eq) (*model.User, error) { - users, err := s.getUsersByCondition(db, condition, 0) - if err != nil { - return nil, err - } - - if len(users) == 0 { - return nil, model.NewErrNotFound("user") - } - - return users[0], nil -} - -func (s *SQLStore) getUsersByCondition(db sq.BaseRunner, condition interface{}, limit uint64) ([]*model.User, error) { - query := s.getQueryBuilder(db). - Select( - "id", - "username", - "email", - "password", - "mfa_secret", - "auth_service", - "auth_data", - "create_at", - "update_at", - "delete_at", - ). - From(s.tablePrefix + "users"). - Where(sq.Eq{"delete_at": 0}). - Where(condition) - - if limit != 0 { - query = query.Limit(limit) - } - - rows, err := query.Query() - if err != nil { - s.logger.Error(`getUsersByCondition ERROR`, mlog.Err(err)) - return nil, err - } - defer s.CloseRows(rows) - - users, err := s.usersFromRows(rows) - if err != nil { - return nil, err - } - - if len(users) == 0 { - return nil, model.NewErrNotFound("user") - } - - return users, nil -} - -func (s *SQLStore) getUserByID(db sq.BaseRunner, userID string) (*model.User, error) { - return s.getUserByCondition(db, sq.Eq{"id": userID}) -} - -func (s *SQLStore) getUsersList(db sq.BaseRunner, userIDs []string, _, _ bool) ([]*model.User, error) { - users, err := s.getUsersByCondition(db, sq.Eq{"id": userIDs}, 0) - if err != nil { - return nil, err - } - - if len(users) != len(userIDs) { - return users, model.NewErrNotAllFound("user", userIDs) - } - - return users, nil -} - -func (s *SQLStore) getUserByEmail(db sq.BaseRunner, email string) (*model.User, error) { - return s.getUserByCondition(db, sq.Eq{"email": email}) -} - -func (s *SQLStore) getUserByUsername(db sq.BaseRunner, username string) (*model.User, error) { - return s.getUserByCondition(db, sq.Eq{"username": username}) -} - -func (s *SQLStore) createUser(db sq.BaseRunner, user *model.User) (*model.User, error) { - now := utils.GetMillis() - user.CreateAt = now - user.UpdateAt = now - user.DeleteAt = 0 - - query := s.getQueryBuilder(db).Insert(s.tablePrefix+"users"). - Columns("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "create_at", "update_at", "delete_at"). - Values(user.ID, user.Username, user.Email, user.Password, user.MfaSecret, user.AuthService, user.AuthData, user.CreateAt, user.UpdateAt, user.DeleteAt) - - _, err := query.Exec() - return user, err -} - -func (s *SQLStore) updateUser(db sq.BaseRunner, user *model.User) (*model.User, error) { - now := utils.GetMillis() - user.UpdateAt = now - - query := s.getQueryBuilder(db).Update(s.tablePrefix+"users"). - Set("username", user.Username). - Set("email", user.Email). - Set("update_at", user.UpdateAt). - Where(sq.Eq{"id": user.ID}) - - result, err := query.Exec() - if err != nil { - return nil, err - } - - rowCount, err := result.RowsAffected() - if err != nil { - return nil, err - } - - if rowCount < 1 { - return nil, UserNotFoundError{user.ID} - } - - return user, nil -} - -func (s *SQLStore) updateUserPassword(db sq.BaseRunner, username, password string) error { - now := utils.GetMillis() - - query := s.getQueryBuilder(db).Update(s.tablePrefix+"users"). - Set("password", password). - Set("update_at", now). - Where(sq.Eq{"username": username}) - - result, err := query.Exec() - if err != nil { - return err - } - - rowCount, err := result.RowsAffected() - if err != nil { - return err - } - - if rowCount < 1 { - return UserNotFoundError{username} - } - - return nil -} - -func (s *SQLStore) updateUserPasswordByID(db sq.BaseRunner, userID, password string) error { - now := utils.GetMillis() - - query := s.getQueryBuilder(db).Update(s.tablePrefix+"users"). - Set("password", password). - Set("update_at", now). - Where(sq.Eq{"id": userID}) - - result, err := query.Exec() - if err != nil { - return err - } - - rowCount, err := result.RowsAffected() - if err != nil { - return err - } - - if rowCount < 1 { - return UserNotFoundError{userID} - } - - return nil -} - -func (s *SQLStore) getUsersByTeam(db sq.BaseRunner, _ string, _ string, _, _ bool) ([]*model.User, error) { - users, err := s.getUsersByCondition(db, nil, 0) - if model.IsErrNotFound(err) { - return []*model.User{}, nil - } - - return users, err -} - -func (s *SQLStore) searchUsersByTeam(db sq.BaseRunner, _ string, searchQuery string, _ string, _, _, _ bool) ([]*model.User, error) { - users, err := s.getUsersByCondition(db, &sq.Like{"username": "%" + searchQuery + "%"}, 10) - if model.IsErrNotFound(err) { - return []*model.User{}, nil - } - - return users, err -} - -func (s *SQLStore) usersFromRows(rows *sql.Rows) ([]*model.User, error) { - users := []*model.User{} - - for rows.Next() { - var user model.User - - err := rows.Scan( - &user.ID, - &user.Username, - &user.Email, - &user.Password, - &user.MfaSecret, - &user.AuthService, - &user.AuthData, - &user.CreateAt, - &user.UpdateAt, - &user.DeleteAt, - ) - if err != nil { - return nil, err - } - - users = append(users, &user) - } - - return users, nil -} - -func (s *SQLStore) patchUserPreferences(db sq.BaseRunner, userID string, patch model.UserPreferencesPatch) (mmModel.Preferences, error) { - preferences, err := s.getUserPreferences(db, userID) - if err != nil { - return nil, err - } - - if len(patch.UpdatedFields) > 0 { - for key, value := range patch.UpdatedFields { - preference := mmModel.Preference{ - UserId: userID, - Category: model.PreferencesCategoryFocalboard, - Name: key, - Value: value, - } - - if err := s.updateUserPreference(db, preference); err != nil { - return nil, err - } - - newPreferences := mmModel.Preferences{} - for _, existingPreference := range preferences { - if preference.Name != existingPreference.Name { - newPreferences = append(newPreferences, existingPreference) - } - } - newPreferences = append(newPreferences, preference) - preferences = newPreferences - } - } - - if len(patch.DeletedFields) > 0 { - for _, key := range patch.DeletedFields { - preference := mmModel.Preference{ - UserId: userID, - Category: model.PreferencesCategoryFocalboard, - Name: key, - } - - if err := s.deleteUserPreference(db, preference); err != nil { - return nil, err - } - - newPreferences := mmModel.Preferences{} - for _, existingPreference := range preferences { - if preference.Name != existingPreference.Name { - newPreferences = append(newPreferences, existingPreference) - } - } - preferences = newPreferences - } - } - - return preferences, nil -} - -func (s *SQLStore) updateUserPreference(db sq.BaseRunner, preference mmModel.Preference) error { - query := s.getQueryBuilder(db). - Insert(s.tablePrefix+"preferences"). - Columns("UserId", "Category", "Name", "Value"). - Values(preference.UserId, preference.Category, preference.Name, preference.Value) - - switch s.dbType { - case model.MysqlDBType: - query = query.SuffixExpr(sq.Expr("ON DUPLICATE KEY UPDATE Value = ?", preference.Value)) - case model.PostgresDBType: - query = query.SuffixExpr(sq.Expr("ON CONFLICT (userid, category, name) DO UPDATE SET Value = ?", preference.Value)) - case model.SqliteDBType: - query = query.SuffixExpr(sq.Expr(" on conflict(userid, category, name) do update set value = excluded.value")) - default: - return store.NewErrNotImplemented("failed to update preference because of missing driver") - } - - if _, err := query.Exec(); err != nil { - return fmt.Errorf("failed to upsert user preference in database: userID: %s name: %s value: %s error: %w", preference.UserId, preference.Name, preference.Value, err) - } - - return nil -} - -func (s *SQLStore) deleteUserPreference(db sq.BaseRunner, preference mmModel.Preference) error { - query := s.getQueryBuilder(db). - Delete(s.tablePrefix + "preferences"). - Where(sq.Eq{"UserId": preference.UserId}). - Where(sq.Eq{"Category": preference.Category}). - Where(sq.Eq{"Name": preference.Name}) - - if _, err := query.Exec(); err != nil { - return fmt.Errorf("failed to delete user preference from database: %w", err) - } - - return nil -} - -func (s *SQLStore) canSeeUser(db sq.BaseRunner, seerID string, seenID string) (bool, error) { - return true, nil -} - -func (s *SQLStore) sendMessage(db sq.BaseRunner, message, postType string, receipts []string) error { - return errUnsupportedOperation -} - -func (s *SQLStore) postMessage(db sq.BaseRunner, message, postType string, channel string) error { - return errUnsupportedOperation -} - -func (s *SQLStore) getUserTimezone(_ sq.BaseRunner, _ string) (string, error) { - return "", errUnsupportedOperation -} - -func (s *SQLStore) getUserPreferences(db sq.BaseRunner, userID string) (mmModel.Preferences, error) { - query := s.getQueryBuilder(db). - Select("userid", "category", "name", "value"). - From(s.tablePrefix + "preferences"). - Where(sq.Eq{ - "userid": userID, - "category": model.PreferencesCategoryFocalboard, - }) - - rows, err := query.Query() - if err != nil { - s.logger.Error("failed to fetch user preferences", mlog.String("user_id", userID), mlog.Err(err)) - return nil, err - } - - defer rows.Close() - - preferences, err := s.preferencesFromRows(rows) - if err != nil { - return nil, err - } - - return preferences, nil -} - -func (s *SQLStore) preferencesFromRows(rows *sql.Rows) ([]mmModel.Preference, error) { - preferences := []mmModel.Preference{} - - for rows.Next() { - var preference mmModel.Preference - - err := rows.Scan( - &preference.UserId, - &preference.Category, - &preference.Name, - &preference.Value, - ) - - if err != nil { - s.logger.Error("failed to scan row for user preference", mlog.Err(err)) - return nil, err - } - - preferences = append(preferences, preference) - } - - return preferences, nil -} diff --git a/server/ws/server.go b/server/ws/server.go index b39f1585..dd249b91 100644 --- a/server/ws/server.go +++ b/server/ws/server.go @@ -67,7 +67,7 @@ func (wss *websocketSession) isAuthenticated() bool { } // NewServer creates a new Server. -func NewServer(auth *auth.Auth, singleUserToken string, isMattermostAuth bool, logger mlog.LoggerIFace, store Store) *Server { +func NewServer(auth *auth.Auth, logger mlog.LoggerIFace, store Store) *Server { return &Server{ listeners: make(map[*websocketSession]bool), listenersByTeam: make(map[string][]*websocketSession), @@ -78,7 +78,7 @@ func NewServer(auth *auth.Auth, singleUserToken string, isMattermostAuth bool, l }, }, auth: auth, - isMattermostAuth: isMattermostAuth, + isMattermostAuth: true, logger: logger, store: store, } diff --git a/server/ws/server_test.go b/server/ws/server_test.go index b345d079..585045b9 100644 --- a/server/ws/server_test.go +++ b/server/ws/server_test.go @@ -13,7 +13,7 @@ import ( ) func TestTeamSubscription(t *testing.T) { - server := NewServer(&auth.Auth{}, "token", false, &mlog.Logger{}, nil) + server := NewServer(&auth.Auth{}, &mlog.Logger{}, nil) session := &websocketSession{ conn: &websocket.Conn{}, mu: sync.Mutex{}, @@ -145,7 +145,7 @@ func TestTeamSubscription(t *testing.T) { } func TestBlocksSubscription(t *testing.T) { - server := NewServer(&auth.Auth{}, "token", false, &mlog.Logger{}, nil) + server := NewServer(&auth.Auth{}, &mlog.Logger{}, nil) session := &websocketSession{ conn: &websocket.Conn{}, mu: sync.Mutex{}, @@ -264,7 +264,7 @@ func TestBlocksSubscription(t *testing.T) { } func TestGetUserIDForTokenInSingleUserMode(t *testing.T) { - server := NewServer(&auth.Auth{}, "token", false, &mlog.Logger{}, nil) + server := NewServer(&auth.Auth{}, &mlog.Logger{}, nil) t.Run("Should return nothing if the token is empty", func(t *testing.T) { require.Empty(t, server.getUserIDForToken("")) From 11320549654db9c109245d38c2a133df000da6e8 Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Fri, 5 Jul 2024 11:27:30 +0530 Subject: [PATCH 3/7] refactor: removed not required function from store level --- .../mattermostauthlayer.go | 24 -- server/services/store/mockstore/mockstore.go | 14 - .../services/store/sqlstore/public_methods.go | 30 -- .../services/store/sqlstore/sqlstore_test.go | 1 - server/services/store/store.go | 6 - server/services/store/storetests/session.go | 18 +- .../store/storetests/subscriptions.go | 325 ------------------ server/services/store/storetests/users.go | 268 --------------- server/services/store/storetests/util.go | 17 - 9 files changed, 1 insertion(+), 702 deletions(-) delete mode 100644 server/services/store/storetests/subscriptions.go diff --git a/server/services/store/mattermostauthlayer/mattermostauthlayer.go b/server/services/store/mattermostauthlayer/mattermostauthlayer.go index bc324874..92258e48 100644 --- a/server/services/store/mattermostauthlayer/mattermostauthlayer.go +++ b/server/services/store/mattermostauthlayer/mattermostauthlayer.go @@ -115,22 +115,6 @@ func (s *MattermostAuthLayer) GetUserByUsername(username string) (*model.User, e return &user, nil } -func (s *MattermostAuthLayer) CreateUser(user *model.User) (*model.User, error) { - return nil, store.NewNotSupportedError("no user creation allowed from focalboard, create it using mattermost") -} - -func (s *MattermostAuthLayer) UpdateUser(user *model.User) (*model.User, error) { - return nil, store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - -func (s *MattermostAuthLayer) UpdateUserPassword(username, password string) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - -func (s *MattermostAuthLayer) UpdateUserPasswordByID(userID, password string) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - func (s *MattermostAuthLayer) PatchUserPreferences(userID string, patch model.UserPreferencesPatch) (mmModel.Preferences, error) { preferences, err := s.GetUserPreferences(userID) if err != nil { @@ -240,10 +224,6 @@ func (s *MattermostAuthLayer) GetSession(token string, expireTime int64) (*model return nil, store.NewNotSupportedError("sessions not used when using mattermost") } -func (s *MattermostAuthLayer) CreateSession(session *model.Session) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - func (s *MattermostAuthLayer) RefreshSession(session *model.Session) error { return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") } @@ -256,10 +236,6 @@ func (s *MattermostAuthLayer) DeleteSession(sessionID string) error { return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") } -func (s *MattermostAuthLayer) CleanUpSessions(expireTime int64) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - func (s *MattermostAuthLayer) GetTeam(id string) (*model.Team, error) { if id == "0" { team := model.Team{ diff --git a/server/services/store/mockstore/mockstore.go b/server/services/store/mockstore/mockstore.go index 973dae3f..98639595 100644 --- a/server/services/store/mockstore/mockstore.go +++ b/server/services/store/mockstore/mockstore.go @@ -65,20 +65,6 @@ func (mr *MockStoreMockRecorder) CanSeeUser(arg0, arg1 interface{}) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CanSeeUser", reflect.TypeOf((*MockStore)(nil).CanSeeUser), arg0, arg1) } -// CleanUpSessions mocks base method. -func (m *MockStore) CleanUpSessions(arg0 int64) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CleanUpSessions", arg0) - ret0, _ := ret[0].(error) - return ret0 -} - -// CleanUpSessions indicates an expected call of CleanUpSessions. -func (mr *MockStoreMockRecorder) CleanUpSessions(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CleanUpSessions", reflect.TypeOf((*MockStore)(nil).CleanUpSessions), arg0) -} - // CreateBoardsAndBlocks mocks base method. func (m *MockStore) CreateBoardsAndBlocks(arg0 *model.BoardsAndBlocks, arg1 string) (*model.BoardsAndBlocks, error) { m.ctrl.T.Helper() diff --git a/server/services/store/sqlstore/public_methods.go b/server/services/store/sqlstore/public_methods.go index 98229f18..42f889ae 100644 --- a/server/services/store/sqlstore/public_methods.go +++ b/server/services/store/sqlstore/public_methods.go @@ -52,11 +52,6 @@ func (s *SQLStore) CanSeeUser(seerID string, seenID string) (bool, error) { } -func (s *SQLStore) CleanUpSessions(expireTime int64) error { - return errors.New("no clean up sessions allowed from focalboard, update it using mattermost") - -} - func (s *SQLStore) CreateBoardsAndBlocks(bab *model.BoardsAndBlocks, userID string) (*model.BoardsAndBlocks, error) { if s.dbType == model.SqliteDBType { return s.createBoardsAndBlocks(s.db, bab, userID) @@ -129,21 +124,11 @@ func (s *SQLStore) CreateCategory(category model.Category) error { } -func (s *SQLStore) CreateSession(session *model.Session) error { - return errors.New("no update allowed from focalboard, update it using mattermost") - -} - func (s *SQLStore) CreateSubscription(sub *model.Subscription) (*model.Subscription, error) { return s.createSubscription(s.db, sub) } -func (s *SQLStore) CreateUser(user *model.User) (*model.User, error) { - return nil, errors.New("no user creation allowed from focalboard, create it using mattermost") - -} - func (s *SQLStore) DeleteBlock(blockID string, modifiedBy string) error { if s.dbType == model.SqliteDBType { return s.deleteBlock(s.db, blockID, modifiedBy) @@ -929,21 +914,6 @@ func (s *SQLStore) UpdateSubscribersNotifiedAt(blockID string, notifiedAt int64) } -func (s *SQLStore) UpdateUser(user *model.User) (*model.User, error) { - return nil, errors.New("no update allowed from focalboard, update it using mattermost") - -} - -func (s *SQLStore) UpdateUserPassword(username string, password string) error { - return errors.New("no update allowed from focalboard, update it using mattermost") - -} - -func (s *SQLStore) UpdateUserPasswordByID(userID string, password string) error { - return errors.New("no update allowed from focalboard, update it using mattermost") - -} - func (s *SQLStore) UpsertNotificationHint(hint *model.NotificationHint, notificationFreq time.Duration) (*model.NotificationHint, error) { return s.upsertNotificationHint(s.db, hint, notificationFreq) diff --git a/server/services/store/sqlstore/sqlstore_test.go b/server/services/store/sqlstore/sqlstore_test.go index cdc3f495..06a81d4f 100644 --- a/server/services/store/sqlstore/sqlstore_test.go +++ b/server/services/store/sqlstore/sqlstore_test.go @@ -21,7 +21,6 @@ func TestSQLStore(t *testing.T) { t.Run("TeamStore", func(t *testing.T) { storetests.StoreTestTeamStore(t, SetupTests) }) t.Run("BoardStore", func(t *testing.T) { storetests.StoreTestBoardStore(t, SetupTests) }) t.Run("BoardsAndBlocksStore", func(t *testing.T) { storetests.StoreTestBoardsAndBlocksStore(t, SetupTests) }) - t.Run("SubscriptionStore", func(t *testing.T) { storetests.StoreTestSubscriptionsStore(t, SetupTests) }) t.Run("NotificationHintStore", func(t *testing.T) { storetests.StoreTestNotificationHintsStore(t, SetupTests) }) t.Run("DataRetention", func(t *testing.T) { storetests.StoreTestDataRetention(t, SetupTests) }) t.Run("CloudStore", func(t *testing.T) { storetests.StoreTestCloudStore(t, SetupTests) }) diff --git a/server/services/store/store.go b/server/services/store/store.go index 5109ea83..342e8845 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -60,10 +60,6 @@ type Store interface { GetUsersList(userIDs []string, showEmail, showName bool) ([]*model.User, error) GetUserByEmail(email string) (*model.User, error) GetUserByUsername(username string) (*model.User, error) - CreateUser(user *model.User) (*model.User, error) - UpdateUser(user *model.User) (*model.User, error) - UpdateUserPassword(username, password string) error - UpdateUserPasswordByID(userID, password string) error GetUsersByTeam(teamID string, asGuestID string, showEmail, showName bool) ([]*model.User, error) SearchUsersByTeam(teamID string, searchQuery string, asGuestID string, excludeBots bool, showEmail, showName bool) ([]*model.User, error) PatchUserPreferences(userID string, patch model.UserPreferencesPatch) (mmModel.Preferences, error) @@ -71,11 +67,9 @@ type Store interface { GetActiveUserCount(updatedSecondsAgo int64) (int, error) GetSession(token string, expireTime int64) (*model.Session, error) - CreateSession(session *model.Session) error RefreshSession(session *model.Session) error UpdateSession(session *model.Session) error DeleteSession(sessionID string) error - CleanUpSessions(expireTime int64) error UpsertSharing(sharing model.Sharing) error GetSharing(rootID string) (*model.Sharing, error) diff --git a/server/services/store/storetests/session.go b/server/services/store/storetests/session.go index 54dc5987..9103c871 100644 --- a/server/services/store/storetests/session.go +++ b/server/services/store/storetests/session.go @@ -1,7 +1,6 @@ package storetests import ( - "fmt" "testing" "time" @@ -37,9 +36,6 @@ func testCreateAndGetAndDeleteSession(t *testing.T, store store.Store) { } t.Run("CreateAndGetSession", func(t *testing.T) { - err := store.CreateSession(session) - require.NoError(t, err) - got, err := store.GetSession(session.Token, 60*60) require.NoError(t, err) require.Equal(t, session, got) @@ -70,15 +66,6 @@ func testGetActiveUserCount(t *testing.T, store store.Store) { t.Run("active user", func(t *testing.T) { // gen random count active user session count := int(time.Now().Unix() % 10) - for i := 0; i < count; i++ { - session := &model.Session{ - ID: fmt.Sprintf("id-%d", i), - UserID: fmt.Sprintf("user-id-%d", i), - Token: fmt.Sprintf("token-%d", i), - } - err := store.CreateSession(session) - require.NoError(t, err) - } got, err := store.GetActiveUserCount(60) require.NoError(t, err) @@ -93,12 +80,9 @@ func testUpdateSession(t *testing.T, store store.Store) { Props: map[string]interface{}{"field1": "A"}, } - err := store.CreateSession(session) - require.NoError(t, err) - // update session session.Props["field1"] = "B" - err = store.UpdateSession(session) + err := store.UpdateSession(session) require.NoError(t, err) got, err := store.GetSession(session.Token, 60) diff --git a/server/services/store/storetests/subscriptions.go b/server/services/store/storetests/subscriptions.go deleted file mode 100644 index e4a21324..00000000 --- a/server/services/store/storetests/subscriptions.go +++ /dev/null @@ -1,325 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. - -package storetests - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/mattermost/mattermost-plugin-boards/server/model" - "github.com/mattermost/mattermost-plugin-boards/server/services/store" -) - -//nolint:dupl -func StoreTestSubscriptionsStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) { - t.Run("CreateSubscription", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testCreateSubscription(t, store) - }) - - t.Run("DeleteSubscription", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testDeleteSubscription(t, store) - }) - - t.Run("UndeleteSubscription", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testUndeleteSubscription(t, store) - }) - - t.Run("GetSubscription", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testGetSubscription(t, store) - }) - - t.Run("GetSubscriptions", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testGetSubscriptions(t, store) - }) - - t.Run("GetSubscribersForBlock", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testGetSubscribersForBlock(t, store) - }) -} - -func testCreateSubscription(t *testing.T, store store.Store) { - t.Run("create subscriptions", func(t *testing.T) { - users := createTestUsers(t, store, 10) - blocks := createTestBlocks(t, store, users[0].ID, 50) - - for i, user := range users { - for j := 0; j < i; j++ { - sub := &model.Subscription{ - BlockType: blocks[j].Type, - BlockID: blocks[j].ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - subNew, err := store.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - - assert.NotZero(t, subNew.NotifiedAt) - assert.NotZero(t, subNew.CreateAt) - assert.Zero(t, subNew.DeleteAt) - } - } - - // ensure each user has the right number of subscriptions - for i, user := range users { - subs, err := store.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Len(t, subs, i) - } - }) - - t.Run("duplicate subscription", func(t *testing.T) { - admin := createTestUsers(t, store, 1)[0] - user := createTestUsers(t, store, 1)[0] - block := createTestBlocks(t, store, admin.ID, 1)[0] - - sub := &model.Subscription{ - BlockType: block.Type, - BlockID: block.ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - subNew, err := store.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - - sub = &model.Subscription{ - BlockType: block.Type, - BlockID: block.ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - - subDup, err := store.CreateSubscription(sub) - require.NoError(t, err, "create duplicate subscription should not error") - - assert.Equal(t, subNew.BlockID, subDup.BlockID) - assert.Equal(t, subNew.SubscriberID, subDup.SubscriberID) - }) - - t.Run("invalid subscription", func(t *testing.T) { - admin := createTestUsers(t, store, 1)[0] - user := createTestUsers(t, store, 1)[0] - block := createTestBlocks(t, store, admin.ID, 1)[0] - - sub := &model.Subscription{} - - _, err := store.CreateSubscription(sub) - assert.ErrorAs(t, err, &model.ErrInvalidSubscription{}, "invalid subscription should error") - - sub.BlockType = block.Type - _, err = store.CreateSubscription(sub) - assert.ErrorAs(t, err, &model.ErrInvalidSubscription{}, "invalid subscription should error") - - sub.BlockID = block.ID - _, err = store.CreateSubscription(sub) - assert.ErrorAs(t, err, &model.ErrInvalidSubscription{}, "invalid subscription should error") - - sub.SubscriberType = "user" - _, err = store.CreateSubscription(sub) - assert.ErrorAs(t, err, &model.ErrInvalidSubscription{}, "invalid subscription should error") - - sub.SubscriberID = user.ID - subNew, err := store.CreateSubscription(sub) - assert.NoError(t, err, "valid subscription should not error") - - assert.NoError(t, subNew.IsValid(), "created subscription should be valid") - }) -} - -func testDeleteSubscription(t *testing.T, s store.Store) { - t.Run("delete subscription", func(t *testing.T) { - user := createTestUsers(t, s, 1)[0] - block := createTestBlocks(t, s, user.ID, 1)[0] - - sub := &model.Subscription{ - BlockType: block.Type, - BlockID: block.ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - subNew, err := s.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - - // check the subscription exists - subs, err := s.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Len(t, subs, 1) - assert.Equal(t, subNew.BlockID, subs[0].BlockID) - assert.Equal(t, subNew.SubscriberID, subs[0].SubscriberID) - - err = s.DeleteSubscription(block.ID, user.ID) - require.NoError(t, err, "delete subscription should not error") - - // check the subscription was deleted - subs, err = s.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Empty(t, subs) - }) - - t.Run("delete non-existent subscription", func(t *testing.T) { - err := s.DeleteSubscription("bogus", "bogus") - require.Error(t, err, "delete non-existent subscription should error") - require.True(t, model.IsErrNotFound(err), "Should be ErrNotFound compatible error") - }) -} - -func testUndeleteSubscription(t *testing.T, s store.Store) { - t.Run("undelete subscription", func(t *testing.T) { - user := createTestUsers(t, s, 1)[0] - block := createTestBlocks(t, s, user.ID, 1)[0] - - sub := &model.Subscription{ - BlockType: block.Type, - BlockID: block.ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - subNew, err := s.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - - // check the subscription exists - subs, err := s.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Len(t, subs, 1) - assert.Equal(t, subNew.BlockID, subs[0].BlockID) - assert.Equal(t, subNew.SubscriberID, subs[0].SubscriberID) - - err = s.DeleteSubscription(block.ID, user.ID) - require.NoError(t, err, "delete subscription should not error") - - // check the subscription was deleted - subs, err = s.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Empty(t, subs) - - // re-create the subscription - subUndeleted, err := s.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - - // check the undeleted subscription exists - subs, err = s.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Len(t, subs, 1) - assert.Equal(t, subUndeleted.BlockID, subs[0].BlockID) - assert.Equal(t, subUndeleted.SubscriberID, subs[0].SubscriberID) - }) -} - -func testGetSubscription(t *testing.T, s store.Store) { - t.Run("get subscription", func(t *testing.T) { - user := createTestUsers(t, s, 1)[0] - block := createTestBlocks(t, s, user.ID, 1)[0] - - sub := &model.Subscription{ - BlockType: block.Type, - BlockID: block.ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - subNew, err := s.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - - // make sure subscription can be fetched - sub, err = s.GetSubscription(block.ID, user.ID) - require.NoError(t, err, "get subscription should not error") - assert.Equal(t, subNew, sub) - }) - - t.Run("get non-existent subscription", func(t *testing.T) { - sub, err := s.GetSubscription("bogus", "bogus") - require.Error(t, err, "get non-existent subscription should error") - require.True(t, model.IsErrNotFound(err), "Should be ErrNotFound compatible error") - require.Nil(t, sub, "get subscription should return nil") - }) -} - -func testGetSubscriptions(t *testing.T, store store.Store) { - t.Run("get subscriptions", func(t *testing.T) { - author := createTestUsers(t, store, 1)[0] - user := createTestUsers(t, store, 1)[0] - blocks := createTestBlocks(t, store, author.ID, 50) - - for _, block := range blocks { - sub := &model.Subscription{ - BlockType: block.Type, - BlockID: block.ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - _, err := store.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - } - - // ensure user has the right number of subscriptions - subs, err := store.GetSubscriptions(user.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Len(t, subs, len(blocks)) - - // ensure author has no subscriptions - subs, err = store.GetSubscriptions(author.ID) - require.NoError(t, err, "get subscriptions should not error") - assert.Empty(t, subs) - }) - - t.Run("get subscriptions for invalid user", func(t *testing.T) { - subs, err := store.GetSubscriptions("bogus") - require.NoError(t, err, "get subscriptions should not error") - assert.Empty(t, subs) - }) -} - -func testGetSubscribersForBlock(t *testing.T, store store.Store) { - t.Run("get subscribers for block", func(t *testing.T) { - users := createTestUsers(t, store, 50) - blocks := createTestBlocks(t, store, users[0].ID, 2) - - for _, user := range users { - sub := &model.Subscription{ - BlockType: blocks[1].Type, - BlockID: blocks[1].ID, - SubscriberType: "user", - SubscriberID: user.ID, - } - _, err := store.CreateSubscription(sub) - require.NoError(t, err, "create subscription should not error") - } - - // make sure block[1] has the right number of users subscribed - subs, err := store.GetSubscribersForBlock(blocks[1].ID) - require.NoError(t, err, "get subscribers for block should not error") - assert.Len(t, subs, 50) - - count, err := store.GetSubscribersCountForBlock(blocks[1].ID) - require.NoError(t, err, "get subscribers for block should not error") - assert.Equal(t, 50, count) - - // make sure block[0] has zero users subscribed - subs, err = store.GetSubscribersForBlock(blocks[0].ID) - require.NoError(t, err, "get subscribers for block should not error") - assert.Empty(t, subs) - - count, err = store.GetSubscribersCountForBlock(blocks[0].ID) - require.NoError(t, err, "get subscribers for block should not error") - assert.Zero(t, count) - }) - - t.Run("get subscribers for invalid block", func(t *testing.T) { - subs, err := store.GetSubscribersForBlock("bogus") - require.NoError(t, err, "get subscribers for block should not error") - assert.Empty(t, subs) - }) -} diff --git a/server/services/store/storetests/users.go b/server/services/store/storetests/users.go index 177517fd..7c327628 100644 --- a/server/services/store/storetests/users.go +++ b/server/services/store/storetests/users.go @@ -4,9 +4,7 @@ package storetests import ( - "fmt" "testing" - "time" "github.com/stretchr/testify/require" @@ -17,72 +15,12 @@ import ( //nolint:dupl func StoreTestUserStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) { - t.Run("GetUsersByTeam", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testGetUsersByTeam(t, store) - }) t.Run("CreateAndGetUser", func(t *testing.T) { store, tearDown := setup(t) defer tearDown() testCreateAndGetUser(t, store) }) - - t.Run("GetUsersList", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testGetUsersList(t, store) - }) - - t.Run("CreateAndUpdateUser", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testCreateAndUpdateUser(t, store) - }) - - t.Run("CreateAndGetRegisteredUserCount", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testCreateAndGetRegisteredUserCount(t, store) - }) - - t.Run("TestPatchUserProps", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testPatchUserProps(t, store) - }) -} - -func testGetUsersByTeam(t *testing.T, store store.Store) { - t.Run("GetTeamUsers", func(t *testing.T) { - users, err := store.GetUsersByTeam("team_1", "", false, false) - require.Equal(t, 0, len(users)) - require.NoError(t, err) - - userID := utils.NewID(utils.IDTypeUser) - - user, err := store.CreateUser(&model.User{ - ID: userID, - Username: "darth.vader", - }) - require.NoError(t, err) - require.NotNil(t, user) - require.Equal(t, userID, user.ID) - require.Equal(t, "darth.vader", user.Username) - - defer func() { - _, _ = store.UpdateUser(&model.User{ - ID: userID, - DeleteAt: utils.GetMillis(), - }) - }() - - users, err = store.GetUsersByTeam("team_1", "", false, false) - require.Equal(t, 1, len(users)) - require.Equal(t, "darth.vader", users[0].Username) - require.NoError(t, err) - }) } func testCreateAndGetUser(t *testing.T, store store.Store) { @@ -92,12 +30,6 @@ func testCreateAndGetUser(t *testing.T, store store.Store) { Email: "mock@email.com", } - t.Run("CreateUser", func(t *testing.T) { - newUser, err := store.CreateUser(user) - require.NoError(t, err) - require.NotNil(t, newUser) - }) - t.Run("GetUserByID", func(t *testing.T) { got, err := store.GetUserByID(user.ID) require.NoError(t, err) @@ -143,203 +75,3 @@ func testCreateAndGetUser(t *testing.T, store store.Store) { require.Nil(t, got) }) } - -func testGetUsersList(t *testing.T, store store.Store) { - for _, id := range []string{"user1", "user2"} { - user := &model.User{ - ID: id, - Username: fmt.Sprintf("%s-username", id), - Email: fmt.Sprintf("%s@sample.com", id), - } - newUser, err := store.CreateUser(user) - require.NoError(t, err) - require.NotNil(t, newUser) - } - - testCases := []struct { - Name string - UserIDs []string - ExpectedError bool - ExpectedIDs []string - }{ - { - Name: "all of the IDs are found", - UserIDs: []string{"user1", "user2"}, - ExpectedError: false, - ExpectedIDs: []string{"user1", "user2"}, - }, - { - Name: "some of the IDs are found", - UserIDs: []string{"user2", "non-existent"}, - ExpectedError: true, - ExpectedIDs: []string{"user2"}, - }, - { - Name: "none of the IDs are found", - UserIDs: []string{"non-existent-1", "non-existent-2"}, - ExpectedError: true, - ExpectedIDs: []string{}, - }, - } - - for _, tc := range testCases { - t.Run(tc.Name, func(t *testing.T) { - users, err := store.GetUsersList(tc.UserIDs, false, false) - if tc.ExpectedError { - require.Error(t, err) - require.True(t, model.IsErrNotFound(err)) - } else { - require.NoError(t, err) - } - - userIDs := []string{} - for _, user := range users { - userIDs = append(userIDs, user.ID) - } - require.ElementsMatch(t, tc.ExpectedIDs, userIDs) - }) - } -} - -func testCreateAndUpdateUser(t *testing.T, store store.Store) { - user := &model.User{ - ID: utils.NewID(utils.IDTypeUser), - } - newUser, err := store.CreateUser(user) - require.NoError(t, err) - require.NotNil(t, newUser) - - t.Run("UpdateUser", func(t *testing.T) { - user.Username = "damao" - user.Email = "mock@email.com" - uUser, err := store.UpdateUser(user) - require.NoError(t, err) - require.NotNil(t, uUser) - require.Equal(t, user.Username, uUser.Username) - require.Equal(t, user.Email, uUser.Email) - - got, err := store.GetUserByID(user.ID) - require.NoError(t, err) - require.Equal(t, user.ID, got.ID) - require.Equal(t, user.Username, got.Username) - require.Equal(t, user.Email, got.Email) - }) - - t.Run("UpdateUserPassword", func(t *testing.T) { - newPassword := utils.NewID(utils.IDTypeNone) - err := store.UpdateUserPassword(user.Username, newPassword) - require.NoError(t, err) - - got, err := store.GetUserByUsername(user.Username) - require.NoError(t, err) - require.Equal(t, user.Username, got.Username) - require.Equal(t, newPassword, got.Password) - }) - - t.Run("UpdateUserPasswordByID", func(t *testing.T) { - newPassword := utils.NewID(utils.IDTypeNone) - err := store.UpdateUserPasswordByID(user.ID, newPassword) - require.NoError(t, err) - - got, err := store.GetUserByID(user.ID) - require.NoError(t, err) - require.Equal(t, user.ID, got.ID) - require.Equal(t, newPassword, got.Password) - }) -} - -func testCreateAndGetRegisteredUserCount(t *testing.T, store store.Store) { - randomN := int(time.Now().Unix() % 10) - for i := 0; i < randomN; i++ { - user, err := store.CreateUser(&model.User{ - ID: utils.NewID(utils.IDTypeUser), - }) - require.NoError(t, err) - require.NotNil(t, user) - } - - got, err := store.GetRegisteredUserCount() - require.NoError(t, err) - require.Equal(t, randomN, got) -} - -func testPatchUserProps(t *testing.T, store store.Store) { - user := &model.User{ - ID: utils.NewID(utils.IDTypeUser), - } - newUser, err := store.CreateUser(user) - require.NoError(t, err) - require.NotNil(t, newUser) - - key1 := "new_key_1" - key2 := "new_key_2" - key3 := "new_key_3" - - // Only update props - patch := model.UserPreferencesPatch{ - UpdatedFields: map[string]string{ - key1: "new_value_1", - key2: "new_value_2", - key3: "new_value_3", - }, - } - - userPreferences, err := store.PatchUserPreferences(user.ID, patch) - require.NoError(t, err) - require.Equal(t, 3, len(userPreferences)) - - for _, preference := range userPreferences { - switch preference.Name { - case key1: - require.Equal(t, "new_value_1", preference.Value) - case key2: - require.Equal(t, "new_value_2", preference.Value) - case key3: - require.Equal(t, "new_value_3", preference.Value) - } - } - - // Delete a prop - patch = model.UserPreferencesPatch{ - DeletedFields: []string{ - key1, - }, - } - - userPreferences, err = store.PatchUserPreferences(user.ID, patch) - require.NoError(t, err) - - for _, preference := range userPreferences { - switch preference.Name { - case key1: - t.Errorf("new_key_1 shouldn't exist in user preference as we just deleted it") - case key2: - require.Equal(t, "new_value_2", preference.Value) - case key3: - require.Equal(t, "new_value_3", preference.Value) - } - } - - // update and delete together - patch = model.UserPreferencesPatch{ - UpdatedFields: map[string]string{ - key3: "new_value_3_new_again", - }, - DeletedFields: []string{ - key2, - }, - } - userPreferences, err = store.PatchUserPreferences(user.ID, patch) - require.NoError(t, err) - - for _, preference := range userPreferences { - switch preference.Name { - case key1: - t.Errorf("new_key_1 shouldn't exist in user preference as we just deleted it") - case key2: - t.Errorf("new_key_2 shouldn't exist in user preference as we just deleted it") - case key3: - require.Equal(t, "new_value_3_new_again", preference.Value) - } - } -} diff --git a/server/services/store/storetests/util.go b/server/services/store/storetests/util.go index 7959485d..9c169888 100644 --- a/server/services/store/storetests/util.go +++ b/server/services/store/storetests/util.go @@ -15,23 +15,6 @@ import ( "github.com/stretchr/testify/require" ) -func createTestUsers(t *testing.T, store store.Store, num int) []*model.User { - var users []*model.User - for i := 0; i < num; i++ { - user := &model.User{ - ID: utils.NewID(utils.IDTypeUser), - Username: fmt.Sprintf("mooncake.%d", i), - Email: fmt.Sprintf("mooncake.%d@example.com", i), - } - newUser, err := store.CreateUser(user) - require.NoError(t, err) - require.NotNil(t, newUser) - - users = append(users, user) - } - return users -} - func createTestBlocks(t *testing.T, store store.Store, userID string, num int) []*model.Block { var blocks []*model.Block for i := 0; i < num; i++ { From 713cf80e74fb497c21ad7b9ea5cb14fe4d9a408e Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Fri, 5 Jul 2024 12:04:53 +0530 Subject: [PATCH 4/7] refactor: removed session check for plugin --- server/api/auth.go | 35 +----------- server/api/blocks.go | 2 +- server/api/boards.go | 2 +- server/api/files.go | 4 +- server/app/auth.go | 5 -- server/auth/auth.go | 18 ------ server/auth/auth_test.go | 28 --------- .../mattermostauthlayer.go | 16 ------ .../services/store/sqlstore/public_methods.go | 20 ------- server/services/store/store.go | 4 -- server/services/store/storetests/session.go | 57 ------------------- server/ws/server.go | 7 +-- 12 files changed, 8 insertions(+), 190 deletions(-) diff --git a/server/api/auth.go b/server/api/auth.go index 837ce3b3..1b873a1c 100644 --- a/server/api/auth.go +++ b/server/api/auth.go @@ -5,20 +5,15 @@ import ( "net/http" "github.com/mattermost/mattermost-plugin-boards/server/model" - "github.com/mattermost/mattermost-plugin-boards/server/services/auth" "github.com/mattermost/mattermost-plugin-boards/server/utils" - - "github.com/mattermost/mattermost/server/public/shared/mlog" ) func (a *API) sessionRequired(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { - return a.attachSession(handler, true) + return a.attachSession(handler) } -func (a *API) attachSession(handler func(w http.ResponseWriter, r *http.Request), required bool) func(w http.ResponseWriter, r *http.Request) { +func (a *API) attachSession(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - token, _ := auth.ParseAuthTokenFromRequest(r) - if a.MattermostAuth && r.Header.Get("Mattermost-User-Id") != "" { userID := r.Header.Get("Mattermost-User-Id") now := utils.GetMillis() @@ -37,30 +32,6 @@ func (a *API) attachSession(handler func(w http.ResponseWriter, r *http.Request) return } - session, err := a.app.GetSession(token) - if err != nil { - if required { - a.errorResponse(w, r, model.NewErrUnauthorized(err.Error())) - return - } - - handler(w, r) - return - } - - authService := session.AuthService - if authService != a.authService { - msg := `Session authService mismatch` - a.logger.Error(msg, - mlog.String("sessionID", session.ID), - mlog.String("want", a.authService), - mlog.String("got", authService), - ) - a.errorResponse(w, r, model.NewErrUnauthorized(msg)) - return - } - - ctx := context.WithValue(r.Context(), sessionContextKey, session) - handler(w, r.WithContext(ctx)) + a.errorResponse(w, r, model.NewErrUnauthorized("Unauthorized")) } } diff --git a/server/api/blocks.go b/server/api/blocks.go index 93db90ec..8facc3af 100644 --- a/server/api/blocks.go +++ b/server/api/blocks.go @@ -16,7 +16,7 @@ import ( func (a *API) registerBlocksRoutes(r *mux.Router) { // Blocks APIs - r.HandleFunc("/boards/{boardID}/blocks", a.attachSession(a.handleGetBlocks, false)).Methods("GET") + r.HandleFunc("/boards/{boardID}/blocks", a.attachSession(a.handleGetBlocks)).Methods("GET") r.HandleFunc("/boards/{boardID}/blocks", a.sessionRequired(a.handlePostBlocks)).Methods("POST") r.HandleFunc("/boards/{boardID}/blocks", a.sessionRequired(a.handlePatchBlocks)).Methods("PATCH") r.HandleFunc("/boards/{boardID}/blocks/{blockID}", a.sessionRequired(a.handleDeleteBlock)).Methods("DELETE") diff --git a/server/api/boards.go b/server/api/boards.go index 8548f123..0f1646f5 100644 --- a/server/api/boards.go +++ b/server/api/boards.go @@ -15,7 +15,7 @@ import ( func (a *API) registerBoardsRoutes(r *mux.Router) { r.HandleFunc("/teams/{teamID}/boards", a.sessionRequired(a.handleGetBoards)).Methods("GET") r.HandleFunc("/boards", a.sessionRequired(a.handleCreateBoard)).Methods("POST") - r.HandleFunc("/boards/{boardID}", a.attachSession(a.handleGetBoard, false)).Methods("GET") + r.HandleFunc("/boards/{boardID}", a.attachSession(a.handleGetBoard)).Methods("GET") r.HandleFunc("/boards/{boardID}", a.sessionRequired(a.handlePatchBoard)).Methods("PATCH") r.HandleFunc("/boards/{boardID}", a.sessionRequired(a.handleDeleteBoard)).Methods("DELETE") r.HandleFunc("/boards/{boardID}/duplicate", a.sessionRequired(a.handleDuplicateBoard)).Methods("POST") diff --git a/server/api/files.go b/server/api/files.go index f270916d..58c0161d 100644 --- a/server/api/files.go +++ b/server/api/files.go @@ -75,8 +75,8 @@ func FileInfoResponseFromJSON(data io.Reader) (*mmModel.FileInfo, error) { func (a *API) registerFilesRoutes(r *mux.Router) { // Files API - r.HandleFunc("/files/teams/{teamID}/{boardID}/{filename}", a.attachSession(a.handleServeFile, false)).Methods("GET") - r.HandleFunc("/files/teams/{teamID}/{boardID}/{filename}/info", a.attachSession(a.getFileInfo, false)).Methods("GET") + r.HandleFunc("/files/teams/{teamID}/{boardID}/{filename}", a.attachSession(a.handleServeFile)).Methods("GET") + r.HandleFunc("/files/teams/{teamID}/{boardID}/{filename}/info", a.attachSession(a.getFileInfo)).Methods("GET") r.HandleFunc("/teams/{teamID}/{boardID}/files", a.sessionRequired(a.handleUploadFile)).Methods("POST") } diff --git a/server/app/auth.go b/server/app/auth.go index dcd48107..d484a987 100644 --- a/server/app/auth.go +++ b/server/app/auth.go @@ -14,11 +14,6 @@ const ( SecondsPerMinute = 60 ) -// GetSession Get a user active session and refresh the session if is needed. -func (a *App) GetSession(token string) (*model.Session, error) { - return a.auth.GetSession(token) -} - // IsValidReadToken validates the read token for a block. func (a *App) IsValidReadToken(boardID string, readToken string) (bool, error) { return a.auth.IsValidReadToken(boardID, readToken) diff --git a/server/auth/auth.go b/server/auth/auth.go index 5474b9ef..267d060e 100644 --- a/server/auth/auth.go +++ b/server/auth/auth.go @@ -6,12 +6,10 @@ import ( "github.com/mattermost/mattermost-plugin-boards/server/services/config" "github.com/mattermost/mattermost-plugin-boards/server/services/permissions" "github.com/mattermost/mattermost-plugin-boards/server/services/store" - "github.com/mattermost/mattermost-plugin-boards/server/utils" "github.com/pkg/errors" ) type AuthInterface interface { - GetSession(token string) (*model.Session, error) IsValidReadToken(boardID string, readToken string) (bool, error) DoesUserHaveTeamAccess(userID string, teamID string) bool } @@ -28,22 +26,6 @@ func New(config *config.Configuration, store store.Store, permissions permission return &Auth{config: config, store: store, permissions: permissions} } -// GetSession Get a user active session and refresh the session if needed. -func (a *Auth) GetSession(token string) (*model.Session, error) { - if len(token) < 1 { - return nil, errors.New("no session token") - } - - session, err := a.store.GetSession(token, a.config.SessionExpireTime) - if err != nil { - return nil, errors.Wrap(err, "unable to get the session for the token") - } - if session.UpdateAt < (utils.GetMillis() - utils.SecondsToMillis(a.config.SessionRefreshTime)) { - _ = a.store.RefreshSession(session) - } - return session, nil -} - // IsValidReadToken validates the read token for a board. func (a *Auth) IsValidReadToken(boardID string, readToken string) (bool, error) { sharing, err := a.store.GetSharing(boardID) diff --git a/server/auth/auth_test.go b/server/auth/auth_test.go index eabded6b..489a32fc 100644 --- a/server/auth/auth_test.go +++ b/server/auth/auth_test.go @@ -54,37 +54,9 @@ func setupTestHelper(t *testing.T) *TestHelper { func TestGetSession(t *testing.T) { th := setupTestHelper(t) - - testcases := []struct { - title string - token string - refreshTime int64 - isError bool - }{ - {"fail, no token", "", 0, true}, - {"fail, invalid username", "badToken", 0, true}, - {"success, good token", "goodToken", 1000, false}, - } - th.Store.EXPECT().GetSession("badToken", gomock.Any()).Return(nil, errors.New("Invalid Token")) th.Store.EXPECT().GetSession("goodToken", gomock.Any()).Return(mockSession, nil) th.Store.EXPECT().RefreshSession(gomock.Any()).Return(nil) - - for _, test := range testcases { - t.Run(test.title, func(t *testing.T) { - if test.refreshTime > 0 { - th.Auth.config.SessionRefreshTime = test.refreshTime - } - - session, err := th.Auth.GetSession(test.token) - if test.isError { - require.Error(t, err) - } else { - require.NoError(t, err) - require.NotNil(t, session) - } - }) - } } func TestIsValidReadToken(t *testing.T) { diff --git a/server/services/store/mattermostauthlayer/mattermostauthlayer.go b/server/services/store/mattermostauthlayer/mattermostauthlayer.go index 92258e48..2b2c48a8 100644 --- a/server/services/store/mattermostauthlayer/mattermostauthlayer.go +++ b/server/services/store/mattermostauthlayer/mattermostauthlayer.go @@ -220,22 +220,6 @@ func (s *MattermostAuthLayer) GetActiveUserCount(updatedSecondsAgo int64) (int, return count, nil } -func (s *MattermostAuthLayer) GetSession(token string, expireTime int64) (*model.Session, error) { - return nil, store.NewNotSupportedError("sessions not used when using mattermost") -} - -func (s *MattermostAuthLayer) RefreshSession(session *model.Session) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - -func (s *MattermostAuthLayer) UpdateSession(session *model.Session) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - -func (s *MattermostAuthLayer) DeleteSession(sessionID string) error { - return store.NewNotSupportedError("no update allowed from focalboard, update it using mattermost") -} - func (s *MattermostAuthLayer) GetTeam(id string) (*model.Team, error) { if id == "0" { team := model.Team{ diff --git a/server/services/store/sqlstore/public_methods.go b/server/services/store/sqlstore/public_methods.go index 42f889ae..1f4d5214 100644 --- a/server/services/store/sqlstore/public_methods.go +++ b/server/services/store/sqlstore/public_methods.go @@ -226,11 +226,6 @@ func (s *SQLStore) DeleteNotificationHint(blockID string) error { } -func (s *SQLStore) DeleteSession(sessionID string) error { - return errors.New("no delete session allowed from focalboard, update it using mattermost") - -} - func (s *SQLStore) DeleteSubscription(blockID string, subscriberID string) error { return s.deleteSubscription(s.db, blockID, subscriberID) @@ -459,11 +454,6 @@ func (s *SQLStore) GetRegisteredUserCount() (int, error) { } -func (s *SQLStore) GetSession(token string, expireTime int64) (*model.Session, error) { - return nil, errors.New("sessions not used when using mattermost") - -} - func (s *SQLStore) GetSharing(rootID string) (*model.Sharing, error) { return s.getSharing(s.db, rootID) @@ -757,11 +747,6 @@ func (s *SQLStore) PostMessage(message string, postType string, channelID string } -func (s *SQLStore) RefreshSession(session *model.Session) error { - return errors.New("no update allowed from focalboard, update it using mattermost") - -} - func (s *SQLStore) RemoveDefaultTemplates(boards []*model.Board) error { return s.removeDefaultTemplates(s.db, boards) @@ -904,11 +889,6 @@ func (s *SQLStore) UpdateCategory(category model.Category) error { } -func (s *SQLStore) UpdateSession(session *model.Session) error { - return errors.New("no update allowed from focalboard, update it using mattermost") - -} - func (s *SQLStore) UpdateSubscribersNotifiedAt(blockID string, notifiedAt int64) error { return s.updateSubscribersNotifiedAt(s.db, blockID, notifiedAt) diff --git a/server/services/store/store.go b/server/services/store/store.go index 342e8845..a5174183 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -66,10 +66,6 @@ type Store interface { GetUserPreferences(userID string) (mmModel.Preferences, error) GetActiveUserCount(updatedSecondsAgo int64) (int, error) - GetSession(token string, expireTime int64) (*model.Session, error) - RefreshSession(session *model.Session) error - UpdateSession(session *model.Session) error - DeleteSession(sessionID string) error UpsertSharing(sharing model.Sharing) error GetSharing(rootID string) (*model.Sharing, error) diff --git a/server/services/store/storetests/session.go b/server/services/store/storetests/session.go index 9103c871..c69a1b7d 100644 --- a/server/services/store/storetests/session.go +++ b/server/services/store/storetests/session.go @@ -4,56 +4,16 @@ import ( "testing" "time" - "github.com/mattermost/mattermost-plugin-boards/server/model" "github.com/mattermost/mattermost-plugin-boards/server/services/store" "github.com/stretchr/testify/require" ) func StoreTestSessionStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) { - t.Run("CreateAndGetAndDeleteSession", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testCreateAndGetAndDeleteSession(t, store) - }) - t.Run("GetActiveUserCount", func(t *testing.T) { store, tearDown := setup(t) defer tearDown() testGetActiveUserCount(t, store) }) - - t.Run("UpdateSession", func(t *testing.T) { - store, tearDown := setup(t) - defer tearDown() - testUpdateSession(t, store) - }) -} - -func testCreateAndGetAndDeleteSession(t *testing.T, store store.Store) { - session := &model.Session{ - ID: "session-id", - Token: "token", - } - - t.Run("CreateAndGetSession", func(t *testing.T) { - got, err := store.GetSession(session.Token, 60*60) - require.NoError(t, err) - require.Equal(t, session, got) - }) - - t.Run("Get nonexistent session", func(t *testing.T) { - got, err := store.GetSession("nonexistent-token", 60*60) - require.True(t, model.IsErrNotFound(err)) - require.Nil(t, got) - }) - - t.Run("DeleteAndGetSession", func(t *testing.T) { - err := store.DeleteSession(session.ID) - require.NoError(t, err) - - _, err = store.GetSession(session.Token, 60*60) - require.Error(t, err) - }) } func testGetActiveUserCount(t *testing.T, store store.Store) { @@ -72,20 +32,3 @@ func testGetActiveUserCount(t *testing.T, store store.Store) { require.Equal(t, count, got) }) } - -func testUpdateSession(t *testing.T, store store.Store) { - session := &model.Session{ - ID: "session-id", - Token: "token", - Props: map[string]interface{}{"field1": "A"}, - } - - // update session - session.Props["field1"] = "B" - err := store.UpdateSession(session) - require.NoError(t, err) - - got, err := store.GetSession(session.Token, 60) - require.NoError(t, err) - require.Equal(t, session, got) -} diff --git a/server/ws/server.go b/server/ws/server.go index dd249b91..16d2b432 100644 --- a/server/ws/server.go +++ b/server/ws/server.go @@ -402,12 +402,7 @@ func (ws *Server) removeListenerFromBlock(listener *websocketSession, blockID st } func (ws *Server) getUserIDForToken(token string) string { - session, err := ws.auth.GetSession(token) - if session == nil || err != nil { - return "" - } - - return session.UserID + return "" } func (ws *Server) authenticateListener(wsSession *websocketSession, token string) { From 788f94596d12716f9f389b609dde849e40726f14 Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Tue, 9 Jul 2024 12:40:43 +0530 Subject: [PATCH 5/7] refactor: removed the unnecessary test --- server/auth/auth_test.go | 46 -------------------- server/services/store/mockstore/mockstore.go | 12 ----- 2 files changed, 58 deletions(-) diff --git a/server/auth/auth_test.go b/server/auth/auth_test.go index 489a32fc..076eefd1 100644 --- a/server/auth/auth_test.go +++ b/server/auth/auth_test.go @@ -3,17 +3,8 @@ package auth import ( "testing" - "github.com/golang/mock/gomock" "github.com/mattermost/mattermost-plugin-boards/server/model" - "github.com/mattermost/mattermost-plugin-boards/server/services/config" - "github.com/mattermost/mattermost-plugin-boards/server/services/permissions/localpermissions" - mockpermissions "github.com/mattermost/mattermost-plugin-boards/server/services/permissions/mocks" "github.com/mattermost/mattermost-plugin-boards/server/services/store/mockstore" - "github.com/mattermost/mattermost-plugin-boards/server/utils" - "github.com/pkg/errors" - "github.com/stretchr/testify/require" - - "github.com/mattermost/mattermost/server/public/shared/mlog" ) type TestHelper struct { @@ -22,43 +13,6 @@ type TestHelper struct { Store *mockstore.MockStore } -var mockSession = &model.Session{ - ID: utils.NewID(utils.IDTypeSession), - Token: "goodToken", - UserID: "12345", - CreateAt: utils.GetMillis() - utils.SecondsToMillis(2000), - UpdateAt: utils.GetMillis() - utils.SecondsToMillis(2000), -} - -func setupTestHelper(t *testing.T) *TestHelper { - ctrl := gomock.NewController(t) - ctrlPermissions := gomock.NewController(t) - cfg := config.Configuration{} - mockStore := mockstore.NewMockStore(ctrl) - mockPermissions := mockpermissions.NewMockStore(ctrlPermissions) - logger, err := mlog.NewLogger() - require.NoError(t, err) - newAuth := New(&cfg, mockStore, localpermissions.New(mockPermissions, logger)) - - // called during default template setup for every test - mockStore.EXPECT().GetTemplateBoards("0", "").AnyTimes() - mockStore.EXPECT().RemoveDefaultTemplates(gomock.Any()).AnyTimes() - mockStore.EXPECT().InsertBlock(gomock.Any(), gomock.Any()).AnyTimes() - - return &TestHelper{ - Auth: newAuth, - Session: *mockSession, - Store: mockStore, - } -} - -func TestGetSession(t *testing.T) { - th := setupTestHelper(t) - th.Store.EXPECT().GetSession("badToken", gomock.Any()).Return(nil, errors.New("Invalid Token")) - th.Store.EXPECT().GetSession("goodToken", gomock.Any()).Return(mockSession, nil) - th.Store.EXPECT().RefreshSession(gomock.Any()).Return(nil) -} - func TestIsValidReadToken(t *testing.T) { // ToDo: reimplement diff --git a/server/services/store/mockstore/mockstore.go b/server/services/store/mockstore/mockstore.go index 98639595..77c8a555 100644 --- a/server/services/store/mockstore/mockstore.go +++ b/server/services/store/mockstore/mockstore.go @@ -892,12 +892,6 @@ func (m *MockStore) GetSession(arg0 string, arg1 int64) (*model.Session, error) return ret0, ret1 } -// GetSession indicates an expected call of GetSession. -func (mr *MockStoreMockRecorder) GetSession(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSession", reflect.TypeOf((*MockStore)(nil).GetSession), arg0, arg1) -} - // GetSharing mocks base method. func (m *MockStore) GetSharing(arg0 string) (*model.Sharing, error) { m.ctrl.T.Helper() @@ -1382,12 +1376,6 @@ func (m *MockStore) RefreshSession(arg0 *model.Session) error { return ret0 } -// RefreshSession indicates an expected call of RefreshSession. -func (mr *MockStoreMockRecorder) RefreshSession(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RefreshSession", reflect.TypeOf((*MockStore)(nil).RefreshSession), arg0) -} - // RemoveDefaultTemplates mocks base method. func (m *MockStore) RemoveDefaultTemplates(arg0 []*model.Board) error { m.ctrl.T.Helper() From e04be8c4aff3ad0ec0541a888f3b0de5ad84a8f2 Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Wed, 10 Jul 2024 12:21:40 +0530 Subject: [PATCH 6/7] refactor: removed MattermostAuth --- server/api/api.go | 11 +++++---- server/api/archive.go | 42 ++--------------------------------- server/api/auth.go | 2 +- server/api/channels.go | 5 ----- server/api/search.go | 10 --------- server/api/statistics.go | 4 ---- server/api/teams.go | 20 +++++------------ server/server/initHandlers.go | 5 ----- server/server/server.go | 2 -- 9 files changed, 14 insertions(+), 87 deletions(-) delete mode 100644 server/server/initHandlers.go diff --git a/server/api/api.go b/server/api/api.go index 830f57ff..9227e251 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -34,12 +34,11 @@ var ( // REST APIs type API struct { - app *app.App - authService string - permissions permissions.PermissionsService - MattermostAuth bool - logger mlog.LoggerIFace - audit *audit.Audit + app *app.App + authService string + permissions permissions.PermissionsService + logger mlog.LoggerIFace + audit *audit.Audit } func NewAPI( diff --git a/server/api/archive.go b/server/api/archive.go index 11590f42..204b8ad2 100644 --- a/server/api/archive.go +++ b/server/api/archive.go @@ -194,45 +194,7 @@ func (a *API) handleArchiveExportTeam(w http.ResponseWriter, r *http.Request) { // description: internal error // schema: // "$ref": "#/definitions/ErrorResponse" - if a.MattermostAuth { - a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in plugin mode")) - return - } - - vars := mux.Vars(r) - teamID := vars["teamID"] - - ctx := r.Context() - session, _ := ctx.Value(sessionContextKey).(*model.Session) - userID := session.UserID - - auditRec := a.makeAuditRecord(r, "archiveExportTeam", audit.Fail) - defer a.audit.LogRecord(audit.LevelRead, auditRec) - auditRec.AddMeta("TeamID", teamID) - - boards, err := a.app.GetBoardsForUserAndTeam(userID, teamID, false) - if err != nil { - a.errorResponse(w, r, err) - return - } - ids := []string{} - for _, board := range boards { - ids = append(ids, board.ID) - } - - opts := model.ExportArchiveOptions{ - TeamID: teamID, - BoardIDs: ids, - } + a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in plugin mode")) + return - filename := fmt.Sprintf("archive-%s%s", time.Now().Format("2006-01-02"), archiveExtension) - w.Header().Set("Content-Type", "application/octet-stream") - w.Header().Set("Content-Disposition", "attachment; filename="+filename) - w.Header().Set("Content-Transfer-Encoding", "binary") - - if err := a.app.ExportArchive(w, opts); err != nil { - a.errorResponse(w, r, err) - } - - auditRec.Success() } diff --git a/server/api/auth.go b/server/api/auth.go index 1b873a1c..44f4a2dd 100644 --- a/server/api/auth.go +++ b/server/api/auth.go @@ -14,7 +14,7 @@ func (a *API) sessionRequired(handler func(w http.ResponseWriter, r *http.Reques func (a *API) attachSession(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - if a.MattermostAuth && r.Header.Get("Mattermost-User-Id") != "" { + if r.Header.Get("Mattermost-User-Id") != "" { userID := r.Header.Get("Mattermost-User-Id") now := utils.GetMillis() session := &model.Session{ diff --git a/server/api/channels.go b/server/api/channels.go index 234e5b76..d9acc3c7 100644 --- a/server/api/channels.go +++ b/server/api/channels.go @@ -50,11 +50,6 @@ func (a *API) handleGetChannel(w http.ResponseWriter, r *http.Request) { // schema: // "$ref": "#/definitions/ErrorResponse" - if !a.MattermostAuth { - a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in standalone mode")) - return - } - teamID := mux.Vars(r)["teamID"] channelID := mux.Vars(r)["channelID"] userID := getUserID(r) diff --git a/server/api/search.go b/server/api/search.go index 86dd16e1..684bb487 100644 --- a/server/api/search.go +++ b/server/api/search.go @@ -51,11 +51,6 @@ func (a *API) handleSearchMyChannels(w http.ResponseWriter, r *http.Request) { // schema: // "$ref": "#/definitions/ErrorResponse" - if !a.MattermostAuth { - a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in standalone mode")) - return - } - query := r.URL.Query() searchQuery := query.Get("search") @@ -220,11 +215,6 @@ func (a *API) handleSearchLinkableBoards(w http.ResponseWriter, r *http.Request) // schema: // "$ref": "#/definitions/ErrorResponse" - if !a.MattermostAuth { - a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in standalone mode")) - return - } - teamID := mux.Vars(r)["teamID"] term := r.URL.Query().Get("q") userID := getUserID(r) diff --git a/server/api/statistics.go b/server/api/statistics.go index bf773eaa..9f4a10b8 100644 --- a/server/api/statistics.go +++ b/server/api/statistics.go @@ -33,10 +33,6 @@ func (a *API) handleStatistics(w http.ResponseWriter, r *http.Request) { // description: internal error // schema: // "$ref": "#/definitions/ErrorResponse" - if !a.MattermostAuth { - a.errorResponse(w, r, model.NewErrNotImplemented("not permitted in standalone mode")) - return - } // user must have right to access analytics userID := getUserID(r) diff --git a/server/api/teams.go b/server/api/teams.go index 81d1179b..3d864178 100644 --- a/server/api/teams.go +++ b/server/api/teams.go @@ -100,20 +100,12 @@ func (a *API) handleGetTeam(w http.ResponseWriter, r *http.Request) { var team *model.Team var err error - if a.MattermostAuth { - team, err = a.app.GetTeam(teamID) - if model.IsErrNotFound(err) { - a.errorResponse(w, r, model.NewErrUnauthorized("invalid team")) - } - if err != nil { - a.errorResponse(w, r, err) - } - } else { - team, err = a.app.GetRootTeam() - if err != nil { - a.errorResponse(w, r, err) - return - } + team, err = a.app.GetTeam(teamID) + if model.IsErrNotFound(err) { + a.errorResponse(w, r, model.NewErrUnauthorized("invalid team")) + } + if err != nil { + a.errorResponse(w, r, err) } auditRec := a.makeAuditRecord(r, "getTeam", audit.Fail) diff --git a/server/server/initHandlers.go b/server/server/initHandlers.go deleted file mode 100644 index 6f71a462..00000000 --- a/server/server/initHandlers.go +++ /dev/null @@ -1,5 +0,0 @@ -package server - -func (s *Server) initHandlers() { - s.api.MattermostAuth = true -} diff --git a/server/server/server.go b/server/server/server.go index 2139b0f8..97d764f9 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -198,8 +198,6 @@ func New(params Params) (*Server, error) { app: app, } - server.initHandlers() - return &server, nil } From 89cbecac272e8aa33aa5c44ef771928b39527032 Mon Sep 17 00:00:00 2001 From: Rajat Dabade Date: Sat, 13 Jul 2024 11:42:26 +0530 Subject: [PATCH 7/7] Deperate isPlugin and isSingleUser part - 2 (#5) * removed remaining singleUser and isPlugin code * chore: linter fixes --- server/boards/boardsapp.go | 1 - server/integrationtests/clienttestlib.go | 7 +- server/server/server.go | 4 +- .../store/sqlstore/data_migrations.go | 30 ++--- .../services/store/sqlstore/helpers_test.go | 1 - server/services/store/sqlstore/migrate.go | 32 +++--- .../migrationstests/boards_migrator_test.go | 1 - .../sqlstore/migrationstests/helpers_test.go | 18 ++- server/services/store/sqlstore/params.go | 4 +- server/services/store/sqlstore/sqlstore.go | 4 - webapp/src/pages/changePasswordPage.scss | 73 ------------ webapp/src/pages/changePasswordPage.tsx | 99 ---------------- webapp/src/pages/loginPage.scss | 68 ----------- webapp/src/pages/loginPage.tsx | 103 ----------------- webapp/src/pages/registerPage.scss | 69 ------------ webapp/src/pages/registerPage.tsx | 106 ------------------ webapp/src/router.tsx | 69 +++--------- 17 files changed, 52 insertions(+), 637 deletions(-) delete mode 100644 webapp/src/pages/changePasswordPage.scss delete mode 100644 webapp/src/pages/changePasswordPage.tsx delete mode 100644 webapp/src/pages/loginPage.scss delete mode 100644 webapp/src/pages/loginPage.tsx delete mode 100644 webapp/src/pages/registerPage.scss delete mode 100644 webapp/src/pages/registerPage.tsx diff --git a/server/boards/boardsapp.go b/server/boards/boardsapp.go index 5e08fdfa..7fcc2a9f 100644 --- a/server/boards/boardsapp.go +++ b/server/boards/boardsapp.go @@ -78,7 +78,6 @@ func NewBoardsApp(api model.ServicesAPI) (*BoardsApp, error) { TablePrefix: cfg.DBTablePrefix, Logger: logger, DB: sqlDB, - IsPlugin: true, NewMutexFn: func(name string) (*cluster.Mutex, error) { return cluster.NewMutex(&mutexAPIAdapter{api: api}, name) }, diff --git a/server/integrationtests/clienttestlib.go b/server/integrationtests/clienttestlib.go index 38048576..27cb15bb 100644 --- a/server/integrationtests/clienttestlib.go +++ b/server/integrationtests/clienttestlib.go @@ -150,8 +150,7 @@ func newTestServerWithLicense(singleUserToken string, licenseType LicenseType) * if err = logger.Configure("", cfg.LoggingCfgJSON, nil); err != nil { panic(err) } - singleUser := len(singleUserToken) > 0 - innerStore, err := server.NewStore(cfg, singleUser, logger) + innerStore, err := server.NewStore(cfg, logger) if err != nil { panic(err) } @@ -199,7 +198,7 @@ func NewTestServerPluginMode() *server.Server { if err = logger.Configure("", cfg.LoggingCfgJSON, nil); err != nil { panic(err) } - innerStore, err := server.NewStore(cfg, false, logger) + innerStore, err := server.NewStore(cfg, logger) if err != nil { panic(err) } @@ -235,7 +234,7 @@ func newTestServerLocalMode() *server.Server { panic(err) } - db, err := server.NewStore(cfg, false, logger) + db, err := server.NewStore(cfg, logger) if err != nil { panic(err) } diff --git a/server/server/server.go b/server/server/server.go index 97d764f9..e5f1c578 100644 --- a/server/server/server.go +++ b/server/server/server.go @@ -201,7 +201,7 @@ func New(params Params) (*Server, error) { return &server, nil } -func NewStore(config *config.Configuration, isSingleUser bool, logger mlog.LoggerIFace) (store.Store, error) { +func NewStore(config *config.Configuration, logger mlog.LoggerIFace) (store.Store, error) { sqlDB, err := sql.Open(config.DBType, config.DBConfigString) if err != nil { logger.Error("connectDatabase failed", mlog.Err(err)) @@ -221,8 +221,6 @@ func NewStore(config *config.Configuration, isSingleUser bool, logger mlog.Logge TablePrefix: config.DBTablePrefix, Logger: logger, DB: sqlDB, - IsPlugin: false, - IsSingleUser: isSingleUser, } var db store.Store diff --git a/server/services/store/sqlstore/data_migrations.go b/server/services/store/sqlstore/data_migrations.go index 07169ae5..264a83d0 100644 --- a/server/services/store/sqlstore/data_migrations.go +++ b/server/services/store/sqlstore/data_migrations.go @@ -154,20 +154,18 @@ func (s *SQLStore) RunCategoryUUIDIDMigration() error { return txErr } - if s.isPlugin { - if err := s.createCategories(tx); err != nil { - if rollbackErr := tx.Rollback(); rollbackErr != nil { - s.logger.Error("category UUIDs insert categories transaction rollback error", mlog.Err(rollbackErr), mlog.String("methodName", "setSystemSetting")) - } - return err + if err := s.createCategories(tx); err != nil { + if rollbackErr := tx.Rollback(); rollbackErr != nil { + s.logger.Error("category UUIDs insert categories transaction rollback error", mlog.Err(rollbackErr), mlog.String("methodName", "setSystemSetting")) } + return err + } - if err := s.createCategoryBoards(tx); err != nil { - if rollbackErr := tx.Rollback(); rollbackErr != nil { - s.logger.Error("category UUIDs insert category boards transaction rollback error", mlog.Err(rollbackErr), mlog.String("methodName", "setSystemSetting")) - } - return err + if err := s.createCategoryBoards(tx); err != nil { + if rollbackErr := tx.Rollback(); rollbackErr != nil { + s.logger.Error("category UUIDs insert category boards transaction rollback error", mlog.Err(rollbackErr), mlog.String("methodName", "setSystemSetting")) } + return err } if err := s.setSystemSetting(tx, CategoryUUIDIDMigrationKey, strconv.FormatBool(true)); err != nil { @@ -353,10 +351,6 @@ func (s *SQLStore) createCategoryBoards(db sq.BaseRunner) error { // group messages. This function migrates all boards // belonging to a DM to the best possible team. func (s *SQLStore) RunTeamLessBoardsMigration() error { - if !s.isPlugin { - return nil - } - setting, err := s.GetSystemSetting(TeamLessBoardsMigrationKey) if err != nil { return fmt.Errorf("cannot get teamless boards migration state: %w", err) @@ -555,10 +549,6 @@ func (s *SQLStore) getBoardUserTeams(tx sq.BaseRunner, board *model.Board) (map[ } func (s *SQLStore) RunDeletedMembershipBoardsMigration() error { - if !s.isPlugin { - return nil - } - setting, err := s.GetSystemSetting(DeletedMembershipBoardsMigrationKey) if err != nil { return fmt.Errorf("cannot get deleted membership boards migration state: %w", err) @@ -661,7 +651,7 @@ func (s *SQLStore) RunFixCollationsAndCharsetsMigration() error { var collation string var charSet string var err error - if !s.isPlugin || os.Getenv("FOCALBOARD_UNIT_TESTING") == "1" { + if os.Getenv("FOCALBOARD_UNIT_TESTING") == "1" { collation = "utf8mb4_general_ci" charSet = "utf8mb4" } else { diff --git a/server/services/store/sqlstore/helpers_test.go b/server/services/store/sqlstore/helpers_test.go index 95ad6397..952e4179 100644 --- a/server/services/store/sqlstore/helpers_test.go +++ b/server/services/store/sqlstore/helpers_test.go @@ -32,7 +32,6 @@ func SetupTests(t *testing.T) (store.Store, func()) { TablePrefix: "test_", Logger: logger, DB: sqlDB, - IsPlugin: false, } store, err := New(storeParams) require.NoError(t, err) diff --git a/server/services/store/sqlstore/migrate.go b/server/services/store/sqlstore/migrate.go index 294e1c00..41986568 100644 --- a/server/services/store/sqlstore/migrate.go +++ b/server/services/store/sqlstore/migrate.go @@ -74,20 +74,18 @@ func (s *SQLStore) getMigrationConnection() (*sql.DB, error) { } func (s *SQLStore) Migrate() error { - if s.isPlugin { - mutex, mutexErr := s.NewMutexFn("Boards_dbMutex") - if mutexErr != nil { - return fmt.Errorf("error creating database mutex: %w", mutexErr) - } - - s.logger.Debug("Acquiring cluster lock for Focalboard migrations") - mutex.Lock() - defer func() { - s.logger.Debug("Releasing cluster lock for Focalboard migrations") - mutex.Unlock() - }() + mutex, mutexErr := s.NewMutexFn("Boards_dbMutex") + if mutexErr != nil { + return fmt.Errorf("error creating database mutex: %w", mutexErr) } + s.logger.Debug("Acquiring cluster lock for Focalboard migrations") + mutex.Lock() + defer func() { + s.logger.Debug("Releasing cluster lock for Focalboard migrations") + mutex.Unlock() + }() + if err := s.EnsureSchemaMigrationFormat(); err != nil { return err } @@ -148,12 +146,10 @@ func (s *SQLStore) Migrate() error { } params := map[string]interface{}{ - "prefix": s.tablePrefix, - "postgres": s.dbType == model.PostgresDBType, - "sqlite": s.dbType == model.SqliteDBType, - "mysql": s.dbType == model.MysqlDBType, - "plugin": s.isPlugin, - "singleUser": s.isSingleUser, + "prefix": s.tablePrefix, + "postgres": s.dbType == model.PostgresDBType, + "sqlite": s.dbType == model.SqliteDBType, + "mysql": s.dbType == model.MysqlDBType, } migrationAssets := &embedded.AssetSource{ diff --git a/server/services/store/sqlstore/migrationstests/boards_migrator_test.go b/server/services/store/sqlstore/migrationstests/boards_migrator_test.go index d82c6c0a..11f60167 100644 --- a/server/services/store/sqlstore/migrationstests/boards_migrator_test.go +++ b/server/services/store/sqlstore/migrationstests/boards_migrator_test.go @@ -210,7 +210,6 @@ func (bm *BoardsMigrator) Setup() error { TablePrefix: tablePrefix, Logger: logger, DB: bm.db, - IsPlugin: bm.withMattermostMigrations, NewMutexFn: func(name string) (*cluster.Mutex, error) { return nil, fmt.Errorf("not implemented") }, diff --git a/server/services/store/sqlstore/migrationstests/helpers_test.go b/server/services/store/sqlstore/migrationstests/helpers_test.go index c4532e45..4508828a 100644 --- a/server/services/store/sqlstore/migrationstests/helpers_test.go +++ b/server/services/store/sqlstore/migrationstests/helpers_test.go @@ -10,9 +10,8 @@ import ( ) type TestHelper struct { - t *testing.T - f *foundation.Foundation - isPlugin bool + t *testing.T + f *foundation.Foundation } func (th *TestHelper) IsPostgres() bool { @@ -33,20 +32,19 @@ func SetupPluginTestHelper(t *testing.T) (*TestHelper, func()) { t.Skip("Skipping plugin mode test for SQLite") } - return setupTestHelper(t, true) + return setupTestHelper(t) } func SetupTestHelper(t *testing.T) (*TestHelper, func()) { - return setupTestHelper(t, false) + return setupTestHelper(t) } -func setupTestHelper(t *testing.T, isPlugin bool) (*TestHelper, func()) { - f := foundation.New(t, NewBoardsMigrator(isPlugin)) +func setupTestHelper(t *testing.T) (*TestHelper, func()) { + f := foundation.New(t, NewBoardsMigrator(true)) th := &TestHelper{ - t: t, - f: f, - isPlugin: isPlugin, + t: t, + f: f, } tearDown := func() { diff --git a/server/services/store/sqlstore/params.go b/server/services/store/sqlstore/params.go index eb2e3849..352ae3b3 100644 --- a/server/services/store/sqlstore/params.go +++ b/server/services/store/sqlstore/params.go @@ -22,8 +22,6 @@ type Params struct { TablePrefix string Logger mlog.LoggerIFace DB *sql.DB - IsPlugin bool - IsSingleUser bool NewMutexFn MutexFactory ServicesAPI servicesAPI SkipMigrations bool @@ -31,7 +29,7 @@ type Params struct { } func (p Params) CheckValid() error { - if p.IsPlugin && p.NewMutexFn == nil { + if p.NewMutexFn == nil { return ErrStoreParam{name: "NewMutexFn", issue: "cannot be nil in plugin mode"} } return nil diff --git a/server/services/store/sqlstore/sqlstore.go b/server/services/store/sqlstore/sqlstore.go index a0148c2c..2165be4f 100644 --- a/server/services/store/sqlstore/sqlstore.go +++ b/server/services/store/sqlstore/sqlstore.go @@ -22,8 +22,6 @@ type SQLStore struct { tablePrefix string connectionString string dbPingAttempts int - isPlugin bool - isSingleUser bool logger mlog.LoggerIFace NewMutexFn MutexFactory servicesAPI servicesAPI @@ -51,8 +49,6 @@ func New(params Params) (*SQLStore, error) { tablePrefix: params.TablePrefix, connectionString: params.ConnectionString, logger: params.Logger, - isPlugin: params.IsPlugin, - isSingleUser: params.IsSingleUser, NewMutexFn: params.NewMutexFn, servicesAPI: params.ServicesAPI, configFn: params.ConfigFn, diff --git a/webapp/src/pages/changePasswordPage.scss b/webapp/src/pages/changePasswordPage.scss deleted file mode 100644 index 15b37ec2..00000000 --- a/webapp/src/pages/changePasswordPage.scss +++ /dev/null @@ -1,73 +0,0 @@ -.ChangePasswordPage { - border: 1px solid #ccc; - border-radius: 15px; - width: 450px; - height: 400px; - margin: 150px auto; - padding: 40px; - display: flex; - align-items: center; - justify-content: flex-start; - flex-direction: column; - box-shadow: rgba(var(--center-channel-color-rgb), 0.1) 0 0 0 1px, - rgba(var(--center-channel-color-rgb), 0.3) 0 4px 8px; - - form { - display: flex; - flex-direction: column; - align-items: flex-start; - justify-content: center; - } - - @media screen and (max-width: 430px) { - position: fixed; - top: 0; - left: 0; - right: 0; - bottom: 0; - width: 100%; - height: 100%; - margin: auto; - padding-top: 10px; - } - - .title { - font-size: 16px; - font-weight: 500; - } - - .oldPassword, - .newPassword { - margin-bottom: 10px; - - label { - display: inline-block; - width: 140px; - } - - input { - display: inline-block; - width: 250px; - border: 1px solid #ccc; - border-radius: 4px; - padding: 7px; - min-height: 44px; - } - } - - form > .Button { - margin-top: 10px; - margin-bottom: 20px; - min-height: 38px; - min-width: 250px; - } - - .error { - color: #900000; - } - - .succeeded { - background-color: #cfc; - padding: 5px; - } -} diff --git a/webapp/src/pages/changePasswordPage.tsx b/webapp/src/pages/changePasswordPage.tsx deleted file mode 100644 index dc969729..00000000 --- a/webapp/src/pages/changePasswordPage.tsx +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. -import React, {useState} from 'react' -import {Link} from 'react-router-dom' - -import Button from '../widgets/buttons/button' -import client from '../octoClient' -import './changePasswordPage.scss' -import {IUser} from '../user' -import {useAppSelector} from '../store/hooks' -import {getMe} from '../store/users' - -const ChangePasswordPage = () => { - const [oldPassword, setOldPassword] = useState('') - const [newPassword, setNewPassword] = useState('') - const [errorMessage, setErrorMessage] = useState('') - const [succeeded, setSucceeded] = useState(false) - const user = useAppSelector(getMe) - - if (!user) { - return ( -
-
{'Change Password'}
- {'Log in first'} -
- ) - } - - const handleSubmit = async (userId: string): Promise => { - const response = await client.changePassword(userId, oldPassword, newPassword) - if (response.code === 200) { - setOldPassword('') - setNewPassword('') - setErrorMessage('') - setSucceeded(true) - } else { - setErrorMessage(`Change password failed: ${response.json?.error}`) - } - } - - return ( -
-
{'Change Password'}
-
{ - e.preventDefault() - handleSubmit(user.id) - }} - > -
- { - setOldPassword(e.target.value) - setErrorMessage('') - }} - /> -
-
- { - setNewPassword(e.target.value) - setErrorMessage('') - }} - /> -
- -
- {errorMessage && -
- {errorMessage} -
- } - {succeeded && - {'Password changed, click to continue.'} - } - {!succeeded && - {'Cancel'} - } -
- ) -} - -export default React.memo(ChangePasswordPage) diff --git a/webapp/src/pages/loginPage.scss b/webapp/src/pages/loginPage.scss deleted file mode 100644 index 78f440d6..00000000 --- a/webapp/src/pages/loginPage.scss +++ /dev/null @@ -1,68 +0,0 @@ -.LoginPage { - border: 1px solid #ccc; - border-radius: 15px; - width: 450px; - height: 400px; - margin: 150px auto; - display: flex; - align-items: center; - justify-content: flex-start; - flex-direction: column; - box-shadow: rgba(var(--center-channel-color-rgb), 0.1) 0 0 0 1px, - rgba(var(--center-channel-color-rgb), 0.3) 0 4px 8px; - - form { - display: flex; - flex-direction: column; - align-items: flex-start; - justify-content: center; - margin: 50px auto; - } - - @media screen and (max-width: 430px) { - position: fixed; - top: 0; - left: 0; - right: 0; - bottom: 0; - width: 100%; - height: 100%; - margin: auto; - padding-top: 10px; - } - - .title { - font-size: 16px; - font-weight: 500; - } - - .username, - .password { - margin-bottom: 10px; - - label { - display: inline-block; - width: 140px; - } - - input { - display: inline-block; - width: 250px; - border: 1px solid #ccc; - border-radius: 4px; - padding: 7px; - min-height: 44px; - } - } - - form > .Button { - margin-top: 10px; - margin-bottom: 20px; - min-height: 38px; - min-width: 250px; - } - - .error { - color: #900000; - } -} diff --git a/webapp/src/pages/loginPage.tsx b/webapp/src/pages/loginPage.tsx deleted file mode 100644 index 702b8e40..00000000 --- a/webapp/src/pages/loginPage.tsx +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. -import React, {useState} from 'react' -import {Link, Redirect, useLocation, useHistory} from 'react-router-dom' -import {FormattedMessage} from 'react-intl' - -import {useAppDispatch, useAppSelector} from '../store/hooks' -import {fetchMe, getLoggedIn} from '../store/users' - -import Button from '../widgets/buttons/button' -import client from '../octoClient' -import './loginPage.scss' - -const LoginPage = () => { - const [username, setUsername] = useState('') - const [password, setPassword] = useState('') - const [errorMessage, setErrorMessage] = useState('') - const dispatch = useAppDispatch() - const loggedIn = useAppSelector(getLoggedIn) - const queryParams = new URLSearchParams(useLocation().search) - const history = useHistory() - - const handleLogin = async (): Promise => { - const logged = await client.login(username, password) - if (logged) { - await dispatch(fetchMe()) - if (queryParams) { - history.push(queryParams.get('r') || '/') - } else { - history.push('/') - } - } else { - setErrorMessage('Login failed') - } - } - - if (loggedIn) { - return - } - - return ( -
-
{ - e.preventDefault() - handleLogin() - }} - > -
- -
-
- { - setUsername(e.target.value) - setErrorMessage('') - }} - /> -
-
- { - setPassword(e.target.value) - setErrorMessage('') - }} - /> -
- -
- - - - {errorMessage && -
- {errorMessage} -
- } -
- ) -} - -export default React.memo(LoginPage) diff --git a/webapp/src/pages/registerPage.scss b/webapp/src/pages/registerPage.scss deleted file mode 100644 index 20a47fe7..00000000 --- a/webapp/src/pages/registerPage.scss +++ /dev/null @@ -1,69 +0,0 @@ -.RegisterPage { - border: 1px solid #ccc; - border-radius: 15px; - width: 450px; - height: 600px; - margin: 150px auto; - padding: 40px; - display: flex; - align-items: center; - justify-content: flex-start; - flex-direction: column; - box-shadow: rgba(var(--center-channel-color-rgb), 0.1) 0 0 0 1px, - rgba(var(--center-channel-color-rgb), 0.3) 0 4px 8px; - - form { - display: flex; - flex-direction: column; - align-items: flex-start; - justify-content: center; - } - - @media screen and (max-width: 430px) { - position: fixed; - top: 0; - left: 0; - right: 0; - bottom: 0; - width: 100%; - height: 100%; - margin: auto; - padding-top: 10px; - } - - .title { - font-size: 16px; - font-weight: 500; - } - - .email, - .username, - .password { - margin-bottom: 10px; - - label { - display: inline-block; - width: 140px; - } - - input { - display: inline-block; - width: 250px; - border: 1px solid #ccc; - border-radius: 4px; - min-height: 44px; - padding: 7px; - } - } - - form > .Button { - margin-top: 10px; - margin-bottom: 20px; - min-height: 38px; - min-width: 250px; - } - - .error { - color: #900000; - } -} diff --git a/webapp/src/pages/registerPage.tsx b/webapp/src/pages/registerPage.tsx deleted file mode 100644 index bb8c5609..00000000 --- a/webapp/src/pages/registerPage.tsx +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. -// See LICENSE.txt for license information. -import React, {useState} from 'react' -import {useHistory, Link, Redirect} from 'react-router-dom' -import {FormattedMessage} from 'react-intl' - -import {useAppDispatch, useAppSelector} from '../store/hooks' -import {fetchMe, getLoggedIn} from '../store/users' - -import Button from '../widgets/buttons/button' -import client from '../octoClient' -import './registerPage.scss' - -const RegisterPage = () => { - const [username, setUsername] = useState('') - const [password, setPassword] = useState('') - const [email, setEmail] = useState('') - const [errorMessage, setErrorMessage] = useState('') - const history = useHistory() - const dispatch = useAppDispatch() - const loggedIn = useAppSelector(getLoggedIn) - - const handleRegister = async (): Promise => { - const queryString = new URLSearchParams(window.location.search) - const signupToken = queryString.get('t') || '' - - const response = await client.register(email, username, password, signupToken) - if (response.code === 200) { - const logged = await client.login(username, password) - if (logged) { - await dispatch(fetchMe()) - history.push('/') - } - } else if (response.code === 401) { - setErrorMessage('Invalid registration link, please contact your administrator') - } else { - setErrorMessage(`${response.json?.error}`) - } - } - - if (loggedIn) { - return - } - - return ( -
-
{ - e.preventDefault() - handleRegister() - }} - > -
- -
-
- setEmail(e.target.value.trim())} - /> -
-
- setUsername(e.target.value.trim())} - /> -
-
- setPassword(e.target.value)} - /> -
- -
- - - - {errorMessage && -
- {errorMessage} -
- } -
- ) -} - -export default React.memo(RegisterPage) diff --git a/webapp/src/router.tsx b/webapp/src/router.tsx index 01b67f1e..02de0445 100644 --- a/webapp/src/router.tsx +++ b/webapp/src/router.tsx @@ -14,11 +14,8 @@ import {createBrowserHistory, History} from 'history' import {IAppWindow} from './types' import BoardPage from './pages/boardPage/boardPage' -import ChangePasswordPage from './pages/changePasswordPage' import WelcomePage from './pages/welcome/welcomePage' import ErrorPage from './pages/errorPage' -import LoginPage from './pages/loginPage' -import RegisterPage from './pages/registerPage' import {Utils} from './utils' import octoClient from './octoClient' import {setGlobalError, getGlobalError} from './store/globalError' @@ -29,8 +26,6 @@ import FBRoute from './route' declare let window: IAppWindow -const UUID_REGEX = new RegExp(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/) - function HomeToCurrentTeam(props: {path: string, exact: boolean}) { return ( { - const isPlugin = Utils.isFocalboardPlugin() let browserHistory: History if (props.history) { @@ -123,48 +117,30 @@ const FocalboardRouter = (props: Props): JSX.Element => { }, []) } - if (isPlugin) { - useEffect(() => { - if (window.frontendBaseURL) { - browserHistory.replace(window.location.pathname.replace(window.frontendBaseURL, '')) - } - }, []) - } + useEffect(() => { + if (window.frontendBaseURL) { + browserHistory.replace(window.location.pathname.replace(window.frontendBaseURL, '')) + } + }, []) return ( - {isPlugin && - } - {isPlugin && - - - } + + + + - - {!isPlugin && - - - } - {!isPlugin && - - - } - {!isPlugin && - - - } - @@ -194,21 +170,6 @@ const FocalboardRouter = (props: Props): JSX.Element => { > - - {!isPlugin && - { - const boardIdIsValidUUIDV4 = UUID_REGEX.test(boardId || '') - if (boardIdIsValidUUIDV4) { - return `/${Utils.buildOriginalPath('', boardId, viewId, cardId)}` - } - return '' - }} - > - - } )